transport/grpc: add Num method to ConnPool
This is needed for Spanner, which needs to know the pool size to
determine a bunch of clever stuff to do with Spanner sessions.
See pending CL https://code-review.googlesource.com/c/gocloud/+/49874
Change-Id: I85c94cb22dc0df560f2122afad71ae683e67ee93
Reviewed-on: https://code-review.googlesource.com/c/google-api-go-client/+/50574
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hengfeng Li <hengfeng@google.com>
Reviewed-by: Tyler Bui-Palsulich <tbp@google.com>
Reviewed-by: Knut Olav Løite <koloite@gmail.com>
diff --git a/transport/grpc/pool.go b/transport/grpc/pool.go
index 3555ae5..8802d06 100644
--- a/transport/grpc/pool.go
+++ b/transport/grpc/pool.go
@@ -18,7 +18,12 @@
// Conns aren't returned to the pool.
Conn() *grpc.ClientConn
- // Close closes every ClientConn in the group.
+ // Num returns the number of connections in the pool.
+ //
+ // It will always return the same value.
+ Num() int
+
+ // Close closes every ClientConn in the pool.
//
// The error returned by Close may be a single error or multiple errors.
Close() error
@@ -36,6 +41,10 @@
return p.conn
}
+func (p *singleConnPool) Num() int {
+ return 1
+}
+
func (p *singleConnPool) Close() error {
return p.conn.Close()
}
@@ -46,6 +55,10 @@
idx uint32 // access via sync/atomic
}
+func (p *roundRobinConnPool) Num() int {
+ return len(p.conns)
+}
+
func (p *roundRobinConnPool) Conn() *grpc.ClientConn {
i := atomic.AddUint32(&p.idx, 1)
return p.conns[i%uint32(len(p.conns))]