// A proxy with a custom origin server connection timeout func impatientProxy(maxConns uint64, idleTimeout time.Duration) (*server.Server, error) { forwarder, err := forward.New(nil, forward.IdleTimeoutSetter(idleTimeout)) if err != nil { log.Error(err) } // Middleware: Handle HTTP CONNECT httpConnect, err := httpconnect.New(forwarder, httpconnect.IdleTimeoutSetter(idleTimeout)) if err != nil { log.Error(err) } srv := server.NewServer(httpConnect) // Add net.Listener wrappers for inbound connections srv.AddListenerWrappers( // Close connections after 30 seconds of no activity func(ls net.Listener) net.Listener { return listeners.NewIdleConnListener(ls, time.Second*30) }, ) ready := make(chan string) wait := func(addr string) { ready <- addr } go func(err *error) { if *err = srv.ServeHTTP("localhost:0", wait); err != nil { log.Errorf("Unable to serve: %s", err) } }(&err) <-ready return srv, err }
func main() { var err error _ = flag.CommandLine.Parse(os.Args[1:]) if *help { flag.Usage() return } // Logging // TODO: use real parameters err = logging.Init("instanceid", "version", "releasedate", "") if err != nil { log.Error(err) } filterChain := filters.Join( commonfilter.New(&commonfilter.Options{ AllowLocalhost: testingLocal, }), httpconnect.New(&httpconnect.Options{IdleTimeout: time.Duration(*idleClose) * time.Second}), pforward.New(&pforward.Options{Force: true, IdleTimeout: time.Duration(*idleClose) * time.Second}), ) // Create server srv := server.NewServer(filterChain) // Add net.Listener wrappers for inbound connections srv.AddListenerWrappers( // Limit max number of simultaneous connections func(ls net.Listener) net.Listener { return listeners.NewLimitedListener(ls, *maxConns) }, // Close connections after 30 seconds of no activity func(ls net.Listener) net.Listener { return listeners.NewIdleConnListener(ls, time.Duration(*idleClose)*time.Second) }, ) // Serve HTTP/S if *https { err = srv.ListenAndServeHTTPS(*addr, *keyfile, *certfile, nil) } else { err = srv.ListenAndServeHTTP(*addr, nil) } if err != nil { log.Errorf("Error serving: %v", err) } }
func basicServer(maxConns uint64, idleTimeout time.Duration) *server.Server { // Middleware: Forward HTTP Messages forwarder, err := forward.New(nil, forward.IdleTimeoutSetter(idleTimeout)) if err != nil { log.Error(err) } // Middleware: Handle HTTP CONNECT httpConnect, err := httpconnect.New(forwarder, httpconnect.IdleTimeoutSetter(idleTimeout)) if err != nil { log.Error(err) } // Middleware: Common request filter commonHandler, err := commonfilter.New(httpConnect, testingLocal) if err != nil { log.Error(err) } // Create server srv := server.NewServer(commonHandler) // Add net.Listener wrappers for inbound connections srv.AddListenerWrappers( // Limit max number of simultaneous connections func(ls net.Listener) net.Listener { return listeners.NewLimitedListener(ls, maxConns) }, // Close connections after 30 seconds of no activity func(ls net.Listener) net.Listener { return listeners.NewIdleConnListener(ls, idleTimeout) }, ) return srv }
func main() { var err error _ = flag.CommandLine.Parse(os.Args[1:]) if *help { flag.Usage() return } // Logging // TODO: use real parameters err = logging.Init("instanceid", "version", "releasedate", "") if err != nil { log.Error(err) } // Middleware (runs in reverse order as they are added) // Middleware: Forward HTTP Messages forwarder, err := forward.New(nil, forward.IdleTimeoutSetter(time.Duration(*idleClose)*time.Second)) if err != nil { log.Error(err) } // Middleware: Handle HTTP CONNECT httpConnect, err := httpconnect.New(forwarder, httpconnect.IdleTimeoutSetter(time.Duration(*idleClose)*time.Second)) if err != nil { log.Error(err) } // Middleware: Common request filter commonHandler, err := commonfilter.New(httpConnect, testingLocal) if err != nil { log.Error(err) } // Create server srv := server.NewServer(commonHandler) // Add net.Listener wrappers for inbound connections srv.AddListenerWrappers( // Limit max number of simultaneous connections func(ls net.Listener) net.Listener { return listeners.NewLimitedListener(ls, *maxConns) }, // Close connections after 30 seconds of no activity func(ls net.Listener) net.Listener { return listeners.NewIdleConnListener(ls, time.Duration(*idleClose)*time.Second) }, ) // Serve HTTP/S if *https { err = srv.ServeHTTPS(*addr, *keyfile, *certfile, nil) } else { err = srv.ServeHTTP(*addr, nil) } if err != nil { log.Errorf("Error serving: %v", err) } }