Skip to content

Commit db50f5f

Browse files
authored
fix: increase dial timeout (#16040)
Increase default dial timeout for 500ms to 1s and also make it configurable through an env var and the update-checker
1 parent dc0c456 commit db50f5f

9 files changed

Lines changed: 53 additions & 6 deletions

File tree

yarn-project/foundation/src/config/env_var.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export type EnvVar =
123123
| 'P2P_PEER_PENALTY_VALUES'
124124
| 'P2P_QUERY_FOR_IP'
125125
| 'P2P_REQRESP_INDIVIDUAL_REQUEST_TIMEOUT_MS'
126+
| 'P2P_REQRESP_DIAL_TIMEOUT_MS'
126127
| 'P2P_REQRESP_OVERALL_REQUEST_TIMEOUT_MS'
127128
| 'P2P_DISABLE_STATUS_HANDSHAKE'
128129
| 'P2P_ALLOW_ONLY_VALIDATORS'

yarn-project/p2p/src/client/p2p_client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export class P2PClient<T extends P2PClientType = P2PClientType.Full>
154154

155155
public updateP2PConfig(config: Partial<P2PConfig>): Promise<void> {
156156
this.txPool.updateConfig(config);
157+
this.p2pService.updateConfig(config);
157158
return Promise.resolve();
158159
}
159160

yarn-project/p2p/src/services/dummy_service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { PeerId } from '@libp2p/interface';
77
import EventEmitter from 'events';
88

99
import type { PeerManagerInterface } from './peer-manager/interface.js';
10+
import type { P2PReqRespConfig } from './reqresp/config.js';
1011
import { type AuthRequest, StatusMessage } from './reqresp/index.js';
1112
import type {
1213
ReqRespInterface,
@@ -30,6 +31,8 @@ import {
3031
* A dummy implementation of the P2P Service.
3132
*/
3233
export class DummyP2PService implements P2PService {
34+
updateConfig(_config: Partial<P2PReqRespConfig>): void {}
35+
3336
/** Returns an empty array for peers. */
3437
getPeers(): PeerInfo[] {
3538
return [];
@@ -217,6 +220,7 @@ export class DummyPeerManager implements PeerManagerInterface {
217220
}
218221

219222
export class DummyReqResp implements ReqRespInterface {
223+
updateConfig(_config: Partial<P2PReqRespConfig>): void {}
220224
start(
221225
_subProtocolHandlers: ReqRespSubProtocolHandlers,
222226
_subProtocolValidators: ReqRespSubProtocolValidators,

yarn-project/p2p/src/services/libp2p/libp2p_service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import { gossipScoreThresholds } from '../gossipsub/scoring.js';
6565
import type { PeerManagerInterface } from '../peer-manager/interface.js';
6666
import { PeerManager } from '../peer-manager/peer_manager.js';
6767
import { PeerScoring } from '../peer-manager/peer_scoring.js';
68+
import type { P2PReqRespConfig } from '../reqresp/config.js';
6869
import {
6970
DEFAULT_SUB_PROTOCOL_VALIDATORS,
7071
type ReqRespInterface,
@@ -173,6 +174,10 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
173174
};
174175
}
175176

177+
public updateConfig(config: Partial<P2PReqRespConfig>) {
178+
this.reqresp.updateConfig(config);
179+
}
180+
176181
/**
177182
* Creates an instance of the LibP2P service.
178183
* @param config - The configuration to use when creating the service.

yarn-project/p2p/src/services/reqresp/config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { type ConfigMapping, booleanConfigHelper, numberConfigHelper } from '@az
22

33
export const DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS = 2000;
44
export const DEFAULT_OVERALL_REQUEST_TIMEOUT_MS = 4000;
5+
export const DEFAULT_REQRESP_DIAL_TIMEOUT_MS = 1000;
56
export const DEFAULT_OPTIMISTIC_NEGOTIATION = false;
67

78
// For use in tests.
89
export const DEFAULT_P2P_REQRESP_CONFIG: P2PReqRespConfig = {
910
overallRequestTimeoutMs: DEFAULT_OVERALL_REQUEST_TIMEOUT_MS,
1011
individualRequestTimeoutMs: DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS,
12+
dialTimeoutMs: DEFAULT_REQRESP_DIAL_TIMEOUT_MS,
1113
p2pOptimisticNegotiation: DEFAULT_OPTIMISTIC_NEGOTIATION,
1214
};
1315

@@ -20,6 +22,9 @@ export interface P2PReqRespConfig {
2022

2123
/** Whether to use optimistic protocol negotiation when dialing to another peer (opposite of `negotiateFully`). */
2224
p2pOptimisticNegotiation: boolean;
25+
26+
/** How long to wait for the dial protocol to establish a connection */
27+
dialTimeoutMs: number;
2328
}
2429

2530
export const p2pReqRespConfigMappings: Record<keyof P2PReqRespConfig, ConfigMapping> = {
@@ -33,6 +38,11 @@ export const p2pReqRespConfigMappings: Record<keyof P2PReqRespConfig, ConfigMapp
3338
description: 'The timeout for an individual request response peer interaction.',
3439
...numberConfigHelper(DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS),
3540
},
41+
dialTimeoutMs: {
42+
env: 'P2P_REQRESP_DIAL_TIMEOUT_MS',
43+
description: 'How long to wait for the dial protocol to establish a connection',
44+
...numberConfigHelper(DEFAULT_REQRESP_DIAL_TIMEOUT_MS),
45+
},
3646
p2pOptimisticNegotiation: {
3747
env: 'P2P_REQRESP_OPTIMISTIC_NEGOTIATION',
3848
description:

yarn-project/p2p/src/services/reqresp/interface.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TxArray, TxHashArray } from '@aztec/stdlib/tx';
44

55
import type { PeerId } from '@libp2p/interface';
66

7+
import type { P2PReqRespConfig } from './config.js';
78
import { AuthRequest, AuthResponse } from './protocols/auth.js';
89
import { BlockTxsRequest, BlockTxsResponse } from './protocols/block_txs/block_txs_reqresp.js';
910
import { StatusMessage } from './protocols/status.js';
@@ -255,4 +256,6 @@ export interface ReqRespInterface {
255256
payload: Buffer,
256257
dialTimeout?: number,
257258
): Promise<ReqRespResponse>;
259+
260+
updateConfig(config: Partial<P2PReqRespConfig>): void;
258261
}

yarn-project/p2p/src/services/reqresp/reqresp.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import {
1818
} from '../../errors/reqresp.error.js';
1919
import { SnappyTransform } from '../encoding.js';
2020
import type { PeerScoring } from '../peer-manager/peer_scoring.js';
21-
import type { P2PReqRespConfig } from './config.js';
21+
import {
22+
DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS,
23+
DEFAULT_OVERALL_REQUEST_TIMEOUT_MS,
24+
DEFAULT_REQRESP_DIAL_TIMEOUT_MS,
25+
type P2PReqRespConfig,
26+
} from './config.js';
2227
import { BatchConnectionSampler } from './connection-sampler/batch_connection_sampler.js';
2328
import { ConnectionSampler, RandomSampler } from './connection-sampler/connection_sampler.js';
2429
import {
@@ -57,8 +62,9 @@ import { ReqRespStatus, ReqRespStatusError, parseStatusChunk, prettyPrintReqResp
5762
* see: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#the-reqresp-domain
5863
*/
5964
export class ReqResp implements ReqRespInterface {
60-
private overallRequestTimeoutMs: number;
61-
private individualRequestTimeoutMs: number;
65+
private overallRequestTimeoutMs: number = DEFAULT_OVERALL_REQUEST_TIMEOUT_MS;
66+
private individualRequestTimeoutMs: number = DEFAULT_INDIVIDUAL_REQUEST_TIMEOUT_MS;
67+
private dialTimeoutMs: number = DEFAULT_REQRESP_DIAL_TIMEOUT_MS;
6268

6369
// Warning, if the `start` function is not called as the parent class constructor, then the default sub protocol handlers will be used ( not good )
6470
private subProtocolHandlers: ReqRespSubProtocolHandlers = DEFAULT_SUB_PROTOCOL_HANDLERS;
@@ -79,8 +85,7 @@ export class ReqResp implements ReqRespInterface {
7985
rateLimits: Partial<ReqRespSubProtocolRateLimits> = {},
8086
telemetryClient: TelemetryClient = getTelemetryClient(),
8187
) {
82-
this.overallRequestTimeoutMs = config.overallRequestTimeoutMs;
83-
this.individualRequestTimeoutMs = config.individualRequestTimeoutMs;
88+
this.updateConfig(config);
8489

8590
this.rateLimiter = new RequestResponseRateLimiter(peerScoring, rateLimits);
8691

@@ -96,6 +101,20 @@ export class ReqResp implements ReqRespInterface {
96101
this.metrics = new ReqRespMetrics(telemetryClient);
97102
}
98103

104+
public updateConfig(config: Partial<P2PReqRespConfig>): void {
105+
if (typeof config.overallRequestTimeoutMs === 'number') {
106+
this.overallRequestTimeoutMs = config.overallRequestTimeoutMs;
107+
}
108+
109+
if (typeof config.individualRequestTimeoutMs === 'number') {
110+
this.individualRequestTimeoutMs = config.individualRequestTimeoutMs;
111+
}
112+
113+
if (typeof config.dialTimeoutMs === 'number') {
114+
this.dialTimeoutMs = config.dialTimeoutMs;
115+
}
116+
}
117+
99118
get tracer() {
100119
return this.metrics.tracer;
101120
}
@@ -372,7 +391,7 @@ export class ReqResp implements ReqRespInterface {
372391
peerId: PeerId,
373392
subProtocol: ReqRespSubProtocol,
374393
payload: Buffer,
375-
dialTimeout: number = 500,
394+
dialTimeout: number = this.dialTimeoutMs,
376395
): Promise<ReqRespResponse> {
377396
let stream: Stream | undefined;
378397
try {

yarn-project/p2p/src/services/service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ENR } from '@chainsafe/enr';
66
import type { PeerId } from '@libp2p/interface';
77
import type EventEmitter from 'events';
88

9+
import type { P2PReqRespConfig } from './reqresp/config.js';
910
import type { StatusMessage } from './reqresp/index.js';
1011
import type {
1112
ReqRespSubProtocol,
@@ -81,6 +82,8 @@ export interface P2PService {
8182
): Promise<void>;
8283

8384
handleAuthRequestFromPeer(authRequest: AuthRequest, peerId: PeerId): Promise<StatusMessage>;
85+
86+
updateConfig(config: Partial<P2PReqRespConfig>): void;
8487
}
8588

8689
/**

yarn-project/p2p/src/test-helpers/reqresp-nodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export const createReqResp = async (
242242
const config: P2PReqRespConfig = {
243243
overallRequestTimeoutMs: 4000,
244244
individualRequestTimeoutMs: 2000,
245+
dialTimeoutMs: 1000,
245246
p2pOptimisticNegotiation: false,
246247
};
247248
const req = new ReqResp(config, p2p, peerScoring, undefined, rateLimits);

0 commit comments

Comments
 (0)