Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 50b4d44

Browse files
committed
allow configuring source of random numbers for path selection
1 parent cd32b38 commit 50b4d44

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

client_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestRendezvous(t *testing.T) {
6060
defer torch.Stop()
6161

6262
// FIXME: implement threadsafe Pick in Torch
63-
dst := torch.Pick(weighRelayWith)
63+
dst := torch.Pick(weighRelayWith, nil)
6464

6565
cookie := make([]byte, 20)
6666
payload := make([]byte, 148)
@@ -145,7 +145,7 @@ func TestHandshakeFailure(t *testing.T) {
145145
return 1
146146
}
147147
return 0
148-
})
148+
}, nil)
149149

150150
tc1, _, err := torch.UnguardedCircuitTo(ctx, 1, n)
151151
if err != nil {

torch.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"compress/zlib"
55
"encoding/base64"
66
"fmt"
7-
"github.com/andres-erbsen/torch/config"
8-
"github.com/andres-erbsen/torch/directory"
7+
mathrand "math/rand"
98
"net"
109
"net/http"
1110
"strconv"
1211
"strings"
1312
"sync"
1413
"time"
1514

15+
"github.com/andres-erbsen/torch/config"
16+
"github.com/andres-erbsen/torch/directory"
17+
1618
"golang.org/x/net/context"
1719
"golang.org/x/net/proxy"
1820
)
@@ -193,13 +195,13 @@ func (t *Torch) WithDirectory(f func(*directory.Directory) interface{}) interfac
193195
return f(t.cachedDir)
194196
}
195197

196-
func (t *Torch) Pick(weighWith func(w *directory.BandwidthWeights, n *directory.NodeInfo) int64) *directory.NodeInfo {
198+
func (t *Torch) Pick(weighWith func(w *directory.BandwidthWeights, n *directory.NodeInfo) int64, rnd *mathrand.Rand) *directory.NodeInfo {
197199
weigh := func(n *directory.NodeInfo) int64 {
198200
return weighWith(&t.cachedDir.Consensus.BandwidthWeights, n)
199201
}
200202

201203
return t.WithDirectory(func(d *directory.Directory) interface{} {
202-
return directory.Pick(weigh, d.Routers, nil)
204+
return directory.Pick(weigh, d.Routers, rnd)
203205
}).(*directory.NodeInfo)
204206

205207
}
@@ -217,7 +219,7 @@ func (t *Torch) UnguardedCircuitTo(ctx context.Context, n int, dst *directory.No
217219

218220
nodes := make([]*directory.NodeInfo, n)
219221
for i := 0; i <= n-2; i++ {
220-
nodes[i] = t.Pick(weighRelayWith)
222+
nodes[i] = t.Pick(weighRelayWith, nil)
221223
}
222224
nodes[n-1] = dst
223225

@@ -250,7 +252,7 @@ func BuildCircuit(ctx context.Context, dialer proxy.Dialer, nodes []*directory.N
250252
}
251253

252254
func (t *Torch) UnguardedExitCircuit(ctx context.Context, n int) (*TorConn, *Circuit, error) {
253-
return t.UnguardedCircuitTo(ctx, n, t.Pick(weighExitWith))
255+
return t.UnguardedCircuitTo(ctx, n, t.Pick(weighExitWith, nil))
254256
}
255257

256258
func (t *Torch) Stop() error {

0 commit comments

Comments
 (0)