@@ -160,6 +160,66 @@ func ExampleUsage() {
160
160
}
161
161
```
162
162
163
+ # HTTP integration
164
+ This is a quick guide on how to use groupcache in a service that is already listening for HTTP requests. In some
165
+ circumstances you may want to have groupcache response using the same HTTP port that non groupcache requests are
166
+ received through. In this case you must explicitly create the ` transport.HttpTransport ` which can then be passed
167
+ to your HTTP router/handler.
168
+
169
+ ``` go
170
+ func main () {
171
+ mux := http.NewServeMux ()
172
+
173
+ // Add endpoints specific to our application
174
+ mux.HandleFunc (" /index" , func (w http.ResponseWriter , r *http.Request ) {
175
+ fmt.Fprintf (w, " Hello, this is a non groupcache handler" )
176
+ })
177
+
178
+ // Explicitly instantiate and use the HTTP transport
179
+ t := transport.NewHttpTransport (
180
+ transport.HttpTransportOptions {
181
+ // BasePath specifies the HTTP path that will serve groupcache requests.
182
+ // If blank, it defaults to "/_groupcache/".
183
+ BasePath: " /_groupcache/" ,
184
+ // Context optionally specifies a context for the server to use when it
185
+ // receives a request.
186
+ Context: nil ,
187
+ // Client optionally provide a custom http client with TLS config
188
+ Client: nil ,
189
+ // Scheme is is either `http` or `https` defaults to `http`
190
+ Scheme: " http" ,
191
+ },
192
+ )
193
+
194
+ // Create a new groupcache instance
195
+ instance := groupcache.New (groupcache.Options {
196
+ // All of these fields are optional
197
+ HashFn: fnv1.HashBytes64 ,
198
+ Logger: slog.Default (),
199
+ Transport: t,
200
+ Replicas: 50 ,
201
+ })
202
+
203
+ // Add the groupcache handler
204
+ mux.Handle (" /_groupcache/" , t)
205
+
206
+ server := http.Server {
207
+ Addr: " 192.168.1.1:8080" ,
208
+ Handler: mux,
209
+ }
210
+
211
+ // Start a HTTP server to listen for peer requests from the groupcache
212
+ go func () {
213
+ log.Printf (" Serving....\n " )
214
+ if err := server.ListenAndServe (); err != nil {
215
+ log.Fatal (err)
216
+ }
217
+ }()
218
+ defer func () { _ = server.Shutdown (context.Background ()) }()
219
+ }
220
+ ```
221
+
222
+
163
223
# Source Code Internals
164
224
If you are reading this, you are likely in front of a Github page and are interested in building a custom transport
165
225
or creating a Pull Request. In which case, the following explains the most of the important structs and how they
@@ -201,7 +261,7 @@ Is a consistent hash ring which holds an instantiated client for each peer in th
201
261
### peer.Info
202
262
Is a struct which holds information used to identify each peer in the cluster. The ` peer.Info ` struct which represents
203
263
the current instance MUST be correctly identified by setting ` IsSelf = true ` . Without this, groupcache would send its
204
- self hash ring requests via the transport. To avoid accidentaly creating a cluster without correctly identifying
264
+ self hash ring requests via the transport. To avoid accidentally creating a cluster without correctly identifying
205
265
which peer in the cluster is our instance, ` Instance.SetPeers() ` will return an error if at least one peer with
206
266
` IsSelf ` is not set to ` true ` .
207
267
@@ -210,12 +270,26 @@ Contains data structures used by groupcache to serialize data between remote ins
210
270
211
271
### cluster package
212
272
Is a convenience package containing functions to easily spawn and shutdown groupcache instances (called daemons) or to
213
- create a cluster of group cache instances using the default ` transport.HttpTransport ` .
273
+ create a local cluster of group cache instances using the default ` transport.HttpTransport ` .
274
+
275
+ ** SpawnDaemon()** Spawns a single instance of groupcache using the config provided. The returned * Daemon has methods which
276
+ make interacting with the groupcache instance simple.
277
+
278
+ ``` go
279
+ // Starts an instance of groupcache with the provided transport
280
+ d , _ := cluster.SpawnDaemon (ctx, " 192.168.1.1:8080" , groupcache.Options {})
281
+ defer d.Shutdown (context.Background ())
282
+ ```
283
+
284
+ ** Start()** and ** StartWith()** starts a local cluster of groupcache daemons suitable for testing. Users who wish to
285
+ test groupcache in their own project test suites can use these methods to start and stop clusters.
286
+ See ` cluster_test.go ` for more examples.
214
287
``` go
215
- // Start a 3 instance cluster using all the default options
288
+ // Start a 3 instance cluster using the default options
216
289
_ := cluster.Start (context.Background (), 3 , groupcache.Options {})
217
290
defer cluster.Shutdown (context.Background ())
218
291
```
219
292
220
293
### Code Map
221
- ![ docs/code-diagram.png] ( docs/code-diagram.png )
294
+ ![ docs/code-diagram.png] ( docs/code-diagram.png )
295
+
0 commit comments