You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/gateway/agent/nonce.go

31 lines
603 B
Go

package agent
import (
"sync"
"time"
)
type memoryNonceStore struct {
mu sync.Mutex
values map[string]time.Time
}
func newMemoryNonceStore() *memoryNonceStore {
return &memoryNonceStore{values: make(map[string]time.Time)}
}
func (s *memoryNonceStore) UseNonce(nonce string, now, expiresAt time.Time) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
for value, expiry := range s.values {
if !expiry.After(now) {
delete(s.values, value)
}
}
if expiry, exists := s.values[nonce]; exists && expiry.After(now) {
return false, nil
}
s.values[nonce] = expiresAt
return true, nil
}