From 2fbcee7b59bf98514c2232d22e24f5bb039e2115 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 14 Feb 2024 19:14:56 +0300 Subject: [PATCH] export grpc pool conn Signed-off-by: Vasiliy Tolstov --- grpc_pool.go | 24 ++++++++++++------------ options.go | 2 +- response.go | 2 +- stream.go | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/grpc_pool.go b/grpc_pool.go index c31deaf..f602710 100644 --- a/grpc_pool.go +++ b/grpc_pool.go @@ -21,22 +21,22 @@ type ConnPool struct { type streamsPool struct { // head of list - head *poolConn + head *PoolConn // busy conns list - busy *poolConn + busy *PoolConn // the siza of list count int // idle conn idle int } -type poolConn struct { +type PoolConn struct { err error *grpc.ClientConn - next *poolConn + next *PoolConn pool *ConnPool sp *streamsPool - pre *poolConn + pre *PoolConn addr string streams int created int64 @@ -59,7 +59,7 @@ func NewConnPool(size int, ttl time.Duration, idle int, ms int) *ConnPool { } } -func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption) (*poolConn, error) { +func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption) (*PoolConn, error) { if strings.HasPrefix(addr, "http") { addr = addr[strings.Index(addr, ":")+3:] } @@ -67,7 +67,7 @@ func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption p.Lock() sp, ok := p.conns[addr] if !ok { - sp = &streamsPool{head: &poolConn{}, busy: &poolConn{}, count: 0, idle: 0} + sp = &streamsPool{head: &PoolConn{}, busy: &PoolConn{}, count: 0, idle: 0} p.conns[addr] = sp } // while we have conns check streams and then return one @@ -135,7 +135,7 @@ func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption if err != nil { return nil, err } - conn = &poolConn{ClientConn: cc, err: nil, addr: addr, pool: p, sp: sp, streams: 1, created: time.Now().Unix(), pre: nil, next: nil, in: false} + conn = &PoolConn{ClientConn: cc, err: nil, addr: addr, pool: p, sp: sp, streams: 1, created: time.Now().Unix(), pre: nil, next: nil, in: false} // add conn to streams pool p.Lock() @@ -147,7 +147,7 @@ func (p *ConnPool) Get(ctx context.Context, addr string, opts ...grpc.DialOption return conn, nil } -func (p *ConnPool) Put(conn *poolConn, err error) { +func (p *ConnPool) Put(conn *PoolConn, err error) { p.Lock() p, sp, created := conn.pool, conn.sp, conn.created // try to add conn @@ -182,11 +182,11 @@ func (p *ConnPool) Put(conn *poolConn, err error) { p.Unlock() } -func (conn *poolConn) Close() { +func (conn *PoolConn) Close() { conn.pool.Put(conn, conn.err) } -func removeConn(conn *poolConn) { +func removeConn(conn *PoolConn) { if conn.pre != nil { conn.pre.next = conn.next } @@ -199,7 +199,7 @@ func removeConn(conn *poolConn) { conn.sp.count-- } -func addConnAfter(conn *poolConn, after *poolConn) { +func addConnAfter(conn *PoolConn, after *PoolConn) { conn.next = after.next conn.pre = after if after.next != nil { diff --git a/options.go b/options.go index 3e41ebd..0404438 100644 --- a/options.go +++ b/options.go @@ -10,7 +10,7 @@ import ( ) var ( - // DefaultPoolMaxStreams maximum streams on a connectioin + // DefaultPoolMaxStreams maximum streams on a connection // (20) DefaultPoolMaxStreams = 20 diff --git a/response.go b/response.go index 5fbda87..e0b7b27 100644 --- a/response.go +++ b/response.go @@ -9,7 +9,7 @@ import ( ) type response struct { - conn *poolConn + conn *PoolConn stream grpc.ClientStream codec codec.Codec } diff --git a/stream.go b/stream.go index 322e397..8108e17 100644 --- a/stream.go +++ b/stream.go @@ -17,7 +17,7 @@ type grpcStream struct { request client.Request response client.Response close func(err error) - conn *poolConn + conn *PoolConn sync.RWMutex closed bool }