/
drop.go
67 lines (61 loc) · 1.42 KB
/
drop.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package p2ptest
import (
"sync"
"github.com/brendoncarroll/go-p2p/s/memswarm"
)
// NewDropFirstPairwise returns a Message transformer which drops the first message between each pair
// of addresses {src, dst}.
//
// NewDropFirstPairwise tracks src/dst combinations.
// NewDropFirstTuple tracks src/dst permutations.
func NewDropFirstPairwise() func(*memswarm.Message) bool {
type A = memswarm.Addr
type M = memswarm.Message
type FlowID [2]A
var mu sync.Mutex
m := map[FlowID]struct{}{}
return func(x *M) bool {
flowID := FlowID{min(x.Src, x.Dst), max(x.Src, x.Dst)}
mu.Lock()
defer mu.Unlock()
if _, exists := m[flowID]; exists {
return true
}
m[flowID] = struct{}{}
return false
}
}
// NewDropFirstTuple returns a Message transformer which drops the first message
// for each (src, dst) tuple.
//
// NewDropFirstTuple tracks src/dst permutations.
// NewDropFirstPairwise tracks src/dst combinations.
func NewDropFirstTuple() func(*memswarm.Message) bool {
type A = memswarm.Addr
type M = memswarm.Message
type Tuple [2]A
var mu sync.Mutex
m := map[Tuple]struct{}{}
return func(x *M) bool {
t := Tuple{x.Src, x.Dst}
mu.Lock()
defer mu.Unlock()
if _, exists := m[t]; exists {
return true
}
m[t] = struct{}{}
return false
}
}
func min(a, b memswarm.Addr) memswarm.Addr {
if a.N < b.N {
return a
}
return b
}
func max(a, b memswarm.Addr) memswarm.Addr {
if a.N > b.N {
return a
}
return b
}