From 3198bf851e63431cea39e1d85fa62d4ea2440450 Mon Sep 17 00:00:00 2001 From: Zachary Huff Date: Thu, 15 Aug 2019 02:40:33 -0400 Subject: [PATCH] Add nonces --- nonces/nonces.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nonces/nonces.go diff --git a/nonces/nonces.go b/nonces/nonces.go new file mode 100644 index 0000000..18c14f4 --- /dev/null +++ b/nonces/nonces.go @@ -0,0 +1,40 @@ +package nonces + +import ( + "sync" + "time" +) + +var ( + pastNonces = map[string]bool{} + presNonces = map[string]bool{} + lock = sync.RWMutex{} +) + +func Add(k string) { + lock.Lock() + presNonces[k] = true + lock.Unlock() +} + +func Contains(k string) (v bool) { + lock.RLock() + if presNonces[k] || pastNonces[k] { + v = true + } + lock.RUnlock() + + return +} + +func init() { + go func() { + for { + time.Sleep(15 * time.Second) + lock.Lock() + pastNonces = presNonces + presNonces = map[string]bool{} + lock.Unlock() + } + }() +}