Skip to content

Commit 2c227c4

Browse files
author
mobus
committed
feat: grace support custom handler
1 parent b82873d commit 2c227c4

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

examples/netepoll/main.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
_ "net/http/pprof"
7+
"os"
8+
"syscall"
9+
"time"
10+
11+
"github.com/rcrowley/go-metrics"
12+
"github.com/sunvim/utils/netpoll"
13+
)
14+
15+
func main() {
16+
setLimit()
17+
go metrics.Log(metrics.DefaultRegistry, 5*time.Second, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
18+
19+
go func() {
20+
if err := http.ListenAndServe(":6060", nil); err != nil {
21+
log.Fatalf("pprof failed: %v", err)
22+
}
23+
}()
24+
25+
var handler = &netpoll.DataHandler{
26+
NoShared: true,
27+
NoCopy: true,
28+
BufferSize: 1024,
29+
HandlerFunc: func(req []byte) (res []byte) {
30+
res = req
31+
return
32+
},
33+
}
34+
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
35+
panic(err)
36+
}
37+
}
38+
39+
func setLimit() {
40+
var rLimit syscall.Rlimit
41+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
42+
panic(err)
43+
}
44+
rLimit.Cur = rLimit.Max
45+
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
46+
panic(err)
47+
}
48+
49+
log.Printf("set cur limit: %d", rLimit.Cur)
50+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/hslam/scheduler v0.0.0-20211028175315-641598104976
88
github.com/hslam/sendfile v1.0.1
99
github.com/hslam/splice v1.0.3
10+
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
1011
github.com/stretchr/testify v1.7.0
1112
gopkg.in/eapache/queue.v1 v1.1.0
1213
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ github.com/hslam/splice v1.0.3 h1:CwSmzu6AAm8sb2wYgSGvTwixy3seqA6xJ9NXQ0ff3j4=
1212
github.com/hslam/splice v1.0.3/go.mod h1:7D1QlFptoG0ruXzcAwpzckKxUN4+ZpvrIhwfbcAQcx8=
1313
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1414
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15+
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
16+
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
1517
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1618
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
1719
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

grace/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import (
1212
type serv struct {
1313
stop chan os.Signal
1414
cancel func()
15+
funcs []func() error
1516
}
1617

1718
type Service interface {
19+
Register(fn func() error)
1820
Wait()
1921
}
2022

@@ -23,11 +25,20 @@ func (s *serv) Wait() {
2325
select {
2426
case <-s.stop:
2527
s.cancel()
28+
for _, fn := range s.funcs {
29+
if err := fn(); err != nil {
30+
log.Printf("err: %v \n", err)
31+
}
32+
}
2633
time.Sleep(100 * time.Millisecond)
2734
log.Println("all services exited totally.")
2835
}
2936
}
3037

38+
func (s *serv) Register(fn func() error) {
39+
s.funcs = append(s.funcs, fn)
40+
}
41+
3142
func New(ctx context.Context) (context.Context, Service) {
3243
stopChan := make(chan os.Signal)
3344
signal.Notify(stopChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

grace/service_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package grace
2+
3+
import (
4+
"context"
5+
"log"
6+
"syscall"
7+
"testing"
8+
"time"
9+
)
10+
11+
func TestWait(t *testing.T) {
12+
_, service := New(context.Background())
13+
service.Register(func() error {
14+
log.Println("exit 1")
15+
return nil
16+
})
17+
service.Register(func() error {
18+
log.Println("exit 2")
19+
return nil
20+
})
21+
go func() {
22+
time.Sleep(10 * time.Second)
23+
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
24+
}()
25+
service.Wait()
26+
}

0 commit comments

Comments
 (0)