Skip to content

Commit

Permalink
rapide: remove random source bookkeeping and use mrand.Int
Browse files Browse the repository at this point in the history
This fallsback to fastrand in go1.21 and is well fast enough.
  • Loading branch information
Jorropo committed Mar 23, 2023
1 parent 751763d commit c1184d1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 1 addition & 3 deletions rapide/rapide.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
mrand "math/rand"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -51,9 +50,8 @@ func (c *Client) Get(ctx context.Context, root cid.Cid, traversal ipsl.Traversal
errors: make([]error, len(c.ServerDrivenDownloaders)),
}

seedRand := mrand.New(mrand.NewSource(mrand.Int63()))
for i, sdd := range c.ServerDrivenDownloaders {
d.startServerDrivenWorker(ctx, sdd, &d.root, &d.errors[i], seedRand.Int63()^int64(i))
d.startServerDrivenWorker(ctx, sdd, &d.root, &d.errors[i])
}

return out
Expand Down
8 changes: 3 additions & 5 deletions rapide/serverdriven.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ type serverDrivenWorker struct {
outErr *error
current *node
tasks map[cid.Cid]*node
rand mrand.Rand

// TODO: add a dontGoThere map which tells you what part of the dag this node is not able to handle
}

func (d *download) startServerDrivenWorker(ctx context.Context, impl ServerDrivenDownloader, root *node, outErr *error, seed int64) {
func (d *download) startServerDrivenWorker(ctx context.Context, impl ServerDrivenDownloader, root *node, outErr *error) {
w := &serverDrivenWorker{
impl: impl,
download: d,
outErr: outErr,
current: root,
tasks: make(map[cid.Cid]*node),
rand: *mrand.New(mrand.NewSource(seed)),
}

go w.work(ctx)
Expand Down Expand Up @@ -224,9 +222,9 @@ func (w *serverDrivenWorker) compareChildWithMinimums(child *node, minWorkers ui
// if scores are identical randomly select other nodes to randomly distribute where downloads are placed
if luck == 0 {
// lazy initialisation of luck, this allows to creating a random value when better values exists back to back
luck = uint(w.rand.Int())
luck = uint(mrand.Int())
}
newLuck := uint(w.rand.Int())
newLuck := uint(mrand.Int())
if newLuck >= luck {
break
}
Expand Down

0 comments on commit c1184d1

Please sign in to comment.