0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-03-11 02:18:21 -05:00

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()
var fcgiBackend *FCGIClient
var mut *sync.Mutex
ok := false
if use_persistent_fcgi_connections {
// re use connection, if possible // re use connection, if possible
if persistent_connections == nil { if persistent_connections == nil {
persistent_connections = make(map[string]*FCGIClient) persistent_connections = make(map[string]*FCGIClient)
poor_mans_serialisation = make(map[string]*sync.Mutex) poor_mans_serialisation = make(map[string]*sync.Mutex)
} }
mut, ok := poor_mans_serialisation[network+address] mut, ok = poor_mans_serialisation[network+address]
if !ok || mut == nil { if !ok || mut == nil {
poor_mans_serialisation[network+address] = new(sync.Mutex) poor_mans_serialisation[network+address] = new(sync.Mutex)
} }
poor_mans_serialisation[network+address].Lock() poor_mans_serialisation[network+address].Lock()
defer poor_mans_serialisation[network+address].Unlock() 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)