-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
171 lines (137 loc) · 3.38 KB
/
server.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
170
171
package httpway
import (
"crypto/md5"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
//create a new server instance
//param server - is a instance of http.Server, can be nil and a default one will be created
func NewServer(server *http.Server) *Server {
srv := &Server{}
if server != nil {
srv.Server = server
} else {
srv.Server = &http.Server{}
}
return srv
}
//server structure
type Server struct {
*http.Server
serverInstanceId string
listener net.Listener
lastError error
serverGroup *sync.WaitGroup
clientsGroup chan bool
}
//server instance id
func (s *Server) InstanceId() string {
return s.serverInstanceId
}
//this will start server
//command isn't blocking, will exit after run
func (s *Server) Start() error {
if s.Handler == nil {
return errors.New("No server handler set")
}
if s.listener != nil {
return errors.New("Server already started")
}
addr := s.Addr
if addr == "" {
addr = ":http"
}
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
hostname, _ := os.Hostname()
s.serverInstanceId = fmt.Sprintf("%x", md5.Sum([]byte(hostname+addr)))
s.listener = listener
s.serverGroup = &sync.WaitGroup{}
s.clientsGroup = make(chan bool, 50000)
if s.ErrorLog == nil {
if r, ok := s.Handler.(ishttpwayrouter); ok {
s.ErrorLog = log.New(&internalServerLoggerWriter{r.(*Router).Logger}, "", 0)
}
}
s.Handler = &serverHandler{s.Handler, s.clientsGroup, s.serverInstanceId}
s.serverGroup.Add(1)
go func() {
defer s.serverGroup.Done()
err := s.Serve(listener)
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") {
return
}
s.lastError = err
}
}()
return nil
}
//send stop command to the server
func (s *Server) Stop() error {
if s.listener == nil {
return errors.New("Server not started")
}
if err := s.listener.Close(); err != nil {
return err
}
return s.lastError
}
//check if the server is started
//will return true even if the server is stopped but there are still some requests to finish
func (s *Server) IsStarted() bool {
if s.listener != nil {
return true
}
if len(s.clientsGroup) > 0 {
return true
}
return false
}
//wait until server is stopped and all requests are finish
//timeout - is the time to wait for the requests to finish after the server is stopped
//will return error if there are still some requests not finished
func (s *Server) WaitStop(timeout time.Duration) error {
if s.listener == nil {
return errors.New("Server not started")
}
s.serverGroup.Wait()
checkClients := time.Tick(100 * time.Millisecond)
timeoutTime := time.NewTimer(timeout)
for {
select {
case <-checkClients:
if len(s.clientsGroup) == 0 {
return s.lastError
}
case <-timeoutTime.C:
return fmt.Errorf("WaitStop error, timeout after %s waiting for %d client(s) to finish", timeout, len(s.clientsGroup))
}
}
return s.lastError
}
type serverHandler struct {
handler http.Handler
clientsGroup chan bool
serverInstanceId string
}
func (sh *serverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// sh.clientsGroup.Add(1)
// defer sh.clientsGroup.Done()
sh.clientsGroup <- true
defer func() { <-sh.clientsGroup }()
w.Header().Add("X-Server-Instance-Id", sh.serverInstanceId)
sh.handler.ServeHTTP(w, r)
}
type ishttpwayrouter interface {
Middleware(handle Handler) *Router
}