Skip to content

Commit cbe1d98

Browse files
committed
Added license notification to all files, fixed server/main.go
1 parent e9d2d70 commit cbe1d98

File tree

15 files changed

+389
-49
lines changed

15 files changed

+389
-49
lines changed

README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,66 @@ func ExampleUsage() {
160160
}
161161
```
162162

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+
163223
# Source Code Internals
164224
If you are reading this, you are likely in front of a Github page and are interested in building a custom transport
165225
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
201261
### peer.Info
202262
Is a struct which holds information used to identify each peer in the cluster. The `peer.Info` struct which represents
203263
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
205265
which peer in the cluster is our instance, `Instance.SetPeers()` will return an error if at least one peer with
206266
`IsSelf` is not set to `true`.
207267

@@ -210,12 +270,26 @@ Contains data structures used by groupcache to serialize data between remote ins
210270

211271
### cluster package
212272
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.
214287
```go
215-
// Start a 3 instance cluster using all the default options
288+
// Start a 3 instance cluster using the default options
216289
_ := cluster.Start(context.Background(), 3, groupcache.Options{})
217290
defer cluster.Shutdown(context.Background())
218291
```
219292

220293
### Code Map
221-
![docs/code-diagram.png](docs/code-diagram.png)
294+
![docs/code-diagram.png](docs/code-diagram.png)
295+

cache.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
Copyright 2012 Google Inc.
3+
Copyright Derrick J Wippler
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
118
package groupcache
219

320
import (

cache_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
Copyright 2012 Google Inc.
3+
Copyright Derrick J Wippler
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
118
package groupcache
219

320
import (

cluster/cluster.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,52 @@
1+
/*
2+
Copyright Derrick J Wippler
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/*
18+
Package cluster contains convince functions which make managing the creation of multiple groupcache instances
19+
simple.
20+
21+
# SpawnDaemon()
22+
23+
Spawns a single instance of groupcache using the config provided. The returned *Daemon has methods which
24+
make interacting with the groupcache instance simple.
25+
26+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
27+
defer cancel()
28+
29+
// Starts an instance of groupcache with the provided transport
30+
d, err := cluster.SpawnDaemon(ctx, "192.168.1.1:8080", groupcache.Options{})
31+
if err != nil {
32+
log.Fatal("while starting server on 192.168.1.1:8080")
33+
}
34+
35+
d.Shutdown(context.Background())
36+
37+
# Start() and StartWith()
38+
39+
Starts a local cluster of groupcache daemons suitable for testing. Users who wish to test groupcache in their
40+
own project test suites can use these methods to start and stop clusters. See cluster_test.go for more examples.
41+
42+
err := cluster.Start(context.Background(), 2, groupcache.Options{})
43+
require.NoError(t, err)
44+
45+
assert.Equal(t, 2, len(cluster.ListPeers()))
46+
assert.Equal(t, 2, len(cluster.ListDaemons()))
47+
err = cluster.Shutdown(context.Background())
48+
require.NoError(t, err)
49+
*/
150
package cluster
251

352
import (

cluster/cluster_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright Derrick J Wippler
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cluster_test
218

319
import (

cluster/daemon.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright Derrick J Wippler
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package cluster
218

319
import (

0 commit comments

Comments
 (0)