bloomring.go 607 B

123456789101112131415161718192021222324252627282930313233343536
  1. package antireplay
  2. import (
  3. "sync"
  4. ss_bloomring "github.com/v2fly/ss-bloomring"
  5. )
  6. type BloomRing struct {
  7. *ss_bloomring.BloomRing
  8. lock *sync.Mutex
  9. }
  10. func (b BloomRing) Interval() int64 {
  11. return 9999999
  12. }
  13. func (b BloomRing) Check(sum []byte) bool {
  14. b.lock.Lock()
  15. defer b.lock.Unlock()
  16. if b.Test(sum) {
  17. return false
  18. }
  19. b.Add(sum)
  20. return true
  21. }
  22. func NewBloomRing() BloomRing {
  23. const (
  24. DefaultSFCapacity = 1e6
  25. // FalsePositiveRate
  26. DefaultSFFPR = 1e-6
  27. DefaultSFSlot = 10
  28. )
  29. return BloomRing{ss_bloomring.NewBloomRing(DefaultSFSlot, DefaultSFCapacity, DefaultSFFPR), &sync.Mutex{}}
  30. }