Skip to content

Commit

Permalink
feat(web): add packets in flight limit option to STREAM toy
Browse files Browse the repository at this point in the history
  • Loading branch information
justmoon committed Sep 9, 2024
1 parent 7b81950 commit 95141b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface StreamConfiguration {
amount: bigint
maxPacketAmount: bigint
latency: number
maxPacketsInFlight: number
}

interface StreamConfigurationProperties {
Expand All @@ -22,6 +23,7 @@ export const DEFAULT_STREAM_CONFIGURATION: StreamConfiguration = {
amount: 3000n,
maxPacketAmount: 1000n,
latency: 100,
maxPacketsInFlight: 10,
}

export default function StreamConfigurator({
Expand Down Expand Up @@ -59,6 +61,20 @@ export default function StreamConfigurator({
}))
}
/>
<Label htmlFor="max_packets_in_flight">
Max. # of Packets in Flight
</Label>
<Input
type="number"
id="max_packets_in_flight"
value={String(configuration.maxPacketsInFlight)}
onChange={(event) =>
onConfigurationChange((configuration) => ({
...configuration,
maxPacketsInFlight: Number(event.target.value),
}))
}
/>
</div>
</div>
<div className="flex flex-col gap-4 border p-4 rounded-md">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export default function StreamSimulator({
const environment = createTestEnvironment({
maxPacketAmount: configuration.maxPacketAmount,
latency: configuration.latency,
maxPacketsInFlight: configuration.maxPacketsInFlight,

scope,
logger: createLogger("network", { context: logContext }),
})
Expand Down
22 changes: 21 additions & 1 deletion packages/lib-protocol-stream/src/test/mocks/test-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface EnvironmentOptions {
// Environment options
maxPacketAmount?: bigint | undefined
latency?: number | undefined
maxPacketsInFlight?: number | undefined

// Override context
scope?: DisposableScope | undefined
Expand Down Expand Up @@ -63,6 +64,7 @@ interface ResponsePacketEvent {
export function createTestEnvironment({
maxPacketAmount = UINT64_MAX,
latency = 0,
maxPacketsInFlight = Infinity,
scope = createScope("test-environment"),
logger = createLogger("das:test:stream"),
crypto = createMockCryptoContext(),
Expand All @@ -80,6 +82,8 @@ export function createTestEnvironment({
createContext: ({ name }: ContextOptions): StreamProtocolContext => {
const address = `test.${name}`

let packetsInFlight = 0

async function processPacket(packet: IlpPreparePacket) {
if (packet.destination.startsWith(address)) {
throw new Error("Packet addressed to self")
Expand All @@ -105,6 +109,18 @@ export function createTestEnvironment({
await new Promise((resolve) => setTimeout(resolve, latency))
}

if (packetsInFlight >= maxPacketsInFlight) {
return {
type: IlpType.Reject,
data: {
code: IlpErrorCode.T03_CONNECTOR_BUSY,
message: "Too many packets in flight",
triggeredBy: "test.router",
data: new Uint8Array(),
},
}
}

if (packet.amount > maxPacketAmount) {
return {
type: IlpType.Reject,
Expand All @@ -125,7 +141,11 @@ export function createTestEnvironment({
logger.debug?.("routing packet to destination", {
destination: packet.destination,
})
return route.handler(packet)

packetsInFlight++
return route.handler(packet).finally(() => {
packetsInFlight--
})
}
}

Expand Down

0 comments on commit 95141b7

Please sign in to comment.