Skip to content

Commit

Permalink
Merge pull request #65 from getlantern/issue-64
Browse files Browse the repository at this point in the history
Fix data races
  • Loading branch information
oxtoacart committed May 3, 2016
2 parents dc99b11 + ed6d619 commit 3599b95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
18 changes: 14 additions & 4 deletions blacklist/blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand All @@ -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
}
Expand Down
28 changes: 19 additions & 9 deletions http_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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")
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ build:
go build ./...
# Test the project
- script:
name: go test
name: go test -race
code: |
go test ./...

0 comments on commit 3599b95

Please sign in to comment.