Skip to content

Commit edd1b37

Browse files
committed
x
1 parent 9a13c42 commit edd1b37

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

zrpc/internal/rpcpubserver.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,21 @@ type keepAliveServer struct {
5252
}
5353

5454
func (s keepAliveServer) Start(fn RegisterFn) error {
55-
chErr := make(chan error)
55+
errCh := make(chan error)
56+
stopCh := make(chan struct{})
57+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
58+
defer cancel()
59+
5660
go func() {
57-
defer close(chErr)
58-
chErr <- s.Server.Start(fn)
61+
defer close(errCh)
62+
select {
63+
case <-ctx.Done():
64+
case errCh <- s.Server.Start(fn):
65+
case <-stopCh:
66+
}
5967
}()
6068

6169
// Wait for the service to start successfully, otherwise the registration service will fail.
62-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
63-
defer cancel()
6470
ticker := time.NewTicker(time.Second)
6571

6672
l:
@@ -70,20 +76,21 @@ l:
7076
if health.IsReady() {
7177
err := s.registerEtcd()
7278
if err != nil {
79+
close(stopCh)
7380
return err
7481
}
7582
// break for loop
7683
break l
7784
}
7885
case <-ctx.Done():
7986
return errNotReady
80-
case err := <-chErr:
87+
case err := <-errCh:
8188
return err
8289
}
8390
}
8491
ticker.Stop()
8592

86-
return <-chErr
93+
return <-errCh
8794
}
8895

8996
func figureOutListenOn(listenOn string) string {

0 commit comments

Comments
 (0)