This repository has been archived by the owner on Jan 29, 2019. It is now read-only.
forked from linkerd/linkerd-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.go
105 lines (88 loc) · 2.09 KB
/
web.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
package main
import (
"flag"
"fmt"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/go-redis/redis"
)
type redisClient struct {
addr string
key string
expiry time.Duration
client *redis.Client
sync.RWMutex
}
type handler struct {
redisClient *redisClient
latency time.Duration
}
func (c *redisClient) Get() (string, error) {
c.RLock()
defer c.RUnlock()
return c.client.Get(c.key).Result()
}
func (c *redisClient) Set(text string) error {
c.RLock()
defer c.RUnlock()
expiry := time.Duration(rand.Int63n(int64(c.expiry)))
return c.client.Set(c.key, text, expiry).Err()
}
func (c *redisClient) Refresh() {
c.Lock()
defer c.Unlock()
if c.client != nil {
c.client.Close()
}
c.client = redis.NewClient(&redis.Options{Addr: c.addr})
}
func (h *handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if text, err := h.redisClient.Get(); err == nil {
w.Write([]byte(text))
return
}
time.Sleep(h.latency)
text := "hello\n"
h.redisClient.Set(text)
w.Write([]byte(text))
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
addr := flag.String("addr", ":7770", "service address to serve on")
redisAddr := flag.String("redis-addr", "127.0.0.1:6379", "address of redis cluster")
latency := flag.Duration("latency", 300*time.Millisecond, "time to sleep on cache miss")
expiry := flag.Duration("expiry", 3*time.Minute, "max cache key expire time")
flag.Parse()
redisKey := "app:" + strconv.FormatInt(rand.Int63(), 16)
fmt.Printf("serving on %s, caching on %s\n", *addr, redisKey)
client := redisClient{
addr: *redisAddr,
key: redisKey,
expiry: *expiry,
}
client.Refresh()
// refresh connection every 5 seconds, with jitter
jitter := time.Duration(rand.Int63n(int64(*latency)))
go func() {
for _ = range time.Tick(5*time.Second + jitter) {
client.Refresh()
}
}()
httpServer := &http.Server{
Addr: *addr,
ReadTimeout: 10 * time.Second,
Handler: &handler{
redisClient: &client,
latency: *latency,
},
}
err := httpServer.ListenAndServe()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}