-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupcache.go
93 lines (72 loc) · 2 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
package main
import (
"context"
"log"
"os"
"time"
"github.com/groupcache/groupcache-go/v3"
"github.com/groupcache/groupcache-go/v3/transport"
"github.com/udhos/kube/kubeclient"
"github.com/udhos/kubegroup/kubegroup"
)
func startGroupcache(app *application) {
//
// create groupcache instance
//
myIP, errAddr := kubegroup.FindMyAddress()
if errAddr != nil {
log.Fatalf("find my address: %v", errAddr)
}
myAddr := myIP + app.groupCachePort
daemon, errDaemon := groupcache.ListenAndServe(context.TODO(), myAddr, groupcache.Options{})
if errDaemon != nil {
log.Fatalf("groupcache daemon: %v", errDaemon)
}
//
// start watcher for addresses of peers
//
const debug = true
clientsetOpt := kubeclient.Options{DebugLog: debug}
clientset, errClientset := kubeclient.New(clientsetOpt)
if errClientset != nil {
log.Fatalf("kubeclient: %v", errClientset)
}
options := kubegroup.Options{
Client: clientset,
Peers: daemon,
LabelSelector: "app=miniapi",
GroupCachePort: app.groupCachePort, // ":5000"
Debug: debug,
MetricsRegisterer: app.registry,
MetricsGatherer: app.registry,
ForceNamespaceDefault: true,
}
group, errGroup := kubegroup.UpdatePeers(options)
if errGroup != nil {
log.Fatalf("kubegroup: %v", errGroup)
}
app.group = group
//
// create cache
//
getter := groupcache.GetterFunc(
func(_ context.Context, filePath string, dest transport.Sink) error {
log.Printf("cache miss, loading file: %s (ttl:%v)",
filePath, app.groupCacheExpire)
data, errRead := os.ReadFile(filePath)
if errRead != nil {
return errRead
}
var expire time.Time // zero value for expire means no expiration
if app.groupCacheExpire != 0 {
expire = time.Now().Add(app.groupCacheExpire)
}
return dest.SetBytes(data, expire)
},
)
cache, errGroup := daemon.NewGroup("files", app.groupCacheSizeBytes, getter)
if errGroup != nil {
log.Fatalf("new group: %v", errGroup)
}
app.cache = cache
}