Skip to content

Commit cf1a4fb

Browse files
committed
Allow Shutdown to be called more than once for sloppy clients
1 parent d229745 commit cf1a4fb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

ramcache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Ramcache struct {
3131
frozen bool
3232
done chan bool
3333
sync.RWMutex
34+
shutdownOnce sync.Once
3435
}
3536

3637
// New creates a Ramcache with a TTL of 5 minutes. You can change
@@ -156,7 +157,7 @@ func (rc *Ramcache) Keys() []string {
156157
// Shutdown cleanly stops any background work, allowing Ramcache
157158
// to be garbage collected.
158159
func (rc *Ramcache) Shutdown() {
159-
close(rc.done)
160+
rc.shutdownOnce.Do(func() { close(rc.done) })
160161
}
161162

162163
func (rc *Ramcache) cleanup() {

ramcache_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ func TestBool(t *testing.T) {
263263
}
264264
}
265265

266+
func TestShutdownMultiple(t *testing.T) {
267+
r := New()
268+
r.Set("asdfqwer", true)
269+
r.Set("zxcvzxcv", false)
270+
r.Shutdown()
271+
r.Shutdown()
272+
r.Shutdown()
273+
r.Shutdown()
274+
r.Shutdown()
275+
}
276+
266277
func BenchmarkSet(b *testing.B) {
267278
r := New()
268279
for i := 0; i < b.N; i++ {

0 commit comments

Comments
 (0)