Skip to content
Permalink
Browse files

move graceful to waitgroup, add graceful as server.Wait

  • Loading branch information...
asim committed Jul 20, 2017
1 parent 616f54b commit 0f5983a7982a58f94cc13f70edd99b41f214e123
Showing with 50 additions and 25 deletions.
  1. +1 −0 README.md
  2. +2 −2 graceful/README.md
  3. +3 −23 graceful/main.go
  4. +4 −0 waitgroup/README.md
  5. +40 −0 waitgroup/main.go
@@ -24,6 +24,7 @@ This is a repository for micro examples. Feel free to contribute.
- [sidecar](sidecar) - Greeter service using the sidecar with multiple languages
- [stream](stream) - An example of a streaming service and client
- [template](template) - Api, web and srv service templates generated with the 'micro new' command
- [waitgroup](waitgroup) - Demonstrates how to use a waitgroup with a service

## External

@@ -1,6 +1,6 @@
# Graceful

Graceful demonstrates graceful shutdown of a service using sync.WaitGroup.
Graceful demonstrates graceful shutdown of a service using the server.Wait option

It adds each handler executed to sync.WaitGroup which can be waited on before exiting.
The server deregisters the service and waits for handlers to finish executing before exiting.

@@ -2,36 +2,16 @@ package main

import (
"fmt"
"sync"

"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
"golang.org/x/net/context"
)

// graceful is a handler wrapper which adds a handler to a sync.WaitGroup
func graceful(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()

service := micro.NewService(
// wrap handlers with graceful wrapper
micro.WrapHandler(graceful(&wg)),
// handler gracefully once stopped
micro.AfterStop(func() error {
// wait for handlers to finish
wg.Wait()
return nil
}),
service.Server().Init(
server.Wait(true),
)

if err := service.Run(); err != nil {
@@ -0,0 +1,4 @@
# Waitgroup

This demonstrates how to use sync.WaitGroup with a service.

@@ -0,0 +1,40 @@
package main

import (
"fmt"
"sync"

"github.com/micro/go-micro"
"github.com/micro/go-micro/server"
"golang.org/x/net/context"
)

// 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)
}
}

0 comments on commit 0f5983a

Please sign in to comment.
You can’t perform that action at this time.