1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-23 05:27:31 +00:00
steve/pkg/clustercache/cancel_collection.go

30 lines
490 B
Go
Raw Normal View History

2019-09-09 21:28:55 +00:00
package clustercache
import (
"context"
"sync"
"sync/atomic"
)
type cancelCollection struct {
id int64
items sync.Map
}
func (c *cancelCollection) Add(ctx context.Context, obj interface{}) {
key := atomic.AddInt64(&c.id, 1)
c.items.Store(key, obj)
go func() {
<-ctx.Done()
c.items.Delete(key)
}()
}
func (c *cancelCollection) List() (result []interface{}) {
c.items.Range(func(key, value interface{}) bool {
result = append(result, value)
return true
})
return
}