mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-24 11:56:26 +00:00
fix graceful stop
This commit is contained in:
parent
ac9a81d3e7
commit
af25e09154
@ -1,6 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@ -29,6 +30,7 @@ func New(opts ...Option) (Service, error) {
|
|||||||
type service struct {
|
type service struct {
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
opts *options
|
opts *options
|
||||||
|
cancel context.CancelFunc
|
||||||
server *grpc.Server
|
server *grpc.Server
|
||||||
list net.Listener
|
list net.Listener
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
@ -49,6 +51,7 @@ func newService(opts ...Option) (*service, error) {
|
|||||||
if s.opts.error != nil {
|
if s.opts.error != nil {
|
||||||
return nil, s.opts.error
|
return nil, s.opts.error
|
||||||
}
|
}
|
||||||
|
s.opts.ctx, s.cancel = context.WithCancel(s.opts.ctx)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -117,7 +120,7 @@ func (s *service) run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
return <- errs
|
return <-errs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Start() error {
|
func (s *service) Start() error {
|
||||||
@ -136,6 +139,7 @@ func (s *service) Stop() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.server.GracefulStop()
|
s.server.GracefulStop()
|
||||||
|
s.cancel()
|
||||||
s.running = false
|
s.running = false
|
||||||
for i := range s.opts.afterStop {
|
for i := range s.opts.afterStop {
|
||||||
if err := s.opts.afterStop[i](); err != nil {
|
if err := s.opts.afterStop[i](); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user