-
Notifications
You must be signed in to change notification settings - Fork 2
/
af.go
169 lines (155 loc) · 3.42 KB
/
af.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
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
// Copyright 2018 ailn([email protected]). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//Package af provides easy to use graceful restart a http server
package af
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"reflect"
"time"
)
var (
graceful *bool = flag.Bool("graceful", false, "listen on fd open 3 (internal use only)")
)
type AF struct {
Addr string
Handler http.Handler
ShutdownTimeOut time.Duration
listener net.Listener
server *http.Server
signalSlice []os.Signal
signalHandlerMap map[os.Signal]func(*AF)
}
// Default Get default AF
func Default() *AF {
return &AF{
Addr: ":8080",
ShutdownTimeOut: time.Second * 10,
}
}
// NewWithHandler Get AF with a http handler
func NewWithHandler(handler http.Handler) *AF {
return &AF{
Addr: ":8080",
Handler: handler,
ShutdownTimeOut: time.Second * 10,
}
}
// NewWithServer Get AF with a http server
func NewWithServer(server *http.Server) *AF {
return &AF{
server: server,
Addr: server.Addr,
Handler: server.Handler,
ShutdownTimeOut: time.Second * 10,
}
}
//init AF
func (af *AF) init() error {
if af.server == nil {
if af.Handler == nil {
af.Handler = http.DefaultServeMux
}
af.server = &http.Server{
Addr: af.Addr,
Handler: af.Handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
}
var err error
if *graceful {
f := os.NewFile(3, "")
af.listener, err = net.FileListener(f)
} else {
af.listener, err = net.Listen("tcp", af.server.Addr)
}
return err
}
//signal handle
func (af *AF) signalHandle() {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, af.signalSlice...)
for {
select {
case s := <-signalChan:
if f, ok := af.signalHandlerMap[s]; ok {
f(af)
}
break
}
}
}
// HandleSignal Give a handler for signals
func (af *AF) HandleSignal(handler func(*AF), signals ...os.Signal) {
if af.signalHandlerMap == nil {
af.signalHandlerMap = make(map[os.Signal]func(*AF))
}
for _, s := range signals {
if _, ok := af.signalHandlerMap[s]; !ok {
af.signalSlice = append(af.signalSlice, s)
}
af.signalHandlerMap[s] = handler
}
}
//Run the AF
func (af *AF) Run() error {
flag.Parse()
var err error
if err = af.init(); err != nil {
return err
}
go func(af *AF) {
log.Println(fmt.Sprintf("AF server is run at pid:%d", os.Getpid()))
err = af.server.Serve(af.listener)
}(af)
if af.signalHandlerMap == nil {
af.defaultSignalHandle()
}
af.signalHandle()
return nil
}
//Stop the AF
func (af *AF) Stop() {
ctx, _ := context.WithTimeout(context.Background(), af.ShutdownTimeOut)
err := af.server.Shutdown(ctx)
if err == nil {
os.Exit(0)
}
os.Exit(1)
}
//Reload the AF
func (af *AF) Reload() error {
//listener
listener, ok := af.listener.(*net.TCPListener)
if !ok {
return errors.New("AF.listener type must be *net.TCPListener,but got type " + reflect.TypeOf(af.listener).Name())
}
file, err := listener.File()
if err != nil {
return err
}
args := os.Args
if !*graceful {
args = append(args, "-graceful")
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.ExtraFiles = []*os.File{file}
err = cmd.Start()
if err == nil {
af.Stop()
}
return err
}