@@ -32,10 +32,12 @@ type CacheTest struct {
3232type RecentIPCache struct {
3333 cache map [string ]* CacheTest
3434 mu sync.RWMutex
35+
36+ tracer scamper.Tracer
3537}
3638
37- // GetTrace returns test content in []byte given a connection and tracer
38- func (rc * RecentIPCache ) Trace (conn connection.Connection , sc scamper. Tracer ) {
39+ // Trace performs a trace and adds it to the cache.
40+ func (rc * RecentIPCache ) Trace (conn connection.Connection ) {
3941 ip := conn .RemoteIP
4042 rc .mu .Lock ()
4143 _ , ok := rc .cache [ip ]
@@ -47,12 +49,12 @@ func (rc *RecentIPCache) Trace(conn connection.Connection, sc scamper.Tracer) {
4749 rc .cache [ip ] = nc
4850 rc .mu .Unlock ()
4951
50- nc .data = sc .Trace (conn , nc .timeStamp )
52+ nc .data = rc . tracer .Trace (conn , nc .timeStamp )
5153 close (nc .done )
5254 return
5355 }
5456 rc .mu .Unlock ()
55- sc .CreateCacheTest (conn , time .Now (), rc .GetData (ip ))
57+ rc . tracer .CreateCacheTest (conn , time .Now (), rc .GetData (ip ))
5658}
5759
5860func (rc * RecentIPCache ) GetCacheLength () int {
@@ -74,18 +76,20 @@ func (rc *RecentIPCache) GetData(ip string) string {
7476
7577// New creates and returns a RecentIPCache. It also starts up a background
7678// goroutine that scrubs the cache.
77- func New (ctx context.Context ) * RecentIPCache {
78- m := & RecentIPCache {}
79- m .cache = make (map [string ]* CacheTest )
79+ func New (ctx context.Context , tracer scamper.Tracer , ipCacheTimeout , ipCacheUpdatePeriod time.Duration ) * RecentIPCache {
80+ m := & RecentIPCache {
81+ cache : make (map [string ]* CacheTest ),
82+ tracer : tracer ,
83+ }
8084 go func () {
81- ticker := time .NewTicker (* IPCacheUpdatePeriod )
85+ ticker := time .NewTicker (ipCacheUpdatePeriod )
8286 defer ticker .Stop ()
8387 for now := range ticker .C {
8488 if ctx .Err () != nil {
8589 return
8690 }
8791 for k , v := range m .cache {
88- if now .Sub (v .timeStamp ) > * IPCacheTimeout {
92+ if now .Sub (v .timeStamp ) > ipCacheTimeout {
8993 fmt .Println ("try to delete " + k )
9094 m .mu .Lock ()
9195 delete (m .cache , k )
@@ -98,36 +102,3 @@ func New(ctx context.Context) *RecentIPCache {
98102 }()
99103 return m
100104}
101-
102- func (m * RecentIPCache ) len () int {
103- m .mu .RLock ()
104- defer m .mu .RUnlock ()
105- return len (m .cache )
106- }
107-
108- // Add an IP to the cache.
109- func (m * RecentIPCache ) Add (ip string ) {
110- m .mu .Lock ()
111- defer m .mu .Unlock ()
112- fmt .Printf ("func Add: Now is %d\n " , time .Now ().Unix ())
113- _ , present := m .cache [ip ]
114- if ! present {
115- m .cache [ip ] = & CacheTest {
116- timeStamp : time .Now (),
117- done : make (chan struct {}),
118- }
119- fmt .Printf ("just add %s %d\n " , ip , m .cache [ip ].timeStamp .Unix ())
120- }
121- }
122-
123- // Has tests whether an IP is in the cache.
124- func (m * RecentIPCache ) Has (ip string ) bool {
125- m .mu .RLock ()
126- defer m .mu .RUnlock ()
127- //fmt.Printf("func Has: Now is %d, length of cache: %d \n", time.Now().Unix(), m.Len())
128- if m .len () == 0 {
129- return false
130- }
131- _ , ok := m .cache [ip ]
132- return ok
133- }
0 commit comments