|
|
@@ -64,14 +64,17 @@ var (
|
|
|
ErrNoServers = errors.New("memcache: no servers configured or available")
|
|
|
)
|
|
|
|
|
|
-// DefaultTimeout is the default socket read/write timeout.
|
|
|
-const DefaultTimeout = 100 * time.Millisecond
|
|
|
-
|
|
|
const (
|
|
|
- buffered = 8 // arbitrary buffered channel size, for readability
|
|
|
- maxIdleConnsPerAddr = 2 // TODO(bradfitz): make this configurable?
|
|
|
+ // DefaultTimeout is the default socket read/write timeout.
|
|
|
+ DefaultTimeout = 100 * time.Millisecond
|
|
|
+
|
|
|
+ // DefaultMaxIdleConns is the default maximum number of idle connections
|
|
|
+ // kept for any single address.
|
|
|
+ DefaultMaxIdleConns = 2
|
|
|
)
|
|
|
|
|
|
+const buffered = 8 // arbitrary buffered channel size, for readability
|
|
|
+
|
|
|
// resumableError returns true if err is only a protocol-level cache error.
|
|
|
// This is used to determine whether or not a server connection should
|
|
|
// be re-used or not. If an error occurs, by default we don't reuse the
|
|
|
@@ -133,6 +136,14 @@ type Client struct {
|
|
|
// If zero, DefaultTimeout is used.
|
|
|
Timeout time.Duration
|
|
|
|
|
|
+ // MaxIdleConns specifies the maximum number of idle connections that will
|
|
|
+ // be maintained per address. If less than one, DefaultMaxIdleConns will be
|
|
|
+ // used.
|
|
|
+ //
|
|
|
+ // Consider your expected traffic rates and latency carefully. This should
|
|
|
+ // be set to a number higher than your peak parallel requests.
|
|
|
+ MaxIdleConns int
|
|
|
+
|
|
|
selector ServerSelector
|
|
|
|
|
|
lk sync.Mutex
|
|
|
@@ -196,7 +207,7 @@ func (c *Client) putFreeConn(addr net.Addr, cn *conn) {
|
|
|
c.freeconn = make(map[string][]*conn)
|
|
|
}
|
|
|
freelist := c.freeconn[addr.String()]
|
|
|
- if len(freelist) >= maxIdleConnsPerAddr {
|
|
|
+ if len(freelist) >= c.maxIdleConns() {
|
|
|
cn.nc.Close()
|
|
|
return
|
|
|
}
|
|
|
@@ -225,6 +236,13 @@ func (c *Client) netTimeout() time.Duration {
|
|
|
return DefaultTimeout
|
|
|
}
|
|
|
|
|
|
+func (c *Client) maxIdleConns() int {
|
|
|
+ if c.MaxIdleConns > 0 {
|
|
|
+ return c.MaxIdleConns
|
|
|
+ }
|
|
|
+ return DefaultMaxIdleConns
|
|
|
+}
|
|
|
+
|
|
|
// ConnectTimeoutError is the error type used when it takes
|
|
|
// too long to connect to the desired host. This level of
|
|
|
// detail can generally be ignored.
|