Skip to content

Commit

Permalink
http2: client conn pool abstraction
Browse files Browse the repository at this point in the history
Change-Id: Icbf40b26a25c7084efd062a0a66385450ec537aa
Reviewed-on: https://go-review.googlesource.com/16699
Reviewed-by: Blake Mizerany <blake.mizerany@gmail.com>
  • Loading branch information
bradfitz committed Nov 7, 2015
1 parent 633434a commit d62542d
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 138 deletions.
118 changes: 118 additions & 0 deletions http2/client_conn_pool.go
@@ -0,0 +1,118 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Transport code's client connection pooling.

package http2

import (
"net/http"
"sync"
)

// ClientConnPool manages a pool of HTTP/2 client connections.
type ClientConnPool interface {
GetClientConn(req *http.Request, addr string) (*ClientConn, error)
MarkDead(*ClientConn)
}

type clientConnPool struct {
t *Transport
mu sync.Mutex // TODO: switch to RWMutex
// TODO: add support for sharing conns based on cert names
// (e.g. share conn for googleapis.com and appspot.com)
conns map[string][]*ClientConn // key is host:port
keys map[*ClientConn][]string
}

func (p *clientConnPool) GetClientConn(req *http.Request, addr string) (*ClientConn, error) {
return p.getClientConn(req, addr, true)
}

func (p *clientConnPool) getClientConn(req *http.Request, addr string, dialOnMiss bool) (*ClientConn, error) {
p.mu.Lock()
for _, cc := range p.conns[addr] {
if cc.CanTakeNewRequest() {
p.mu.Unlock()
return cc, nil
}
}
p.mu.Unlock()
if !dialOnMiss {
return nil, ErrNoCachedConn
}

// TODO(bradfitz): use a singleflight.Group to only lock once per 'key'.
// Probably need to vendor it in as github.com/golang/sync/singleflight
// though, since the net package already uses it? Also lines up with
// sameer, bcmills, et al wanting to open source some sync stuff.
cc, err := p.t.dialClientConn(addr)
if err != nil {
return nil, err
}
p.addConn(addr, cc)
return cc, nil
}

func (p *clientConnPool) addConn(key string, cc *ClientConn) {
p.mu.Lock()
defer p.mu.Unlock()
if p.conns == nil {
p.conns = make(map[string][]*ClientConn)
}
if p.keys == nil {
p.keys = make(map[*ClientConn][]string)
}
p.conns[key] = append(p.conns[key], cc)
p.keys[cc] = append(p.keys[cc], key)
}

func (p *clientConnPool) MarkDead(cc *ClientConn) {
p.mu.Lock()
defer p.mu.Unlock()
for _, key := range p.keys[cc] {
vv, ok := p.conns[key]
if !ok {
continue
}
newList := filterOutClientConn(vv, cc)
if len(newList) > 0 {
p.conns[key] = newList
} else {
delete(p.conns, key)
}
}
delete(p.keys, cc)
}

func (p *clientConnPool) closeIdleConnections() {
p.mu.Lock()
defer p.mu.Unlock()
// TODO: don't close a cc if it was just added to the pool
// milliseconds ago and has never been used. There's currently
// a small race window with the HTTP/1 Transport's integration
// where it can add an idle conn just before using it, and
// somebody else can concurrently call CloseIdleConns and
// break some caller's RoundTrip.
for _, vv := range p.conns {
for _, cc := range vv {
cc.closeIfIdle()
}
}
}

func filterOutClientConn(in []*ClientConn, exclude *ClientConn) []*ClientConn {
out := in[:0]
for _, v := range in {
if v != exclude {
out = append(out, v)
}
}
// If we filtered it out, zero out the last item to prevent
// the GC from seeing it.
if len(in) != len(out) {
in[len(in)-1] = nil
}
return out
}

0 comments on commit d62542d

Please sign in to comment.