Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connection pool queuing strategies in HostClient. #1238

Merged
merged 1 commit into from Mar 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 29 additions & 4 deletions client.go
Expand Up @@ -618,6 +618,14 @@ type RetryIfFunc func(request *Request) bool
// TransportFunc wraps every request/response.
type TransportFunc func(*Request, *Response) error

// ConnPoolStrategyType define strategy of connection pool enqueue/dequeue
type ConnPoolStrategyType int

const (
FIFO ConnPoolStrategyType = iota
LIFO
)

// HostClient balances http requests among hosts listed in Addr.
//
// HostClient may be used for balancing load among multiple upstream hosts.
Expand Down Expand Up @@ -772,6 +780,9 @@ type HostClient struct {
// Transport defines a transport-like mechanism that wraps every request/response.
Transport TransportFunc

// Connection pool strategy. Can be either LIFO or FIFO (default).
ConnPoolStrategy ConnPoolStrategyType

clientName atomic.Value
lastUseTime uint32

Expand Down Expand Up @@ -1497,6 +1508,10 @@ var (
// to broken server.
ErrConnectionClosed = errors.New("the server closed connection before returning the first response byte. " +
"Make sure the server returns 'Connection: close' response header before closing the connection")

// ErrConnPoolStrategyNotImpl is returned when HostClient.ConnPoolStrategy is not implement yet.
// If you see this error, then you need to check your HostClient configuration.
ErrConnPoolStrategyNotImpl = errors.New("connection pool strategy is not implement")
)

type timeoutError struct{}
Expand Down Expand Up @@ -1544,10 +1559,20 @@ func (c *HostClient) acquireConn(reqTimeout time.Duration, connectionClose bool)
}
}
} else {
n--
cc = c.conns[n]
c.conns[n] = nil
c.conns = c.conns[:n]
switch c.ConnPoolStrategy {
case LIFO:
n--
cc = c.conns[n]
c.conns[n] = nil
c.conns = c.conns[:n]
case FIFO:
cc = c.conns[0]
copy(c.conns, c.conns[1:])
c.conns[n-1] = nil
c.conns = c.conns[:n-1]
default:
return nil, ErrConnPoolStrategyNotImpl
}
}
c.connsLock.Unlock()

Expand Down