main.go 654 B

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