-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample_test.go
More file actions
207 lines (179 loc) · 5.32 KB
/
example_test.go
File metadata and controls
207 lines (179 loc) · 5.32 KB
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
/*
Copyright Derrick J Wippler
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.
*/
package groupcache_test
import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"time"
"github.com/segmentio/fasthash/fnv1"
"github.com/groupcache/groupcache-go/v3"
"github.com/groupcache/groupcache-go/v3/transport"
"github.com/groupcache/groupcache-go/v3/transport/peer"
)
// ExampleNew demonstrates starting a groupcache http instance with its own
// listener.
// nolint
func ExampleNew() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
// Starts an instance of groupcache with the provided transport
d, err := groupcache.ListenAndServe(ctx, "192.168.1.1:8080", groupcache.Options{
// If transport is nil, defaults to HttpTransport
Transport: nil,
// The following are all optional
HashFn: fnv1.HashBytes64,
Logger: slog.Default(),
Replicas: 50,
})
cancel()
if err != nil {
log.Fatal("while starting server on 192.168.1.1:8080")
}
// Create a new group cache with a max cache size of 3MB
group, err := d.NewGroup("users", 3000000, groupcache.GetterFunc(
func(ctx context.Context, id string, dest transport.Sink) error {
// Set the user in the groupcache to expire after 5 minutes
if err := dest.SetString("hello", time.Now().Add(time.Minute*5)); err != nil {
return err
}
return nil
},
))
if err != nil {
log.Fatal(err)
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
var value string
if err := group.Get(ctx, "12345", transport.StringSink(&value)); err != nil {
log.Fatal(err)
}
fmt.Printf("Value: %s\n", value)
// Remove the key from the groupcache
if err := group.Remove(ctx, "12345"); err != nil {
fmt.Printf("Remove Err: %s\n", err)
log.Fatal(err)
}
// Shutdown the daemon
_ = d.Shutdown(context.Background())
}
// ExampleNewHttpTransport demonstrates how to use groupcache in a service that
// is already listening for HTTP requests.
// nolint
func ExampleNewHttpTransport() {
mux := http.NewServeMux()
// Add endpoints specific to our application
mux.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, this is a non groupcache handler")
})
// Explicitly instantiate and use the HTTP transport
t := transport.NewHttpTransport(
transport.HttpTransportOptions{
// BasePath specifies the HTTP path that will serve groupcache requests.
// If blank, it defaults to "/_groupcache/".
BasePath: "/_groupcache/",
// Context optionally specifies a context for the server to use when it
// receives a request.
Context: nil,
// Client optionally provide a custom http client with TLS config
Client: nil,
// Scheme is is either `http` or `https` defaults to `http`
Scheme: "",
},
)
// Create a new groupcache instance
instance := groupcache.New(groupcache.Options{
HashFn: fnv1.HashBytes64,
Logger: slog.Default(),
Transport: t,
Replicas: 50,
})
// You can set the peers manually
err := instance.SetPeers(context.Background(), []peer.Info{
{
Address: "192.168.1.1:8080",
IsSelf: true,
},
{
Address: "192.168.1.1:8081",
IsSelf: false,
},
{
Address: "192.168.1.1:8082",
IsSelf: false,
},
})
if err != nil {
log.Fatal(err)
}
// OR you can register a peer discovery mechanism
//d := discovery.NewK8s(discovery.K8sConfig{
// OnUpdate: instance.SetPeers,
//})
//defer d.Shutdown(context.Background())
// Add the groupcache handler
mux.Handle("/_groupcache/", t)
server := http.Server{
Addr: "192.168.1.1:8080",
Handler: mux,
}
// Start a HTTP server to listen for peer requests from the groupcache
go func() {
log.Printf("Serving....\n")
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
defer func() { _ = server.Shutdown(context.Background()) }()
// Update the static peer config while groupcache is running
err = instance.SetPeers(context.Background(), []peer.Info{
{
Address: "192.168.1.1:8080",
IsSelf: true,
},
{
Address: "192.168.1.1:8081",
IsSelf: false,
},
})
if err != nil {
log.Fatal(err)
}
// Create a new group cache with a max cache size of 3MB
group, err := instance.NewGroup("users", 3000000, groupcache.GetterFunc(
func(ctx context.Context, id string, dest transport.Sink) error {
// Set the user in the groupcache to expire after 5 minutes
if err := dest.SetString("hello", time.Now().Add(time.Minute*5)); err != nil {
return err
}
return nil
},
))
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var value string
if err := group.Get(ctx, "12345", transport.StringSink(&value)); err != nil {
log.Fatal(err)
}
fmt.Printf("Value: %s\n", value)
// Remove the key from the groupcache
if err := group.Remove(ctx, "12345"); err != nil {
fmt.Printf("Remove Err: %s\n", err)
log.Fatal(err)
}
}