switch for persistent fcgi connections on/off added

This commit is contained in:
Echsecutor 2016-09-01 13:01:32 +02:00
parent 281daad8d4
commit f15c59cc3d

View file

@ -40,6 +40,9 @@ type Handler struct {
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for _, rule := range h.Rules { for _, rule := range h.Rules {
//TODO: add an option in Caddyfile and pass it down to here
use_persistent_fcgi_connections := true
// First requirement: Base path must match and the path must be allowed. // First requirement: Base path must match and the path must be allowed.
if !httpserver.Path(r.URL.Path).Matches(rule.Path) || !rule.AllowedPath(r.URL.Path) { if !httpserver.Path(r.URL.Path).Matches(rule.Path) || !rule.AllowedPath(r.URL.Path) {
continue continue
@ -79,28 +82,32 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
// Connect to FastCGI gateway // Connect to FastCGI gateway
network, address := rule.parseAddress() network, address := rule.parseAddress()
// re use connection, if possible var fcgiBackend *FCGIClient
if persistent_connections == nil { var mut *sync.Mutex
persistent_connections = make(map[string]*FCGIClient) ok := false
poor_mans_serialisation = make(map[string]*sync.Mutex) if use_persistent_fcgi_connections {
} // re use connection, if possible
mut, ok := poor_mans_serialisation[network+address] if persistent_connections == nil {
if !ok || mut == nil { persistent_connections = make(map[string]*FCGIClient)
poor_mans_serialisation[network+address] = new(sync.Mutex) poor_mans_serialisation = make(map[string]*sync.Mutex)
} }
poor_mans_serialisation[network+address].Lock() mut, ok = poor_mans_serialisation[network+address]
defer poor_mans_serialisation[network+address].Unlock() if !ok || mut == nil {
poor_mans_serialisation[network+address] = new(sync.Mutex)
}
poor_mans_serialisation[network+address].Lock()
defer poor_mans_serialisation[network+address].Unlock()
fcgiBackend, ok := persistent_connections[network+address] fcgiBackend, ok = persistent_connections[network+address]
}
// otherwise dial: // otherwise dial:
if !ok || fcgiBackend == nil { if !ok || fcgiBackend == nil {
var err error var err error
persistent_connections[network+address], err = Dial(network, address) fcgiBackend, err = Dial(network, address)
if err != nil { if err != nil {
return http.StatusBadGateway, err return http.StatusBadGateway, err
} }
fcgiBackend = persistent_connections[network+address]
} }
var resp *http.Response var resp *http.Response
@ -122,6 +129,12 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
return http.StatusBadGateway, err return http.StatusBadGateway, err
} }
if use_persistent_fcgi_connections {
persistent_connections[network+address] = fcgiBackend
} else {
defer fcgiBackend.Close()
}
// Write response header // Write response header
writeHeader(w, resp) writeHeader(w, resp)