0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

poor mans serialisation to make up for the lack of demuxing

This commit is contained in:
Echsecutor 2016-09-01 10:55:14 +02:00
parent 05d7ae88fa
commit 582cecb3ef

View file

@ -12,12 +12,14 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
// for persistent fastcgi connections // for persistent fastcgi connections
var persistent_connections map[string]*FCGIClient var persistent_connections map[string]*FCGIClient
var poor_mans_serialisation map[string]*sync.Mutex
// Handler is a middleware type that can handle requests as a FastCGI client. // Handler is a middleware type that can handle requests as a FastCGI client.
type Handler struct { type Handler struct {
@ -77,21 +79,28 @@ 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 // 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)
} }
mut, ok := poor_mans_serialisation[network+address]
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) persistent_connections[network+address], err = Dial(network, address)
if err != nil { if err != nil {
return http.StatusBadGateway, err return http.StatusBadGateway, err
} }
fcgiBackend = persistent_connections[network+address] fcgiBackend = persistent_connections[network+address]
} }
var resp *http.Response var resp *http.Response
@ -107,7 +116,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
resp, err = fcgiBackend.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength) resp, err = fcgiBackend.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength)
} }
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
persistent_connections[network+address].Close() persistent_connections[network+address].Close()
persistent_connections[network+address] = nil persistent_connections[network+address] = nil