-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathserver.go
More file actions
179 lines (154 loc) · 4.44 KB
/
server.go
File metadata and controls
179 lines (154 loc) · 4.44 KB
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
172
173
174
175
176
177
178
179
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package web
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/etcd-io/etcdlabs/cluster"
"github.com/etcd-io/etcdlabs/pkg/ratelimit"
"github.com/axiomhq/hyperloglog"
)
var (
rootPortMu sync.Mutex
rootPort = 2389
)
func startCluster(rootCtx context.Context, rootCancel func()) (*cluster.Cluster, error) {
rootPortMu.Lock()
port := rootPort
rootPort += 10 // for testing
rootPortMu.Unlock()
dir, err := ioutil.TempDir(os.TempDir(), "backend-cluster")
if err != nil {
return nil, err
}
cfg := cluster.Config{
EmbeddedClient: true,
Size: 5,
RootDir: dir,
RootPort: port,
ClientAutoTLS: false,
PeerAutoTLS: false,
RootCtx: rootCtx,
RootCancel: rootCancel,
}
return cluster.Start(cfg)
}
// Server warps http.Server.
type Server struct {
mu sync.RWMutex
addrURL url.URL
httpServer *http.Server
rootCancel func()
stopc chan struct{}
donec chan struct{}
}
var (
globalWebserverPort int
globalCluster *cluster.Cluster
globalServerVisitsMu sync.Mutex
globalServerVisits = hyperloglog.New16()
globalClientRequestIntervalLimit = 3 * time.Second
globalClientRequestLimiter ratelimit.RequestLimiter
globalStopRestartIntervalLimit = 5 * time.Second
globalStopRestartLimiter ratelimit.RequestLimiter
)
// StartServer starts a backend webserver with stoppable listener.
func StartServer(port int) (*Server, error) {
globalWebserverPort = port
rootCtx, rootCancel := context.WithCancel(context.Background())
c, err := startCluster(rootCtx, rootCancel)
if err != nil {
return nil, err
}
globalCluster = c
// allow only 1 request for every 2 second
globalClientRequestLimiter = ratelimit.NewRequestLimiter(rootCtx, globalClientRequestIntervalLimit)
// rate-limit more strictly for every 3 second
globalStopRestartLimiter = ratelimit.NewRequestLimiter(rootCtx, globalStopRestartIntervalLimit)
mux := http.NewServeMux()
mux.Handle("/health", &ContextAdapter{
ctx: rootCtx,
handler: ContextHandlerFunc(func(ctx context.Context, w http.ResponseWriter, req *http.Request) error {
w.WriteHeader(200)
w.Write([]byte("OK"))
return nil
}),
})
mux.Handle("/conn", &ContextAdapter{
ctx: rootCtx,
handler: withCache(ContextHandlerFunc(connectHandler)),
})
mux.Handle("/server-status", &ContextAdapter{
ctx: rootCtx,
handler: withCache(ContextHandlerFunc(serverStatusHandler)),
})
mux.Handle("/client-request", &ContextAdapter{
ctx: rootCtx,
handler: withCache(ContextHandlerFunc(clientRequestHandler)),
})
stopc := make(chan struct{})
addrURL := url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port)}
lg.Infof("started server %s", addrURL.String())
srv := &Server{
addrURL: addrURL,
httpServer: &http.Server{Addr: addrURL.Host, Handler: mux},
rootCancel: rootCancel,
stopc: stopc,
donec: make(chan struct{}),
}
go func() {
defer func() {
if err := recover(); err != nil {
lg.Warnf("etcd-play error (%v)", err)
os.Exit(0)
}
srv.rootCancel()
close(srv.donec)
}()
go func() { updateClusterStatus(srv.stopc) }()
go func() { cleanCache(srv.stopc) }()
if err := srv.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
lg.Fatal(err)
}
}()
return srv, nil
}
// StopNotify returns receive-only stop channel to notify the server has stopped.
func (srv *Server) StopNotify() <-chan struct{} {
return srv.stopc
}
// Stop stops the server. Useful for testing.
func (srv *Server) Stop() {
lg.Warnf("stopping server %s", srv.addrURL.String())
srv.mu.Lock()
if srv.httpServer == nil {
srv.mu.Unlock()
return
}
close(srv.stopc)
srv.httpServer.Close()
<-srv.donec
srv.mu.Unlock()
lg.Warnf("stopped server %s", srv.addrURL.String())
lg.Warn("stopping cluster")
globalCluster.Shutdown()
globalCluster = nil
lg.Warn("stopped cluster")
}