Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
dial delay: address review
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jun 8, 2018
1 parent 928ffbc commit a945f49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
15 changes: 8 additions & 7 deletions dial_delay.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const p_circuit = 290

const numTiers = 2

var tierDelay = 2 * time.Second
var TierDelay = 1 * time.Second

var relay = mafmt.Or(mafmt.And(mafmt.Base(p_circuit), mafmt.Base(ma.P_IPFS)), mafmt.And(mafmt.Base(ma.P_IPFS), mafmt.Base(p_circuit), mafmt.Base(ma.P_IPFS)))

Expand All @@ -21,7 +21,7 @@ var relay = mafmt.Or(mafmt.And(mafmt.Base(p_circuit), mafmt.Base(ma.P_IPFS)), ma
// sending more addresses in case all previous failed
func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multiaddr, chan<- struct{}) {
out := make(chan ma.Multiaddr)
delay := time.NewTimer(tierDelay)
delay := time.NewTimer(TierDelay)
triggerNext := make(chan struct{}, 1)

go func() {
Expand All @@ -30,13 +30,14 @@ func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multi
var pending [numTiers][]ma.Multiaddr
lastTier := -1

// put enqueues the mutliaddr
// put enqueues the multiaddr
put := func(addr ma.Multiaddr) {
tier := getTier(addr)
pending[tier] = append(pending[tier], addr)
}

// get gets the best (lowest tier) multiaddr available
// note that within a single tier put/get behave like a stack (LIFO)
get := func() (ma.Multiaddr, int) {
for i, tier := range pending[:] {
if len(tier) > 0 {
Expand Down Expand Up @@ -64,7 +65,7 @@ func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multi
}
}

// waitForMore woits for addresses from the channel
// waitForMore waits for addresses from the channel
waitForMore := func() (bool, error) {
select {
case addr, ok := <-c:
Expand Down Expand Up @@ -93,12 +94,12 @@ func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multi
put(addr)
return true, false, nil
case <-delay.C:
delay.Reset(tierDelay)
delay.Reset(TierDelay)
case <-triggerNext:
if !delay.Stop() {
<-delay.C
}
delay.Reset(tierDelay)
delay.Reset(TierDelay)
case <-ctx.Done():
return false, false, ctx.Err()
}
Expand All @@ -123,7 +124,7 @@ func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multi
if !delay.Stop() {
<-delay.C
}
delay.Reset(tierDelay)
delay.Reset(TierDelay)
case <-ctx.Done():
return false, ctx.Err()
}
Expand Down
4 changes: 2 additions & 2 deletions dial_delay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func prepare() {
}
_ = ma.AddProtocol(circuitProto)

tierDelay = 32 * time.Millisecond // 2x windows timer resolution
TierDelay = 32 * time.Millisecond // 2x windows timer resolution
}

// addrChan creates a multiaddr channel with `nsync` size. If nsync is larger
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestDelayMoreT1T0(t *testing.T) {
func TestDelaySingleT0T1WaitT0(t *testing.T) {
ctx := context.Background()
prepare()
tierDelay = 64 * time.Millisecond // 4x windows timer resolution
TierDelay = 64 * time.Millisecond // 4x windows timer resolution

start := time.Now()
ch, _ := delayDialAddrs(ctx, addrChan(t, 2, T0_A, T1_A, "16ms", T0_B))
Expand Down

0 comments on commit a945f49

Please sign in to comment.