Source File
grpclb_util.go
Belonging Package
google.golang.org/grpc/balancer/grpclb
package grpclb
import (
)
type lbManualResolver struct {
scheme string
ccr resolver.ClientConn
ccb balancer.ClientConn
}
func ( *lbManualResolver) ( resolver.Target, resolver.ClientConn, resolver.BuildOptions) (resolver.Resolver, error) {
.ccr =
return , nil
}
func ( *lbManualResolver) () string {
return .scheme
}
func ( *lbManualResolver) ( resolver.ResolveNowOptions) {
.ccb.ResolveNow()
}
func (*lbManualResolver) () {}
func ( *lbManualResolver) ( resolver.State) {
.ccr.UpdateState()
}
const subConnCacheTime = time.Second * 10
type lbCacheClientConn struct {
cc balancer.ClientConn
timeout time.Duration
subConnCache map[resolver.Address]*subConnCacheEntry
subConnToAddr map[balancer.SubConn]resolver.Address
}
type subConnCacheEntry struct {
sc balancer.SubConn
cancel func()
abortDeleting bool
}
func ( balancer.ClientConn) *lbCacheClientConn {
return &lbCacheClientConn{
cc: ,
timeout: subConnCacheTime,
subConnCache: make(map[resolver.Address]*subConnCacheEntry),
subConnToAddr: make(map[balancer.SubConn]resolver.Address),
}
}
func ( *lbCacheClientConn) ( []resolver.Address, balancer.NewSubConnOptions) (balancer.SubConn, error) {
if len() != 1 {
return nil, fmt.Errorf("grpclb calling NewSubConn with addrs of length %v", len())
}
:= [0]
.Metadata = nil
.mu.Lock()
defer .mu.Unlock()
.cancel()
delete(.subConnCache, )
return .sc, nil
}
, := .cc.NewSubConn(, )
if != nil {
return nil,
}
.subConnToAddr[] =
return , nil
}
func ( *lbCacheClientConn) ( balancer.SubConn) {
.mu.Lock()
defer .mu.Unlock()
, := .subConnToAddr[]
if ! {
return
}
if , := .subConnCache[]; {
delete(.subConnToAddr, )
.cc.RemoveSubConn()
}
return
}
:= &subConnCacheEntry{
sc: ,
}
.subConnCache[] =
:= time.AfterFunc(.timeout, func() {
.mu.Lock()
defer .mu.Unlock()
if .abortDeleting {
return
}
.cc.RemoveSubConn()
delete(.subConnToAddr, )
delete(.subConnCache, )
})
.cancel = func() {
.abortDeleting = true
}
}
}
func ( *lbCacheClientConn) ( balancer.State) {
.cc.UpdateState()
}
func ( *lbCacheClientConn) () {
for , := range .subConnCache {
.cancel()
}
.mu.Unlock()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |