()
| 10 | ) |
| 11 | |
| 12 | func 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 | |
| 44 | const sweepInterval = 1 * time.Second |
| 45 | const ttl = sweepInterval / 3 |
nothing calls this directly
no test coverage detected