Skip to content

Commit

Permalink
Add nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
zachhuff386 committed Aug 15, 2019
1 parent 8df5d97 commit 3198bf8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions nonces/nonces.go
Original file line number Diff line number Diff line change
@@ -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()
}
}()
}

0 comments on commit 3198bf8

Please sign in to comment.