Skip to content

Commit f419ca9

Browse files
committed
improve peer rotation
1 parent 8899662 commit f419ca9

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

connection/connectionPool.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,23 @@ void peerWatchdog(ConnectionPool& conns_)
236236
}
237237
}
238238
}
239-
239+
std::random_device rd;
240+
std::mt19937 gen(rd());
241+
std::uniform_int_distribution<int> dist(0, N - 1);
240242
// if there is no worst, randomly pick 1
241243
if (!worst) {
242244
if (N > 0) {
243-
std::random_device rd;
244-
std::mt19937 gen(rd());
245-
std::uniform_int_distribution<int> dist(0, N - 1);
246245
int randomIdx = dist(gen);
247246
conns_.get(randomIdx, worst);
248247
}
249248
}
250249
if (worst) {
251250
std::vector<std::string> newPeer;
251+
std::string mode = dist(gen) % 2 == 0 ? "closest" : "random";
252252
if (worst->isBM()) {
253-
newPeer = GetPeerFromDNS(1, 0, "closest");
253+
newPeer = GetPeerFromDNS(1, 0, mode);
254254
} else {
255-
newPeer = GetPeerFromDNS(0, 1, "random");
255+
newPeer = GetPeerFromDNS(0, 1, mode);
256256
}
257257
ParsedEndpoint parsed;
258258
if (!newPeer.empty() && parseEndpoint(newPeer[0], parsed)) {

0 commit comments

Comments
 (0)