Make server starts and stops idempotent
This commit is contained in:
parent
4a4159a9f6
commit
65733b8268
35
grpc.go
35
grpc.go
@ -53,6 +53,8 @@ type grpcServer struct {
|
||||
opts server.Options
|
||||
handlers map[string]server.Handler
|
||||
subscribers map[*subscriber][]broker.Subscriber
|
||||
// marks the serve as started
|
||||
started bool
|
||||
// used for first registration
|
||||
registered bool
|
||||
}
|
||||
@ -454,7 +456,10 @@ func (g *grpcServer) newCodec(contentType string) (codec.NewCodec, error) {
|
||||
}
|
||||
|
||||
func (g *grpcServer) Options() server.Options {
|
||||
g.RLock()
|
||||
opts := g.opts
|
||||
g.RUnlock()
|
||||
|
||||
return opts
|
||||
}
|
||||
|
||||
@ -700,7 +705,14 @@ func (g *grpcServer) Deregister() error {
|
||||
}
|
||||
|
||||
func (g *grpcServer) Start() error {
|
||||
config := g.opts
|
||||
g.RLock()
|
||||
if g.started {
|
||||
g.RUnlock()
|
||||
return nil
|
||||
}
|
||||
g.RUnlock()
|
||||
|
||||
config := g.Options()
|
||||
|
||||
// micro: config.Transport.Listen(config.Address)
|
||||
ts, err := net.Listen("tcp", config.Address)
|
||||
@ -781,13 +793,32 @@ func (g *grpcServer) Start() error {
|
||||
config.Broker.Disconnect()
|
||||
}()
|
||||
|
||||
// mark the server as started
|
||||
g.Lock()
|
||||
g.started = true
|
||||
g.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *grpcServer) Stop() error {
|
||||
g.RLock()
|
||||
if !g.started {
|
||||
g.RUnlock()
|
||||
return nil
|
||||
}
|
||||
g.RUnlock()
|
||||
|
||||
ch := make(chan error)
|
||||
g.exit <- ch
|
||||
return <-ch
|
||||
|
||||
var err error
|
||||
select {
|
||||
case err = <-ch:
|
||||
g.started = false
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (g *grpcServer) String() string {
|
||||
|
Loading…
x
Reference in New Issue
Block a user