Skip to content

Commit

Permalink
Merge pull request #40 from VincentYe123/fix-client-conn
Browse files Browse the repository at this point in the history
Fix excessive client connection creation
  • Loading branch information
sercand committed May 25, 2023
2 parents b9702cf + 64ab038 commit 838b0bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (b *kubeBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts
if err != nil && err != io.EOF {
grpclog.Errorf("kuberesolver: watching ended with error='%v', will reconnect again", err)
}
}, time.Second, ctx.Done())
}, time.Second, time.Second*30, ctx.Done())
return r, nil
}

Expand Down Expand Up @@ -278,7 +278,7 @@ func (k *kResolver) watch() error {
case <-k.t.C:
k.resolve()
case <-k.rn:
k.resolve()
//k.resolve()
case up, hasMore := <-sw.ResultChan():
if hasMore {
k.handle(up.Object)
Expand Down
8 changes: 4 additions & 4 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ func TestBuilder(t *testing.T) {
fc := &fakeConn{
cmp: make(chan struct{}),
}
rs, err := bl.Build(parseTarget("kubernetes://kube-dns.kube-system:53"), fc, resolver.BuildOptions{})
_, err = bl.Build(parseTarget("kubernetes://kube-dns.kube-system:53"), fc, resolver.BuildOptions{})
if err != nil {
t.Fatal(err)
}
<-fc.cmp
if len(fc.found) == 0 {
t.Fatal("could not found endpoints")
}
fmt.Printf("ResolveNow \n")
rs.ResolveNow(resolver.ResolveNowOptions{})
<-fc.cmp
// fmt.Printf("ResolveNow \n")
// rs.ResolveNow(resolver.ResolveNowOptions{})
// <-fc.cmp

}

Expand Down
8 changes: 7 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"google.golang.org/grpc/grpclog"
)

func until(f func(), period time.Duration, stopCh <-chan struct{}) {
func until(f func(), initialPeriod, maxPeriod time.Duration, stopCh <-chan struct{}) {
select {
case <-stopCh:
return
default:
}
period := initialPeriod
for {
func() {
defer handleCrash()
Expand All @@ -22,6 +23,11 @@ func until(f func(), period time.Duration, stopCh <-chan struct{}) {
case <-stopCh:
return
case <-time.After(period):
if period*2 <= maxPeriod {
period *= 2
} else {
period = initialPeriod
}
}
}
}
Expand Down

0 comments on commit 838b0bc

Please sign in to comment.