2016-09-24 13:29:23 +08:00
|
|
|
package fastcgi
|
|
|
|
|
2016-11-17 12:29:43 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2016-11-20 00:05:29 +08:00
|
|
|
"time"
|
2016-11-17 12:29:43 +08:00
|
|
|
)
|
2016-09-24 13:29:23 +08:00
|
|
|
|
|
|
|
type dialer interface {
|
2016-11-17 12:29:43 +08:00
|
|
|
Dial() (Client, error)
|
|
|
|
Close(Client) error
|
2016-09-24 13:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// basicDialer is a basic dialer that wraps default fcgi functions.
|
|
|
|
type basicDialer struct {
|
2016-11-20 00:05:29 +08:00
|
|
|
network string
|
|
|
|
address string
|
|
|
|
timeout time.Duration
|
2016-09-24 13:29:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-20 00:05:29 +08:00
|
|
|
func (b basicDialer) Dial() (Client, error) { return Dial(b.network, b.address, b.timeout) }
|
2016-11-17 12:29:43 +08:00
|
|
|
func (b basicDialer) Close(c Client) error { return c.Close() }
|
2016-09-24 13:29:23 +08:00
|
|
|
|
|
|
|
// persistentDialer keeps a pool of fcgi connections.
|
|
|
|
// connections are not closed after use, rather added back to the pool for reuse.
|
|
|
|
type persistentDialer struct {
|
|
|
|
size int
|
|
|
|
network string
|
|
|
|
address string
|
2016-11-20 00:05:29 +08:00
|
|
|
timeout time.Duration
|
2016-11-17 12:29:43 +08:00
|
|
|
pool []Client
|
2016-09-24 13:29:23 +08:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:29:43 +08:00
|
|
|
func (p *persistentDialer) Dial() (Client, error) {
|
2016-09-24 13:29:23 +08:00
|
|
|
p.Lock()
|
|
|
|
// connection is available, return first one.
|
|
|
|
if len(p.pool) > 0 {
|
|
|
|
client := p.pool[0]
|
|
|
|
p.pool = p.pool[1:]
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// no connection available, create new one
|
2016-11-20 00:05:29 +08:00
|
|
|
return Dial(p.network, p.address, p.timeout)
|
2016-09-24 13:29:23 +08:00
|
|
|
}
|
|
|
|
|
2016-11-17 12:29:43 +08:00
|
|
|
func (p *persistentDialer) Close(client Client) error {
|
2016-09-24 13:29:23 +08:00
|
|
|
p.Lock()
|
|
|
|
if len(p.pool) < p.size {
|
|
|
|
// pool is not full yet, add connection for reuse
|
|
|
|
p.pool = append(p.pool, client)
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Unlock()
|
|
|
|
|
|
|
|
// otherwise, close the connection.
|
|
|
|
return client.Close()
|
|
|
|
}
|
2016-11-17 12:29:43 +08:00
|
|
|
|
|
|
|
type loadBalancingDialer struct {
|
|
|
|
dialers []dialer
|
|
|
|
current int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loadBalancingDialer) Dial() (Client, error) {
|
|
|
|
nextDialerIndex := atomic.AddInt64(&m.current, 1) % int64(len(m.dialers))
|
|
|
|
currentDialer := m.dialers[nextDialerIndex]
|
|
|
|
|
|
|
|
client, err := currentDialer.Dial()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dialerAwareClient{Client: client, dialer: currentDialer}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loadBalancingDialer) Close(c Client) error {
|
|
|
|
// Close the client according to dialer behaviour
|
|
|
|
if da, ok := c.(*dialerAwareClient); ok {
|
|
|
|
return da.dialer.Close(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Cannot close client")
|
|
|
|
}
|
|
|
|
|
|
|
|
type dialerAwareClient struct {
|
|
|
|
Client
|
|
|
|
dialer dialer
|
|
|
|
}
|