client_conn_pool.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Transport code's client connection pooling.
  5. package http2
  6. import (
  7. "net/http"
  8. "sync"
  9. )
  10. // ClientConnPool manages a pool of HTTP/2 client connections.
  11. type ClientConnPool interface {
  12. GetClientConn(req *http.Request, addr string) (*ClientConn, error)
  13. MarkDead(*ClientConn)
  14. }
  15. type clientConnPool struct {
  16. t *Transport
  17. mu sync.Mutex // TODO: switch to RWMutex
  18. // TODO: add support for sharing conns based on cert names
  19. // (e.g. share conn for googleapis.com and appspot.com)
  20. conns map[string][]*ClientConn // key is host:port
  21. keys map[*ClientConn][]string
  22. }
  23. func (p *clientConnPool) GetClientConn(req *http.Request, addr string) (*ClientConn, error) {
  24. return p.getClientConn(req, addr, true)
  25. }
  26. func (p *clientConnPool) getClientConn(req *http.Request, addr string, dialOnMiss bool) (*ClientConn, error) {
  27. p.mu.Lock()
  28. for _, cc := range p.conns[addr] {
  29. if cc.CanTakeNewRequest() {
  30. p.mu.Unlock()
  31. return cc, nil
  32. }
  33. }
  34. p.mu.Unlock()
  35. if !dialOnMiss {
  36. return nil, ErrNoCachedConn
  37. }
  38. // TODO(bradfitz): use a singleflight.Group to only lock once per 'key'.
  39. // Probably need to vendor it in as github.com/golang/sync/singleflight
  40. // though, since the net package already uses it? Also lines up with
  41. // sameer, bcmills, et al wanting to open source some sync stuff.
  42. cc, err := p.t.dialClientConn(addr)
  43. if err != nil {
  44. return nil, err
  45. }
  46. p.addConn(addr, cc)
  47. return cc, nil
  48. }
  49. func (p *clientConnPool) addConn(key string, cc *ClientConn) {
  50. p.mu.Lock()
  51. defer p.mu.Unlock()
  52. if p.conns == nil {
  53. p.conns = make(map[string][]*ClientConn)
  54. }
  55. if p.keys == nil {
  56. p.keys = make(map[*ClientConn][]string)
  57. }
  58. p.conns[key] = append(p.conns[key], cc)
  59. p.keys[cc] = append(p.keys[cc], key)
  60. }
  61. func (p *clientConnPool) MarkDead(cc *ClientConn) {
  62. p.mu.Lock()
  63. defer p.mu.Unlock()
  64. for _, key := range p.keys[cc] {
  65. vv, ok := p.conns[key]
  66. if !ok {
  67. continue
  68. }
  69. newList := filterOutClientConn(vv, cc)
  70. if len(newList) > 0 {
  71. p.conns[key] = newList
  72. } else {
  73. delete(p.conns, key)
  74. }
  75. }
  76. delete(p.keys, cc)
  77. }
  78. func (p *clientConnPool) closeIdleConnections() {
  79. p.mu.Lock()
  80. defer p.mu.Unlock()
  81. // TODO: don't close a cc if it was just added to the pool
  82. // milliseconds ago and has never been used. There's currently
  83. // a small race window with the HTTP/1 Transport's integration
  84. // where it can add an idle conn just before using it, and
  85. // somebody else can concurrently call CloseIdleConns and
  86. // break some caller's RoundTrip.
  87. for _, vv := range p.conns {
  88. for _, cc := range vv {
  89. cc.closeIfIdle()
  90. }
  91. }
  92. }
  93. func filterOutClientConn(in []*ClientConn, exclude *ClientConn) []*ClientConn {
  94. out := in[:0]
  95. for _, v := range in {
  96. if v != exclude {
  97. out = append(out, v)
  98. }
  99. }
  100. // If we filtered it out, zero out the last item to prevent
  101. // the GC from seeing it.
  102. if len(in) != len(out) {
  103. in[len(in)-1] = nil
  104. }
  105. return out
  106. }