Unverified Commit 5a278ff0 authored by Steven Allen's avatar Steven Allen Committed by GitHub

feat: remove the context from the donthavetimeoutmanager (#303)

This removes one goroutine per peer which tends to be a pretty big deal. This
brings go-ipfs down from 5.5 to 4.5 goroutines per peer.
parent 694d2f81
...@@ -72,17 +72,17 @@ type dontHaveTimeoutMgr struct { ...@@ -72,17 +72,17 @@ type dontHaveTimeoutMgr struct {
// newDontHaveTimeoutMgr creates a new dontHaveTimeoutMgr // newDontHaveTimeoutMgr creates a new dontHaveTimeoutMgr
// onDontHaveTimeout is called when pending keys expire (not cancelled before timeout) // onDontHaveTimeout is called when pending keys expire (not cancelled before timeout)
func newDontHaveTimeoutMgr(ctx context.Context, pc PeerConnection, onDontHaveTimeout func([]cid.Cid)) *dontHaveTimeoutMgr { func newDontHaveTimeoutMgr(pc PeerConnection, onDontHaveTimeout func([]cid.Cid)) *dontHaveTimeoutMgr {
return newDontHaveTimeoutMgrWithParams(ctx, pc, onDontHaveTimeout, dontHaveTimeout, return newDontHaveTimeoutMgrWithParams(pc, onDontHaveTimeout, dontHaveTimeout,
latencyMultiplier, maxExpectedWantProcessTime) latencyMultiplier, maxExpectedWantProcessTime)
} }
// newDontHaveTimeoutMgrWithParams is used by the tests // newDontHaveTimeoutMgrWithParams is used by the tests
func newDontHaveTimeoutMgrWithParams(ctx context.Context, pc PeerConnection, onDontHaveTimeout func([]cid.Cid), func newDontHaveTimeoutMgrWithParams(pc PeerConnection, onDontHaveTimeout func([]cid.Cid),
defaultTimeout time.Duration, latencyMultiplier int, defaultTimeout time.Duration, latencyMultiplier int,
maxExpectedWantProcessTime time.Duration) *dontHaveTimeoutMgr { maxExpectedWantProcessTime time.Duration) *dontHaveTimeoutMgr {
ctx, shutdown := context.WithCancel(ctx) ctx, shutdown := context.WithCancel(context.Background())
mqp := &dontHaveTimeoutMgr{ mqp := &dontHaveTimeoutMgr{
ctx: ctx, ctx: ctx,
shutdown: shutdown, shutdown: shutdown,
...@@ -101,10 +101,7 @@ func newDontHaveTimeoutMgrWithParams(ctx context.Context, pc PeerConnection, onD ...@@ -101,10 +101,7 @@ func newDontHaveTimeoutMgrWithParams(ctx context.Context, pc PeerConnection, onD
// Shutdown the dontHaveTimeoutMgr. Any subsequent call to Start() will be ignored // Shutdown the dontHaveTimeoutMgr. Any subsequent call to Start() will be ignored
func (dhtm *dontHaveTimeoutMgr) Shutdown() { func (dhtm *dontHaveTimeoutMgr) Shutdown() {
dhtm.shutdown() dhtm.shutdown()
}
// onShutdown is called when the dontHaveTimeoutMgr shuts down
func (dhtm *dontHaveTimeoutMgr) onShutdown() {
dhtm.lk.Lock() dhtm.lk.Lock()
defer dhtm.lk.Unlock() defer dhtm.lk.Unlock()
...@@ -114,13 +111,6 @@ func (dhtm *dontHaveTimeoutMgr) onShutdown() { ...@@ -114,13 +111,6 @@ func (dhtm *dontHaveTimeoutMgr) onShutdown() {
} }
} }
// closeAfterContext is called when the dontHaveTimeoutMgr starts.
// It monitors for the context being cancelled.
func (dhtm *dontHaveTimeoutMgr) closeAfterContext() {
<-dhtm.ctx.Done()
dhtm.onShutdown()
}
// Start the dontHaveTimeoutMgr. This method is idempotent // Start the dontHaveTimeoutMgr. This method is idempotent
func (dhtm *dontHaveTimeoutMgr) Start() { func (dhtm *dontHaveTimeoutMgr) Start() {
dhtm.lk.Lock() dhtm.lk.Lock()
...@@ -132,8 +122,6 @@ func (dhtm *dontHaveTimeoutMgr) Start() { ...@@ -132,8 +122,6 @@ func (dhtm *dontHaveTimeoutMgr) Start() {
} }
dhtm.started = true dhtm.started = true
go dhtm.closeAfterContext()
// If we already have a measure of latency to the peer, use it to // If we already have a measure of latency to the peer, use it to
// calculate a reasonable timeout // calculate a reasonable timeout
latency := dhtm.peerConn.Latency() latency := dhtm.peerConn.Latency()
......
...@@ -75,13 +75,13 @@ func TestDontHaveTimeoutMgrTimeout(t *testing.T) { ...@@ -75,13 +75,13 @@ func TestDontHaveTimeoutMgrTimeout(t *testing.T) {
latMultiplier := 2 latMultiplier := 2
expProcessTime := 5 * time.Millisecond expProcessTime := 5 * time.Millisecond
expectedTimeout := expProcessTime + latency*time.Duration(latMultiplier) expectedTimeout := expProcessTime + latency*time.Duration(latMultiplier)
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
tr := timeoutRecorder{} tr := timeoutRecorder{}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
dontHaveTimeout, latMultiplier, expProcessTime) dontHaveTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add first set of keys // Add first set of keys
dhtm.AddPending(firstks) dhtm.AddPending(firstks)
...@@ -125,13 +125,13 @@ func TestDontHaveTimeoutMgrCancel(t *testing.T) { ...@@ -125,13 +125,13 @@ func TestDontHaveTimeoutMgrCancel(t *testing.T) {
latMultiplier := 1 latMultiplier := 1
expProcessTime := time.Duration(0) expProcessTime := time.Duration(0)
expectedTimeout := latency expectedTimeout := latency
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
tr := timeoutRecorder{} tr := timeoutRecorder{}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
dontHaveTimeout, latMultiplier, expProcessTime) dontHaveTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys // Add keys
dhtm.AddPending(ks) dhtm.AddPending(ks)
...@@ -156,13 +156,13 @@ func TestDontHaveTimeoutWantCancelWant(t *testing.T) { ...@@ -156,13 +156,13 @@ func TestDontHaveTimeoutWantCancelWant(t *testing.T) {
latMultiplier := 1 latMultiplier := 1
expProcessTime := time.Duration(0) expProcessTime := time.Duration(0)
expectedTimeout := latency expectedTimeout := latency
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
tr := timeoutRecorder{} tr := timeoutRecorder{}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
dontHaveTimeout, latMultiplier, expProcessTime) dontHaveTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys // Add keys
dhtm.AddPending(ks) dhtm.AddPending(ks)
...@@ -200,13 +200,13 @@ func TestDontHaveTimeoutRepeatedAddPending(t *testing.T) { ...@@ -200,13 +200,13 @@ func TestDontHaveTimeoutRepeatedAddPending(t *testing.T) {
latency := time.Millisecond * 5 latency := time.Millisecond * 5
latMultiplier := 1 latMultiplier := 1
expProcessTime := time.Duration(0) expProcessTime := time.Duration(0)
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
tr := timeoutRecorder{} tr := timeoutRecorder{}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
dontHaveTimeout, latMultiplier, expProcessTime) dontHaveTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys repeatedly // Add keys repeatedly
for _, c := range ks { for _, c := range ks {
...@@ -230,12 +230,12 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfPingError(t *testing.T) { ...@@ -230,12 +230,12 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfPingError(t *testing.T) {
defaultTimeout := 10 * time.Millisecond defaultTimeout := 10 * time.Millisecond
expectedTimeout := expProcessTime + defaultTimeout expectedTimeout := expProcessTime + defaultTimeout
tr := timeoutRecorder{} tr := timeoutRecorder{}
ctx := context.Background()
pc := &mockPeerConn{latency: latency, err: fmt.Errorf("ping error")} pc := &mockPeerConn{latency: latency, err: fmt.Errorf("ping error")}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
defaultTimeout, latMultiplier, expProcessTime) defaultTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys // Add keys
dhtm.AddPending(ks) dhtm.AddPending(ks)
...@@ -264,12 +264,12 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfLatencyLonger(t *testing.T) { ...@@ -264,12 +264,12 @@ func TestDontHaveTimeoutMgrUsesDefaultTimeoutIfLatencyLonger(t *testing.T) {
expProcessTime := time.Duration(0) expProcessTime := time.Duration(0)
defaultTimeout := 10 * time.Millisecond defaultTimeout := 10 * time.Millisecond
tr := timeoutRecorder{} tr := timeoutRecorder{}
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
defaultTimeout, latMultiplier, expProcessTime) defaultTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys // Add keys
dhtm.AddPending(ks) dhtm.AddPending(ks)
...@@ -297,12 +297,12 @@ func TestDontHaveTimeoutNoTimeoutAfterShutdown(t *testing.T) { ...@@ -297,12 +297,12 @@ func TestDontHaveTimeoutNoTimeoutAfterShutdown(t *testing.T) {
latMultiplier := 1 latMultiplier := 1
expProcessTime := time.Duration(0) expProcessTime := time.Duration(0)
tr := timeoutRecorder{} tr := timeoutRecorder{}
ctx := context.Background()
pc := &mockPeerConn{latency: latency} pc := &mockPeerConn{latency: latency}
dhtm := newDontHaveTimeoutMgrWithParams(ctx, pc, tr.onTimeout, dhtm := newDontHaveTimeoutMgrWithParams(pc, tr.onTimeout,
dontHaveTimeout, latMultiplier, expProcessTime) dontHaveTimeout, latMultiplier, expProcessTime)
dhtm.Start() dhtm.Start()
defer dhtm.Shutdown()
// Add keys // Add keys
dhtm.AddPending(ks) dhtm.AddPending(ks)
......
...@@ -154,7 +154,7 @@ func New(ctx context.Context, p peer.ID, network MessageNetwork, onDontHaveTimeo ...@@ -154,7 +154,7 @@ func New(ctx context.Context, p peer.ID, network MessageNetwork, onDontHaveTimeo
log.Infow("Bitswap: timeout waiting for blocks", "cids", ks, "peer", p) log.Infow("Bitswap: timeout waiting for blocks", "cids", ks, "peer", p)
onDontHaveTimeout(p, ks) onDontHaveTimeout(p, ks)
} }
dhTimeoutMgr := newDontHaveTimeoutMgr(ctx, newPeerConnection(p, network), onTimeout) dhTimeoutMgr := newDontHaveTimeoutMgr(newPeerConnection(p, network), onTimeout)
return newMessageQueue(ctx, p, network, maxMessageSize, sendErrorBackoff, dhTimeoutMgr) return newMessageQueue(ctx, p, network, maxMessageSize, sendErrorBackoff, dhTimeoutMgr)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment