-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupcache.go
127 lines (101 loc) · 3.05 KB
/
groupcache.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/modernprogram/groupcache/v2"
"github.com/rs/zerolog/log"
"github.com/udhos/groupcache_exporter"
"github.com/udhos/groupcache_exporter/groupcache/modernprogram"
"github.com/udhos/kube/kubeclient"
"github.com/udhos/kubegroup/kubegroup"
)
func startGroupcache(app *application, forceNamespaceDefault bool) func() {
workspace := groupcache.NewWorkspace()
//
// create groupcache pool
//
myURL, errURL := kubegroup.FindMyURL(app.cfg.groupcachePort)
if errURL != nil {
log.Fatal().Msgf("groupcache my URL: %v", errURL)
}
log.Info().Msgf("groupcache my URL: %s", myURL)
pool := groupcache.NewHTTPPoolOptsWithWorkspace(workspace, myURL, &groupcache.HTTPPoolOptions{})
//
// start groupcache server
//
app.serverGroupCache = &http.Server{Addr: app.cfg.groupcachePort, Handler: pool}
go func() {
log.Info().Msgf("groupcache server: listening on %s", app.cfg.groupcachePort)
err := app.serverGroupCache.ListenAndServe()
log.Error().Msgf("groupcache server: exited: %v", err)
}()
//
// start watcher for addresses of peers
//
clientsetOpt := kubeclient.Options{DebugLog: app.cfg.kubegroupDebug}
clientset, errClientset := kubeclient.New(clientsetOpt)
if errClientset != nil {
log.Fatal().Msgf("startGroupcache: kubeclient: %v", errClientset)
}
options := kubegroup.Options{
Client: clientset,
LabelSelector: app.cfg.kubegroupLabelSelector,
Pool: pool,
GroupCachePort: app.cfg.groupcachePort,
MetricsRegisterer: app.registry,
MetricsGatherer: app.registry,
MetricsNamespace: app.cfg.kubegroupMetricsNamespace,
Debug: app.cfg.kubegroupDebug,
ForceNamespaceDefault: forceNamespaceDefault,
}
kg, errKg := kubegroup.UpdatePeers(options)
if errKg != nil {
log.Fatal().Msgf("kubegroup error: %v", errKg)
}
//
// create cache
//
getter := groupcache.GetterFunc(
func(c context.Context, key string, dest groupcache.Sink) error {
const me = "groupcache.getter"
ctx, span := app.tracer.Start(c, me)
defer span.End()
resp, isErrorStatus, errFetch := doFetch(ctx, app.tracer, app.httpClient, app.backendURL, key)
if errFetch != nil {
return errFetch
}
data, errJ := json.Marshal(resp)
if errJ != nil {
return fmt.Errorf("%s: marshal json response: %v", me, errJ)
}
var ttl time.Duration
if isErrorStatus {
ttl = app.cfg.cacheErrorTTL
} else {
ttl = app.cfg.cacheTTL
}
expire := time.Now().Add(ttl)
return dest.SetBytes(data, expire)
},
)
// https://talks.golang.org/2013/oscon-dl.slide#46
//
// 64 MB max per-node memory usage
app.cache = groupcache.NewGroupWithWorkspace(workspace, "path",
app.cfg.groupcacheSizeBytes, getter)
//
// expose prometheus metrics for groupcache
//
g := modernprogram.New(app.cache)
labels := map[string]string{}
namespace := ""
collector := groupcache_exporter.NewExporter(namespace, labels, g)
app.registry.MustRegister(collector)
stop := func() {
kg.Close()
}
return stop
}