MCPcopy Create free account
hub / github.com/Shopify/goose / ExampleNew

Function ExampleNew

lockmap/map_test.go:12–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10)
11
12func ExampleNew() {
13 m := New(1*time.Minute, &tomb.Tomb{})
14 m.Tomb().Go(m.Run)
15
16 output := ""
17
18 done := make(chan struct{})
19 waitOrWork := func() {
20 promise, gotLock := m.WaitOrLock("foo", 10*time.Second)
21 if gotLock {
22 // We just grabbed a new lock, do work with it.
23 <-time.After(100 * time.Millisecond)
24 output = "done"
25
26 // Once we're done, remove it, which will close the channel on the promise.
27 m.Release("foo")
28 } else {
29 <-promise
30 fmt.Println(output)
31 close(done)
32 }
33 }
34
35 // Either one could be trigger first
36 go waitOrWork()
37 go waitOrWork()
38
39 <-done
40 // Output:
41 // done
42}
43
44const sweepInterval = 1 * time.Second
45const ttl = sweepInterval / 3

Callers

nothing calls this directly

Calls 4

NewFunction · 0.70
TombMethod · 0.65
WaitOrLockMethod · 0.65
ReleaseMethod · 0.65

Tested by

no test coverage detected