nonce.go 686 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package acme
  2. import (
  3. "sync"
  4. )
  5. // Simple thread-safe stack impl
  6. type nonceStack struct {
  7. lock sync.Mutex
  8. stack []string
  9. }
  10. // Pushes a nonce to the stack.
  11. // Doesn't push empty nonces, or if there's more than 100 nonces on the stack
  12. func (ns *nonceStack) push(v string) {
  13. if v == "" {
  14. return
  15. }
  16. ns.lock.Lock()
  17. defer ns.lock.Unlock()
  18. if len(ns.stack) > 100 {
  19. return
  20. }
  21. ns.stack = append(ns.stack, v)
  22. }
  23. // Pops a nonce from the stack.
  24. // Returns empty string if there are no nonces
  25. func (ns *nonceStack) pop() string {
  26. ns.lock.Lock()
  27. defer ns.lock.Unlock()
  28. n := len(ns.stack)
  29. if n == 0 {
  30. return ""
  31. }
  32. v := ns.stack[n-1]
  33. ns.stack = ns.stack[:n-1]
  34. return v
  35. }