diff --git a/blacklist/blacklist.go b/blacklist/blacklist.go index 88d4c980..09e6cd60 100644 --- a/blacklist/blacklist.go +++ b/blacklist/blacklist.go @@ -41,8 +41,8 @@ func New(maxIdleTime time.Duration, allowedFailures int, blacklistExpiration tim maxIdleTime: maxIdleTime, allowedFailures: allowedFailures, blacklistExpiration: blacklistExpiration, - connections: make(chan string, 1000), - successes: make(chan string, 1000), + connections: make(chan string, 10000), + successes: make(chan string, 10000), firstConnectionTime: make(map[string]time.Time), failureCounts: make(map[string]int), blacklist: make(map[string]time.Time), @@ -54,7 +54,12 @@ func New(maxIdleTime time.Duration, allowedFailures int, blacklistExpiration tim // Succeed records a success for the given addr, which resets the failure count // for that IP and removes it from the blacklist. func (bl *Blacklist) Succeed(ip string) { - bl.successes <- ip + select { + case bl.successes <- ip: + // ip submitted as success + default: + log.Debugf("Unable to record success from %v", ip) + } } // OnConnect records an attempt to connect from the given IP. If the IP is @@ -66,7 +71,12 @@ func (bl *Blacklist) OnConnect(ip string) bool { if blacklisted { log.Debugf("%v is blacklisted", ip) } else { - bl.connections <- ip + select { + case bl.connections <- ip: + // ip submitted as connected + default: + log.Debugf("Unable to record connection from %v", ip) + } } return !blacklisted } diff --git a/http_proxy_test.go b/http_proxy_test.go index 92c6ceb0..7b0797ea 100644 --- a/http_proxy_test.go +++ b/http_proxy_test.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "strconv" + "sync" "testing" "time" @@ -117,10 +118,12 @@ func TestReportStats(t *testing.T) { testRoundTrip(t, httpProxyAddr, false, httpTargetServer, testFn) testRoundTrip(t, tlsProxyAddr, true, httpTargetServer, testFn) time.Sleep(200 * time.Millisecond) + m.tmtx.Lock() assert.Equal(t, 2, len(m.traffic)) if len(m.traffic) > 0 { t.Logf("%+v", m.traffic[0]) } + m.tmtx.Unlock() } func TestMaxConnections(t *testing.T) { @@ -135,9 +138,9 @@ func TestMaxConnections(t *testing.T) { okFn := func(conn net.Conn, targetURL *url.URL) { req := fmt.Sprintf(connectReq, targetURL.Host, targetURL.Host, validToken, deviceId) conn.Write([]byte(req)) - var buf [400]byte - _, err = conn.Read(buf[:]) + var buf [400]byte + _, err := conn.Read(buf[:]) assert.NoError(t, err) time.Sleep(time.Millisecond * 100) @@ -148,11 +151,12 @@ func TestMaxConnections(t *testing.T) { req := fmt.Sprintf(connectReq, targetURL.Host, targetURL.Host, validToken, deviceId) conn.Write([]byte(req)) - var buf [400]byte - _, err = conn.Read(buf[:]) + var buf [400]byte + _, err := conn.Read(buf[:]) if assert.Error(t, err) { e, ok := err.(*net.OpError) + assert.True(t, ok && e.Timeout(), "should be a time out error") } } @@ -161,7 +165,7 @@ func TestMaxConnections(t *testing.T) { go testRoundTrip(t, limitedServerAddr, false, httpTargetServer, okFn) } - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Millisecond * 50) for i := 0; i < 5; i++ { go testRoundTrip(t, limitedServerAddr, false, httpTargetServer, waitFn) @@ -782,6 +786,8 @@ type mockReporter struct { error map[measured.Error]int latency []*measured.LatencyTracker traffic []*measured.TrafficTracker + lmtx sync.Mutex + tmtx sync.Mutex } func (nr *mockReporter) ReportError(e map[*measured.Error]int) error { @@ -791,12 +797,16 @@ func (nr *mockReporter) ReportError(e map[*measured.Error]int) error { return nil } -func (nr *mockReporter) ReportLatency(l []*measured.LatencyTracker) error { - nr.latency = append(nr.latency, l...) +func (mr *mockReporter) ReportLatency(l []*measured.LatencyTracker) error { + mr.lmtx.Lock() + defer mr.lmtx.Unlock() + mr.latency = append(mr.latency, l...) return nil } -func (nr *mockReporter) ReportTraffic(t []*measured.TrafficTracker) error { - nr.traffic = append(nr.traffic, t...) +func (mr *mockReporter) ReportTraffic(t []*measured.TrafficTracker) error { + mr.tmtx.Lock() + defer mr.tmtx.Unlock() + mr.traffic = append(mr.traffic, t...) return nil } diff --git a/wercker.yml b/wercker.yml index 93140790..7508e74a 100644 --- a/wercker.yml +++ b/wercker.yml @@ -27,6 +27,6 @@ build: go build ./... # Test the project - script: - name: go test + name: go test -race code: | go test ./...