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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| package main
import ( "context" "fmt" "math/rand" "os" "os/signal" "sync" "syscall" "time" )
const ( TimeTemplate = "15:04:05.999999999" )
type Service interface { GetName() string Serve(ctx context.Context) Shutdown() error }
type BusinessService struct { }
func (b *BusinessService) GetName() string { return "BusinessService" }
func (b *BusinessService) Serve(ctx context.Context) { for { fmt.Printf("BusinessService serve run at %s\n", time.Now().Format(TimeTemplate)) select { case <-ctx.Done(): fmt.Printf("111111111111cancel %s\n", time.Now().Format(TimeTemplate)) return default: } time.Sleep(time.Second) } return }
func (b *BusinessService) Shutdown() error { fmt.Printf("BusinessService shutdown begin... at %s\n", time.Now().Format(TimeTemplate)) defer func() { fmt.Printf("BusinessService shutdown end... at %s\n", time.Now().Format(TimeTemplate)) }() return nil }
type LogService struct { buffer []string }
func (l *LogService) Serve(ctx context.Context) { for { select { case <-ctx.Done(): fmt.Printf("2222222222cancel %s\n", time.Now().Format(TimeTemplate)) return default: time.Sleep(500 * time.Millisecond) l.buffer = append(l.buffer, fmt.Sprintf("Time: %d", time.Now().Unix())) } } }
func (b *LogService) GetName() string { return "LogService" }
func (l *LogService) Shutdown() (err error) { fmt.Printf("LogService shutdown begin... at %s\n", time.Now().Format(TimeTemplate)) defer fmt.Printf("LogService shutdown end... at %s\n", time.Now().Format(TimeTemplate)) if len(l.buffer) == 0 { return } fmt.Printf("cache [%d] wait to send \n", len(l.buffer)) for _, log := range l.buffer { fmt.Printf("send Log [%s]\n", log) } return }
type ServiceGroup struct { ctx context.Context cancel func() services []Service }
func NewServiceGroup(ctx context.Context) *ServiceGroup { g := ServiceGroup{} g.ctx, g.cancel = context.WithCancel(ctx) return &g }
func (s *ServiceGroup) Add(service Service) { s.services = append(s.services, service) }
func (s *ServiceGroup) run(service Service) (err error) { defer func() { if r := recover(); r != nil { err = r.(error) fmt.Printf("receive panic msg: %s\n", err.Error()) } }() service.Serve(s.ctx) return }
func (s *ServiceGroup) watchDog() { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) for { select { case signalData := <-signalChan: switch signalData { case syscall.SIGINT: fmt.Println("receive signal sigint") case syscall.SIGTERM: fmt.Println("receive signal sigerm") default: fmt.Println("receive singal unknown") } s.cancel() goto CLOSE case <-s.ctx.Done(): goto CLOSE } } CLOSE: for _, service := range s.services { if err := service.Shutdown(); err != nil { fmt.Printf("shutdown failed err: %s", err) } } }
func (s *ServiceGroup) ServeAll() { var wg sync.WaitGroup for idx := range s.services { service := s.services[idx] wg.Add(1) go func() { defer wg.Done() if err := s.run(service); err != nil { fmt.Printf("receive service [%s] has error: 【%s】, do cancel\n", service.GetName(), err.Error()) s.cancel() } }() } wg.Add(1) go func() { defer wg.Done() s.watchDog() }() wg.Wait() }
func main() { rand.Seed(time.Now().Unix()) ctx := context.Background()
g := NewServiceGroup(ctx) g.Add(&LogService{}) g.Add(&BusinessService{}) g.ServeAll() }
|