This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package swarm | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"time" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
mafmt "github.com/whyrusleeping/mafmt" | ||
) | ||
|
||
const p_circuit = 290 | ||
|
||
var relay = mafmt.Or(mafmt.Base(p_circuit), mafmt.And(mafmt.IPFS, mafmt.Base(p_circuit))) | ||
|
||
// delayDialAddrs returns a address channel sorted by priority, pushing the | ||
// addresses with delay between them. The other channel can be used to trigger | ||
// sending more addresses in case all previous failed | ||
func delayDialAddrs(ctx context.Context, c <-chan ma.Multiaddr) (<-chan ma.Multiaddr, chan<- struct{}) { | ||
type delayAddr struct { | ||
maddr ma.Multiaddr | ||
delay time.Duration | ||
} | ||
|
||
gatherAddrs := func(c <-chan ma.Multiaddr) []ma.Multiaddr { | ||
addrs := make([]ma.Multiaddr, 0, len(c)) | ||
for { | ||
select { | ||
case a, ok := <-c: | ||
if !ok { | ||
return addrs | ||
} | ||
addrs = append(addrs, a) | ||
default: //NOTE: This may need to be switched to a tiny timeout at some point | ||
return addrs | ||
} | ||
} | ||
} | ||
|
||
sortTiered := func(addrs []ma.Multiaddr) []*delayAddr { | ||
delayed := make([]*delayAddr, len(addrs)) | ||
sort.Slice(addrs, func(i, j int) bool { | ||
return dialOffset(addrs[i]) < dialOffset(addrs[j]) | ||
}) | ||
|
||
if len(addrs) == 0 { | ||
return nil | ||
} | ||
|
||
lastOffset := dialOffset(addrs[0]) | ||
for i, a := range addrs { | ||
offset := dialOffset(a) | ||
delayed[i] = &delayAddr{ | ||
maddr: a, | ||
delay: offset - lastOffset, | ||
} | ||
lastOffset = offset | ||
} | ||
|
||
return delayed | ||
} | ||
|
||
out := make(chan ma.Multiaddr) | ||
triggerNext := make(chan struct{}, 1) | ||
|
||
go func() { | ||
defer close(out) | ||
|
||
for { | ||
addrs := make([]ma.Multiaddr, 0, 1) | ||
select { | ||
case a, ok := <-c: | ||
if !ok { | ||
return | ||
} | ||
addrs = append(addrs, a) | ||
} | ||
|
||
addrs = append(addrs, gatherAddrs(c)...) | ||
delayed := sortTiered(addrs) | ||
|
||
for _, a := range delayed { | ||
if a.delay > 0 { | ||
after := time.After(a.delay) | ||
select { | ||
case <-triggerNext: | ||
case <-after: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
select { | ||
case out <- a.maddr: | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return out, triggerNext | ||
} | ||
|
||
// dialOffset returns the maximum amount of time dialing to the given address | ||
// can be delayed | ||
func dialOffset(addr ma.Multiaddr) time.Duration { | ||
switch { | ||
case relay.Matches(addr): | ||
return 2000 * time.Millisecond //TODO: adjust based on something | ||
default: | ||
return 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters