Skip to content

Commit 6f86983

Browse files
authored
feat(bigtable): add ip protocol for bigtable connection (#13520)
we will need this for populating direct_access/compatible as it has an `ip_preference` metric field.
1 parent e59df51 commit 6f86983

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

bigtable/internal/transport/connpool.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"log"
2323
"math"
2424
"math/rand"
25+
"net"
2526
"net/url"
2627
"slices"
2728
"sort"
@@ -47,6 +48,30 @@ var maxDrainingTimeout = 30 * time.Minute
4748

4849
const requestParamsHeader = "x-goog-request-params"
4950

51+
// ipProtocol represents the type of IP protocol used.
52+
type ipProtocol int32
53+
54+
const (
55+
// unknown represents an unknown or undetermined IP protocol.
56+
unknown ipProtocol = iota - 1
57+
// ipv6 represents the IPv4 protocol.
58+
ipv4
59+
// ipv6 represents the IPv6 protocol.
60+
ipv6
61+
)
62+
63+
// AddressType returns the string representation of the IPProtocol.
64+
func (ip ipProtocol) addressType() string {
65+
switch ip {
66+
case ipv4:
67+
return "ipv4"
68+
case ipv6:
69+
return "ipv6"
70+
default:
71+
return "unknown"
72+
}
73+
}
74+
5075
// BigtableChannelPoolOption options for configurable
5176
type BigtableChannelPoolOption func(*BigtableChannelPool)
5277

@@ -111,6 +136,13 @@ type BigtableConn struct {
111136
*grpc.ClientConn
112137
isALTSConn atomic.Bool
113138
createdAt atomic.Int64
139+
// remoteAddrType stores the type: -1 (unknown/nil), 0 (ipv4), 1 (ipv6)
140+
remoteAddrType atomic.Int32
141+
}
142+
143+
// ipProtocol returns the IP protocol as a string: "ipv4", "ipv6", or "unknown".
144+
func (bc *BigtableConn) ipProtocol() string {
145+
return ipProtocol(bc.remoteAddrType.Load()).addressType()
114146
}
115147

116148
// Prime sends a PingAndWarm request to warm up the connection.
@@ -137,6 +169,19 @@ func (bc *BigtableConn) Prime(ctx context.Context, fullInstanceName, appProfileI
137169
return err
138170
}
139171

172+
// ip protocol will be -1 if it addr is nil/default, 0 is ipv4 and 1 if ipv6.
173+
if p.Addr != nil {
174+
if tcpAddr, ok := p.Addr.(*net.TCPAddr); ok {
175+
if tcpAddr.IP != nil {
176+
if tcpAddr.IP.To4() != nil {
177+
bc.remoteAddrType.Store(int32(ipv4))
178+
} else {
179+
bc.remoteAddrType.Store(int32(ipv6))
180+
}
181+
}
182+
}
183+
}
184+
140185
if p.AuthInfo != nil {
141186
if _, ok := p.AuthInfo.(alts.AuthInfo); ok {
142187
bc.isALTSConn.Store(true)
@@ -173,8 +218,8 @@ func NewBigtableConn(conn *grpc.ClientConn) *BigtableConn {
173218
ClientConn: conn,
174219
}
175220
bc.createdAt.Store(time.Now().UnixMilli())
221+
bc.remoteAddrType.Store(int32(unknown))
176222
return bc
177-
178223
}
179224

180225
// createdAt returns the creation time of the connection in int64. milliseconds since epoch

bigtable/internal/transport/connpool_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ func entryIndex(s []*connEntry, e *connEntry) int {
6363
return -1
6464
}
6565

66+
func TestBigtableConnIpProtocol(t *testing.T) {
67+
bc := NewBigtableConn(nil)
68+
if got := bc.ipProtocol(); got != "unknown" {
69+
t.Errorf("NewBigtableConn default ipProtocol() got %q, want %q", got, unknown)
70+
}
71+
72+
tests := []struct {
73+
name string
74+
addrType ipProtocol
75+
want string
76+
}{
77+
{name: "IPv4", addrType: ipv4, want: "ipv4"},
78+
{name: "IPv6", addrType: ipv6, want: "ipv6"},
79+
{name: "Unknown", addrType: unknown, want: "unknown"},
80+
}
81+
82+
for _, tc := range tests {
83+
t.Run(tc.name, func(t *testing.T) {
84+
conn := &BigtableConn{}
85+
conn.remoteAddrType.Store(int32(tc.addrType))
86+
if got := conn.ipProtocol(); got != tc.want {
87+
t.Errorf("ipProtocol() with remoteAddrType %d got %q, want %q", tc.addrType, got, tc.want)
88+
}
89+
})
90+
}
91+
}
92+
6693
func TestSelectRoundRobin(t *testing.T) {
6794
pool := &BigtableChannelPool{rrIndex: 0}
6895

0 commit comments

Comments
 (0)