Skip to content

Commit

Permalink
new
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed May 11, 2020
1 parent ccfd454 commit 2f1b2fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
17 changes: 14 additions & 3 deletions micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Service interface {
String() string
// stop
Stop() error

// stop signal
NotifyStop()
}

type service struct {
Expand All @@ -47,6 +50,8 @@ type service struct {
BeforeStop []func() error
AfterStart []func() error
AfterStop []func() error

cc chan os.Signal
}

// Option ...
Expand Down Expand Up @@ -149,12 +154,12 @@ func (s *service) Run() error {
return err
}

ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
s.cc = make(chan os.Signal, 1)
signal.Notify(s.cc, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)

select {
// wait on kill signal
case <-ch:
case <-s.cc:
// wait on context cancel
case <-s.opts.Context.Done():
}
Expand Down Expand Up @@ -190,8 +195,14 @@ func (s *service) Start() error {
return nil
}

// NotifyStop 发送停止信号
func (s *service) NotifyStop() {
s.cc <- syscall.SIGINT
}

// Stop stops the default server
func (s *service) Stop() error {

var gerr error

// for _, fn := range s.BeforeStop {
Expand Down
4 changes: 4 additions & 0 deletions server/naming_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@ func (s *namingResolver) GetListener() net.Listener {
s.Unlock()
return s.opts.Listener
}

func (s *namingResolver) SetAddress(add string) {
s.opts.Address = add
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Server interface {
String() string
GetServer() *grpc.Server
GetListener() net.Listener
SetAddress(add string)
}

// Option option list
Expand Down

0 comments on commit 2f1b2fe

Please sign in to comment.