@@ -2,8 +2,10 @@ package conn
22
33import (
44 "context"
5+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
56 "sync"
67 "sync/atomic"
8+ "time"
79
810 "google.golang.org/grpc"
911 grpcCodes "google.golang.org/grpc/codes"
@@ -194,6 +196,39 @@ func (p *Pool) Release(ctx context.Context) (finalErr error) {
194196 return nil
195197}
196198
199+ func (p * Pool ) connParker (ctx context.Context , ttl , interval time.Duration ) {
200+ ticker := time .NewTicker (interval )
201+ defer ticker .Stop ()
202+ for {
203+ select {
204+ case <- p .done :
205+ return
206+ case <- ticker .C :
207+ for _ , c := range p .collectConns () {
208+ if time .Since (c .LastUsage ()) > ttl {
209+ switch c .GetState () {
210+ case Online , Banned :
211+ _ = c .park (ctx )
212+ default :
213+ // nop
214+ }
215+ }
216+ }
217+ }
218+ }
219+ }
220+
221+ func (p * Pool ) collectConns () []* conn {
222+ p .mtx .RLock ()
223+ defer p .mtx .RUnlock ()
224+ conns := make ([]* conn , 0 , len (p .conns ))
225+ for _ , c := range p .conns {
226+ conns = append (conns , c )
227+ }
228+
229+ return conns
230+ }
231+
197232func NewPool (ctx context.Context , config Config ) * Pool {
198233 onDone := trace .DriverOnPoolNew (config .Trace (), & ctx , stack .FunctionID ("" ))
199234 defer onDone ()
@@ -206,5 +241,9 @@ func NewPool(ctx context.Context, config Config) *Pool {
206241 done : make (chan struct {}),
207242 }
208243
244+ if ttl := config .ConnectionTTL (); ttl > 0 {
245+ go p .connParker (xcontext .ValueOnly (ctx ), ttl , ttl / 2 )
246+ }
247+
209248 return p
210249}
0 commit comments