@@ -66,21 +66,19 @@ func (p *pool) Get(ctx context.Context, endpoint endpoint.Endpoint) Conn {
6666 return cc
6767 }
6868
69- cc = newConn (
70- endpoint ,
71- p .config ,
72- withOnClose (func (c * conn ) {
73- p .mtx .Lock ()
74- defer p .mtx .Unlock ()
75- delete (p .conns , c .Endpoint ().Address ())
76- }),
77- )
69+ cc = newConn (endpoint , p .config , withOnClose (p .remove ))
7870
7971 p .conns [address ] = cc
8072
8173 return cc
8274}
8375
76+ func (p * pool ) remove (c * conn ) {
77+ p .mtx .Lock ()
78+ defer p .mtx .Unlock ()
79+ delete (p .conns , c .Endpoint ().Address ())
80+ }
81+
8482func (p * pool ) Pessimize (ctx context.Context , cc Conn , cause error ) {
8583 e := cc .Endpoint ().Copy ()
8684
@@ -101,7 +99,7 @@ func (p *pool) Pessimize(ctx context.Context, cc Conn, cause error) {
10199 )(cc .SetState (Banned ))
102100}
103101
104- func (p * pool ) Take (ctx context.Context ) error {
102+ func (p * pool ) Take (context.Context ) error {
105103 atomic .AddInt64 (& p .usages , 1 )
106104 return nil
107105}
@@ -134,21 +132,14 @@ func (p *pool) Release(ctx context.Context) error {
134132 return nil
135133}
136134
137- func (p * pool ) connParker (ctx context.Context , interval time.Duration ) {
135+ func (p * pool ) connParker (ctx context.Context , ttl , interval time.Duration ) {
138136 ticker := time .NewTicker (interval )
139- ttl := p .config .ConnectionTTL ()
140137 for {
141138 select {
142139 case <- p .done :
143140 return
144141 case <- ticker .C :
145- p .mtx .RLock ()
146- conns := make ([]* conn , 0 , len (p .conns ))
147- for _ , c := range p .conns {
148- conns = append (conns , c )
149- }
150- p .mtx .RUnlock ()
151- for _ , c := range conns {
142+ for _ , c := range p .collectConns () {
152143 if time .Since (c .LastUsage ()) > ttl {
153144 _ = c .park (ctx )
154145 }
@@ -157,6 +148,16 @@ func (p *pool) connParker(ctx context.Context, interval time.Duration) {
157148 }
158149}
159150
151+ func (p * pool ) collectConns () []* conn {
152+ p .mtx .RLock ()
153+ defer p .mtx .RUnlock ()
154+ conns := make ([]* conn , 0 , len (p .conns ))
155+ for _ , c := range p .conns {
156+ conns = append (conns , c )
157+ }
158+ return conns
159+ }
160+
160161func NewPool (
161162 ctx context.Context ,
162163 config Config ,
@@ -169,7 +170,7 @@ func NewPool(
169170 done : make (chan struct {}),
170171 }
171172 if ttl := config .ConnectionTTL (); ttl > 0 {
172- go p .connParker (ctx , ttl / 2 )
173+ go p .connParker (ctx , ttl , ttl / 2 )
173174 }
174175 return p
175176}
0 commit comments