ソースを参照

Implement a very simple request cache

This really only makes sense for the proxy mode, otherwise it would
"cache" the already loaded responses, but the responses *are* the cache.

Maybe the cache should be a separate variable and we should forbid to
use `-cache` without `-proxy-url`?
Lu Stadler 7 年 前
コミット
bc1a334e24
共有1 個のファイルを変更した19 個の追加10 個の削除を含む
  1. 19 10
      go/fake-http/fake-http.go

+ 19 - 10
go/fake-http/fake-http.go

@ -28,6 +28,7 @@ var flags struct {
28 28
	proxyClientKey  string
29 29
30 30
	proxyMinikube bool
31
	cache         bool
31 32
}
32 33
33 34
func init() {
@ -37,6 +38,7 @@ func init() {
37 38
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
38 39
39 40
	flag.BoolVar(&flags.proxyMinikube, "proxy-minikube", false, "Shortcut for -proxy-url https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key")
41
	flag.BoolVar(&flags.cache, "cache", false, "Cache all requests")
40 42
}
41 43
42 44
func main() {
@ -69,14 +71,19 @@ func main() {
69 71
	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
70 72
		responses.Load(responsesPath)
71 73
74
		stub := responses.Match(req)
75
		haveCachedStub := flags.cache && stub != nil
72 76
		var resp *http.Response
73
		if flags.proxyURL != "" {
77
		if flags.proxyURL != "" && !haveCachedStub {
74 78
			resp = respondWithProxy(flags.proxyURL, &cert, w, req)
75 79
		} else {
76
			resp = respondWithStub(responses, w, req)
80
			resp = respondWithStub(stub, w, req)
77 81
		}
78 82
79
		requestLog.Log(req, resp)
83
		e := requestLog.Log(req, resp)
84
		if flags.cache {
85
			responses.Add(e.AsResponse())
86
		}
80 87
	})
81 88
82 89
	http.HandleFunc("/_log", func(w http.ResponseWriter, req *http.Request) {
@ -140,12 +147,7 @@ func proxyMinikube() error {
140 147
	return nil
141 148
}
142 149
143
func respondWithStub(responses Responses, w http.ResponseWriter, req *http.Request) *http.Response {
144
	resp := responses.Match(req)
145
	if resp == nil {
146
		resp = &Response{Status: 404, Body: "Not found"}
147
	}
148
150
func respondWithStub(resp *Response, w http.ResponseWriter, req *http.Request) *http.Response {
149 151
	time.Sleep(resp.Delay)
150 152
	if resp.RandomDelay > 0 {
151 153
		time.Sleep(time.Duration(rand.Intn(int(resp.RandomDelay))))
@ -227,7 +229,7 @@ func renderYAML(w http.ResponseWriter, responses []Response) error {
227 229
type Log []LogEntry
228 230
229 231
// Log logs the request/response pair.
230
func (l *Log) Log(req *http.Request, resp *http.Response) {
232
func (l *Log) Log(req *http.Request, resp *http.Response) *LogEntry {
231 233
	userAgent := req.Header.Get("User-Agent")
232 234
	log.Printf("%s %s - %d (%s, %q)", req.Method, req.URL, resp.StatusCode, req.RemoteAddr, userAgent)
233 235
@ -249,6 +251,7 @@ func (l *Log) Log(req *http.Request, resp *http.Response) {
249 251
	}
250 252
	io.Copy(e.responseBody, resp.Body)
251 253
	*l = append(*l, e)
254
	return &e
252 255
}
253 256
254 257
// AsResponses returns the log as a list of response definition.
@ -351,6 +354,12 @@ func (rs *Responses) Match(req *http.Request) *Response {
351 354
	return nil
352 355
}
353 356
357
// Add adds the response to the log, to include it in future Match
358
// calls.
359
func (rs *Responses) Add(r Response) {
360
	*rs = append(*rs, r)
361
}
362
354 363
// Load loads responses from the YAML file at path.
355 364
func (rs *Responses) Load(path string) {
356 365
	if path == "" {