forked from asbubam/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (34 loc) · 819 Bytes
/
main.go
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
package main
import (
"fmt"
"sync"
"context"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/server"
)
// waitgroup is a handler wrapper which adds a handler to a sync.WaitGroup
func waitgroup(wg *sync.WaitGroup) server.HandlerWrapper {
return func(h server.HandlerFunc) server.HandlerFunc {
return func(ctx context.Context, req server.Request, rsp interface{}) error {
wg.Add(1)
defer wg.Done()
return h(ctx, req, rsp)
}
}
}
func main() {
var wg sync.WaitGroup
service := micro.NewService(
// wrap handlers with waitgroup wrapper
micro.WrapHandler(waitgroup(&wg)),
// waits for the waitgroup once stopped
micro.AfterStop(func() error {
// wait for handlers to finish
wg.Wait()
return nil
}),
)
if err := service.Run(); err != nil {
fmt.Println(err)
}
}