File size: 817 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package twilio

import (
	"sync"
)

const (
	maxPassiveReplyCount = 5
)

type replyLimiter struct {
	mx sync.Mutex

	state map[string]int
}

func newReplyLimiter() *replyLimiter {
	return &replyLimiter{
		state: make(map[string]int),
	}
}

// RecordPassiveReply will increment the number of passive replies to a number.
func (r *replyLimiter) RecordPassiveReply(toNumber string) {
	r.mx.Lock()
	defer r.mx.Unlock()

	r.state[toNumber]++
}

// ShouldDrop will return true if the message should be dropped.
func (r *replyLimiter) ShouldDrop(toNumber string) bool {
	r.mx.Lock()
	defer r.mx.Unlock()

	return r.state[toNumber] >= maxPassiveReplyCount
}

// Reset will reset the counter for the given number.
func (r *replyLimiter) Reset(toNumber string) {
	r.mx.Lock()
	defer r.mx.Unlock()

	delete(r.state, toNumber)
}