1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package main
import ( "fmt" "os" "os/signal" "sync" "syscall" "time" )
type Service struct { ch chan bool waitGroup *sync.WaitGroup }
func NewService() *Service { s := &Service{ ch: make(chan bool), waitGroup: &sync.WaitGroup{}, }
return s }
func (s *Service) Stop() { close(s.ch) s.waitGroup.Wait() fmt.Println("彻底over,彻底退出") }
func (s *Service) Serve() { fmt.Println("Server start") s.waitGroup.Add(1) defer s.waitGroup.Done() index := 1 for { select { case <-s.ch: fmt.Println("Server stopping...") return default: time.Sleep(time.Second) s.waitGroup.Add(1) go s.anotherServer(index) index++ } } }
func (s *Service) anotherServer(index int) { fmt.Println("anotherServer start", index) defer s.waitGroup.Done() for { select { case <-s.ch: fmt.Println("anotherServer stopping...", index) return default: } } }
func main() { service := NewService() go service.Serve()
ch := make(chan os.Signal) signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM) fmt.Println("收到退出了到信号", <-ch)
service.Stop() }
|