Skip to content

Commit

Permalink
MB-14986: Increase connection pool size and provide API to change it on
Browse files Browse the repository at this point in the history
the fly
CBSE-1762: Provide API to enable TCP Keepalive on memcached connections

Change-Id: I8d9ea0d347fc3da7f80631fddeff3d40b433e4dd
Reviewed-on: http://review.couchbase.org/51043
Reviewed-by: Manik Taneja <manik@couchbase.com>
Tested-by: Manik Taneja <manik@couchbase.com>
  • Loading branch information
maniktaneja authored and Manik Taneja committed May 14, 2015
1 parent d026522 commit e0dd367
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions conn_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ var ConnPoolCallback func(host string, source string, start time.Time, err error

func defaultMkConn(host string, ah AuthHandler) (*memcached.Client, error) {
conn, err := memcached.Connect("tcp", host)

if TCPKeepalive == true {
conn.SetKeepAliveOptions(time.Duration(TCPKeepaliveInterval) * time.Second)
}

if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion examples/streaming/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func main() {
return
}

couchbase.SetConnectionPoolParams(256, 16)
couchbase.SetTcpKeepalive(true, 30)

go performOp(cbbucket)

errCh := make(chan error)
Expand All @@ -58,13 +61,15 @@ func performOp(b *couchbase.Bucket) {
for {
key := fmt.Sprintf("k%d", i)
value := fmt.Sprintf("value%d", i)
log.Printf(" setting key %v", key)
err := b.Set(key, len(value), value)
if err != nil {
log.Printf("set failed error %v", err)
return
}
i++
<-time.After(1 * time.Second)

<-time.After(1 * time.Second)
}

}
32 changes: 30 additions & 2 deletions pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,39 @@ var HTTPTransport = &http.Transport{MaxIdleConnsPerHost: MaxIdleConnsPerHost}
var HTTPClient = &http.Client{Transport: HTTPTransport}

// PoolSize is the size of each connection pool (per host).
var PoolSize = 4
var PoolSize = 64

// PoolOverflow is the number of overflow connections allowed in a
// pool.
var PoolOverflow = PoolSize
var PoolOverflow = 16

// TCP KeepAlive enabled/disabled
var TCPKeepalive = false

// TCP keepalive interval in seconds. Default 30 minutes
var TCPKeepaliveInterval = 30 * 60

// Allow applications to speciify the Poolsize and Overflow
func SetConnectionPoolParams(size, overflow int) {

if size > 0 {
PoolSize = size
}

if overflow > 0 {
PoolOverflow = overflow
}
}

// Allow TCP keepalive parameters to be set by the application
func SetTcpKeepalive(enabled bool, interval int) {

TCPKeepalive = enabled

if interval > 0 {
TCPKeepaliveInterval = interval
}
}

// AuthHandler is a callback that gets the auth username and password
// for the given bucket.
Expand Down

0 comments on commit e0dd367

Please sign in to comment.