pomerium/internal/sets/size_limited.go
Caleb Doxsey 36f73fa6c7
authorize: track session and service account access date (#3220)
* session: add accessed at date

* authorize: track session and service account access times

* Revert "databroker: add support for field masks on Put (#3210)"

This reverts commit 2dc778035d.

* add test

* fix data race in test

* add deadline for update

* track dropped accesses
2022-03-31 09:19:04 -06:00

36 lines
864 B
Go

package sets
// A SizeLimitedStringSet is a StringSet which is limited to a given size. Once
// the capacity is reached an element will be removed at random.
type SizeLimitedStringSet struct {
m map[string]struct{}
capacity int
}
// NewSizeLimitedStringSet create a new SizeLimitedStringSet.
func NewSizeLimitedStringSet(capacity int) *SizeLimitedStringSet {
return &SizeLimitedStringSet{
m: make(map[string]struct{}),
capacity: capacity,
}
}
// Add adds an element to the set.
func (s *SizeLimitedStringSet) Add(element string) {
s.m[element] = struct{}{}
for len(s.m) > s.capacity {
for k := range s.m {
delete(s.m, k)
break
}
}
}
// ForEach iterates over all the elements in the set.
func (s *SizeLimitedStringSet) ForEach(callback func(element string) bool) {
for k := range s.m {
if !callback(k) {
return
}
}
}