Skip to content

Commit b0de415

Browse files
committed
pause the stream
1 parent 0187a89 commit b0de415

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/components/P2P/handleProtocolCommands.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
checkGlobalConnectionsRateLimit,
1313
checkRequestsRateLimit
1414
} from '../../utils/validators.js'
15-
import type { Stream } from '@libp2p/interface'
15+
import type { Connection, Stream } from '@libp2p/interface'
1616

1717
export class ReadableString extends Readable {
1818
private sent = false
@@ -31,9 +31,12 @@ export class ReadableString extends Readable {
3131
}
3232
}
3333

34-
export async function handleProtocolCommands(stream: Stream, connection: any) {
34+
export async function handleProtocolCommands(stream: Stream, connection: Connection) {
3535
const { remotePeer, remoteAddr } = connection
3636

37+
// Pause the stream. We do async operations here before writing.
38+
stream.pause()
39+
3740
P2P_LOGGER.logMessage('Incoming connection from peer ' + remotePeer, true)
3841
P2P_LOGGER.logMessage('Using ' + remoteAddr, true)
3942

@@ -61,7 +64,7 @@ export async function handleProtocolCommands(stream: Stream, connection: any) {
6164
}
6265

6366
const now = Date.now()
64-
const rateLimitCheck = checkRequestsRateLimit(remoteAddr, configuration, now)
67+
const rateLimitCheck = checkRequestsRateLimit(remoteAddr.toString(), configuration, now)
6568
if (!rateLimitCheck.valid) {
6669
P2P_LOGGER.warn(
6770
`Incoming request denied to peer: ${remotePeer} (rate limit exceeded)`
@@ -79,6 +82,9 @@ export async function handleProtocolCommands(stream: Stream, connection: any) {
7982
return
8083
}
8184

85+
// Resume the stream. We can now write.
86+
stream.resume()
87+
8288
// v3 streams are AsyncIterable
8389
let task: Command
8490
try {

0 commit comments

Comments
 (0)