Skip to content

Commit

Permalink
Add connection pool queuing strategies in HostClient.
Browse files Browse the repository at this point in the history
* At first, we implement LIFO(conventional) and FIFO which indicate in #1236
  • Loading branch information
u5surf committed Mar 5, 2022
1 parent 62c15a5 commit 99455b3
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 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 (
LIFO ConnPoolStrategyType = iota
FIFO
)

// 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 determines whether LIFO or FIFO storategy for connection pool
ConnPoolStrategy ConnPoolStrategyType

clientName atomic.Value
lastUseTime uint32

Expand Down Expand Up @@ -1544,10 +1555,17 @@ 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]
c.conns[0] = nil
c.conns = c.conns[1:]
}
}
c.connsLock.Unlock()

Expand Down

0 comments on commit 99455b3

Please sign in to comment.