main.go 749 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "log"
  4. "net"
  5. "net/http"
  6. "os"
  7. "os/signal"
  8. "runtime/debug"
  9. "time"
  10. "golang.org/x/net/netutil"
  11. )
  12. func main() {
  13. // Force garbage collection
  14. go func() {
  15. for _ = range time.Tick(10 * time.Second) {
  16. debug.FreeOSMemory()
  17. }
  18. }()
  19. l, err := net.Listen("tcp", conf.Bind)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. s := &http.Server{
  24. Handler: newHTTPHandler(),
  25. ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
  26. MaxHeaderBytes: 1 << 20,
  27. }
  28. stop := make(chan os.Signal, 1)
  29. signal.Notify(stop, os.Interrupt, os.Kill)
  30. go func() {
  31. log.Printf("Starting server at %s\n", conf.Bind)
  32. log.Fatal(s.Serve(netutil.LimitListener(l, conf.MaxClients)))
  33. }()
  34. <-stop
  35. shutdownVips()
  36. shutdownServer(s)
  37. }