Skip to content

Commit a4e19de

Browse files
committed
x
1 parent d632ecf commit a4e19de

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

zrpc/internal/rpcpubserver.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/zeromicro/go-zero/core/discov"
11-
"github.com/zeromicro/go-zero/core/logx"
1211
"github.com/zeromicro/go-zero/core/netx"
1312
"github.com/zeromicro/go-zero/internal/health"
1413
)
@@ -53,29 +52,34 @@ type keepAliveServer struct {
5352
}
5453

5554
func (s keepAliveServer) Start(fn RegisterFn) error {
55+
chErr := make(chan error)
56+
defer close(chErr)
57+
5658
go func() {
57-
// Wait for the service to start successfully, otherwise the registration service will fail.
58-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
59-
defer cancel()
60-
61-
ticker := time.NewTicker(time.Second / 2)
62-
defer ticker.Stop()
63-
64-
for {
65-
select {
66-
case <-ticker.C:
67-
if health.IsReady() {
68-
logx.Must(s.registerEtcd())
69-
return
59+
chErr <- s.Server.Start(fn)
60+
}()
61+
62+
// Wait for the service to start successfully, otherwise the registration service will fail.
63+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
64+
defer cancel()
65+
66+
ticker := time.NewTicker(time.Second)
67+
defer ticker.Stop()
68+
69+
for {
70+
select {
71+
case <-ticker.C:
72+
if health.IsReady() {
73+
err := s.registerEtcd()
74+
if err != nil {
75+
return err
7076
}
71-
case <-ctx.Done():
72-
logx.Must(errNotReady)
73-
return
77+
return <-chErr
7478
}
79+
case <-ctx.Done():
80+
return errNotReady
7581
}
76-
}()
77-
78-
return s.Server.Start(fn)
82+
}
7983
}
8084

8185
func figureOutListenOn(listenOn string) string {

zrpc/internal/rpcpubserver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestNewRpcPubServer(t *testing.T) {
1919
}, "")
2020
assert.NoError(t, err)
2121
health.MarkReady()
22-
assert.Panics(t, func() {
22+
assert.NotPanics(t, func() {
2323
s.Start(func(server *grpc.Server) {
2424
})
2525
})

zrpc/internal/serverinterceptors/shutdowninterceptor.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ func UnaryShutdownInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerI
4343
}
4444

4545
func handleShutdown(f func() error) error {
46+
//
47+
// if health.IsReady() {
48+
// return errShutdown
49+
// }
50+
4651
if atomic.LoadInt32(&shutdown) == 1 {
4752
return errShutdown
4853
}

0 commit comments

Comments
 (0)