forked from etcd-io/etcd
/
main.go
216 lines (198 loc) · 6.83 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2018 The etcd Authors
//
// 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.
// etcd-proxy is a proxy layer that simulates various network conditions.
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/coreos/etcd/pkg/proxy"
"go.uber.org/zap"
)
var from string
var to string
var httpPort int
var verbose bool
func main() {
// TODO: support TLS
flag.StringVar(&from, "from", "localhost:23790", "Address URL to proxy from.")
flag.StringVar(&to, "to", "localhost:2379", "Address URL to forward.")
flag.IntVar(&httpPort, "http-port", 2378, "Port to serve etcd-proxy API.")
flag.BoolVar(&verbose, "verbose", false, "'true' to run proxy in verbose mode.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %q:\n", os.Args[0])
fmt.Fprintln(os.Stderr, `
etcd-proxy simulates various network conditions for etcd testing purposes.
See README.md for more examples.
Example:
# build etcd
$ ./build
$ ./bin/etcd
# build etcd-proxy
$ make build-etcd-proxy
# to test etcd with proxy layer
$ ./bin/etcd-proxy --help
$ ./bin/etcd-proxy --from localhost:23790 --to localhost:2379 --http-port 2378 --verbose
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:2379 put foo bar
$ ETCDCTL_API=3 ./bin/etcdctl --endpoints localhost:23790 put foo bar`)
flag.PrintDefaults()
}
flag.Parse()
cfg := proxy.ServerConfig{
From: url.URL{Scheme: "tcp", Host: from},
To: url.URL{Scheme: "tcp", Host: to},
}
if verbose {
cfg.Logger = zap.NewExample()
}
p := proxy.NewServer(cfg)
<-p.Ready()
defer p.Close()
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(fmt.Sprintf("proxying [%s -> %s]\n", p.From(), p.To())))
})
mux.HandleFunc("/delay-tx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
w.Write([]byte(fmt.Sprintf("current send latency %v\n", p.LatencyTx())))
case http.MethodPut, http.MethodPost:
if err := req.ParseForm(); err != nil {
w.Write([]byte(fmt.Sprintf("wrong form %q\n", err.Error())))
return
}
lat, err := time.ParseDuration(req.PostForm.Get("latency"))
if err != nil {
w.Write([]byte(fmt.Sprintf("wrong latency form %q\n", err.Error())))
return
}
rv, err := time.ParseDuration(req.PostForm.Get("random-variable"))
if err != nil {
w.Write([]byte(fmt.Sprintf("wrong random-variable form %q\n", err.Error())))
return
}
p.DelayTx(lat, rv)
w.Write([]byte(fmt.Sprintf("added send latency %v±%v (current latency %v)\n", lat, rv, p.LatencyTx())))
case http.MethodDelete:
lat := p.LatencyTx()
p.UndelayTx()
w.Write([]byte(fmt.Sprintf("removed latency %v\n", lat)))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
mux.HandleFunc("/delay-rx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
w.Write([]byte(fmt.Sprintf("current receive latency %v\n", p.LatencyRx())))
case http.MethodPut, http.MethodPost:
if err := req.ParseForm(); err != nil {
w.Write([]byte(fmt.Sprintf("wrong form %q\n", err.Error())))
return
}
lat, err := time.ParseDuration(req.PostForm.Get("latency"))
if err != nil {
w.Write([]byte(fmt.Sprintf("wrong latency form %q\n", err.Error())))
return
}
rv, err := time.ParseDuration(req.PostForm.Get("random-variable"))
if err != nil {
w.Write([]byte(fmt.Sprintf("wrong random-variable form %q\n", err.Error())))
return
}
p.DelayRx(lat, rv)
w.Write([]byte(fmt.Sprintf("added receive latency %v±%v (current latency %v)\n", lat, rv, p.LatencyRx())))
case http.MethodDelete:
lat := p.LatencyRx()
p.UndelayRx()
w.Write([]byte(fmt.Sprintf("removed latency %v\n", lat)))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
mux.HandleFunc("/pause-tx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPut, http.MethodPost:
p.PauseTx()
w.Write([]byte(fmt.Sprintf("paused forwarding [%s -> %s]\n", p.From(), p.To())))
case http.MethodDelete:
p.UnpauseTx()
w.Write([]byte(fmt.Sprintf("unpaused forwarding [%s -> %s]\n", p.From(), p.To())))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
mux.HandleFunc("/pause-rx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPut, http.MethodPost:
p.PauseRx()
w.Write([]byte(fmt.Sprintf("paused forwarding [%s <- %s]\n", p.From(), p.To())))
case http.MethodDelete:
p.UnpauseRx()
w.Write([]byte(fmt.Sprintf("unpaused forwarding [%s <- %s]\n", p.From(), p.To())))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
mux.HandleFunc("/blackhole-tx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPut, http.MethodPost:
p.BlackholeTx()
w.Write([]byte(fmt.Sprintf("blackholed; dropping packets [%s -> %s]\n", p.From(), p.To())))
case http.MethodDelete:
p.UnblackholeTx()
w.Write([]byte(fmt.Sprintf("unblackholed; restart forwarding [%s -> %s]\n", p.From(), p.To())))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
mux.HandleFunc("/blackhole-rx", func(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodPut, http.MethodPost:
p.BlackholeRx()
w.Write([]byte(fmt.Sprintf("blackholed; dropping packets [%s <- %s]\n", p.From(), p.To())))
case http.MethodDelete:
p.UnblackholeRx()
w.Write([]byte(fmt.Sprintf("unblackholed; restart forwarding [%s <- %s]\n", p.From(), p.To())))
default:
w.Write([]byte(fmt.Sprintf("unsupported method %q\n", req.Method)))
}
})
srv := &http.Server{
Addr: fmt.Sprintf(":%d", httpPort),
Handler: mux,
}
defer srv.Close()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sig)
go func() {
s := <-sig
fmt.Printf("\n\nreceived signal %q, shutting down HTTP server\n\n", s)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
err := srv.Shutdown(ctx)
cancel()
fmt.Printf("gracefully stopped HTTP server with %v\n\n", err)
os.Exit(0)
}()
fmt.Printf("\nserving HTTP server http://localhost:%d\n\n", httpPort)
err := srv.ListenAndServe()
fmt.Printf("HTTP server exit with error %v\n", err)
}