Refactoring: typing, comments-into-names changes.

Signed-off-by: Denis Gladkiy <denis.gladkiy@hardcoreeng.com>
This commit is contained in:
Denis Gladkiy
2025-10-06 18:55:16 +06:00
parent 42eee03403
commit 81481fda5a
3 changed files with 14 additions and 14 deletions
@@ -976,9 +976,9 @@ describe('backrpc', () => {
)
// Test that request limit can be configured
expect(server.requestsLimit).toBe(25) // Default value
server.requestsLimit = 10 // Set custom limit
expect(server.requestsLimit).toBe(10)
expect(server.requestsLimitPerClient).toBe(25) // Default value
server.requestsLimitPerClient = 10 // Set custom limit
expect(server.requestsLimitPerClient).toBe(10)
const client = new BackRPCClient(
'client1' as ClientId,
+10 -10
View File
@@ -40,7 +40,7 @@ interface RPCClientInfo<ClientT extends string> {
requestsTotal: number
requestsTime: number
helloCounter: number
aliveTimeout: number // Per-client timeout in seconds
perClientAliveTimeoutSeconds: number
}
export class BackRPCServer<ClientT extends string = ClientId> {
@@ -59,8 +59,7 @@ export class BackRPCServer<ClientT extends string = ClientId> {
private bound: Promise<void> | undefined
// A limit of requests per one client.
requestsLimit: number = 25
requestsLimitPerClient: number = 25
stopTick?: () => void
@@ -68,7 +67,7 @@ export class BackRPCServer<ClientT extends string = ClientId> {
private readonly handlers: BackRPCServerHandler<ClientT>,
private readonly tickMgr: TickManager,
readonly host: string = '*',
private readonly port: number = 0,
private readonly port: number | 'random' = 'random',
private readonly options: zmq.SocketOptions<zmq.Router> = {}
) {
this.router = new zmq.Router({
@@ -96,9 +95,9 @@ export class BackRPCServer<ClientT extends string = ClientId> {
const timeSinceLastSeen = now - clientRecord.lastSeen
// Use per-client timeout instead of global timeout
if (timeSinceLastSeen > clientRecord.aliveTimeout * 1000) {
if (timeSinceLastSeen > clientRecord.perClientAliveTimeoutSeconds * 1000) {
console.warn(
`Client ${clientId} has been inactive for ${Math.round(timeSinceLastSeen / 1000)}s (timeout: ${clientRecord.aliveTimeout}s), marking as dead`
`Client ${clientId} has been inactive for ${Math.round(timeSinceLastSeen / 1000)}s (timeout: ${clientRecord.perClientAliveTimeoutSeconds}s), marking as dead`
)
this.handleClose(clientRecord.id, clientId, true)
}
@@ -156,7 +155,8 @@ export class BackRPCServer<ClientT extends string = ClientId> {
}
private async start (): Promise<void> {
this.bound = this.router.bind(`tcp://${this.host}:${this.port}`)
const port = this.port === 'random' ? 0 : this.port
this.bound = this.router.bind(`tcp://${this.host}:${port}`)
await this.bound
// Read messages from clients.
@@ -200,10 +200,10 @@ export class BackRPCServer<ClientT extends string = ClientId> {
requestsTime: 0,
requestsTotal: 0,
helloCounter: 0,
aliveTimeout: clientAliveTimeout
perClientAliveTimeoutSeconds: clientAliveTimeout
} satisfies RPCClientInfo<ClientT>)
clientInfo.helloCounter++
clientInfo.aliveTimeout = clientAliveTimeout // Update timeout on reconnection
clientInfo.perClientAliveTimeoutSeconds = clientAliveTimeout // Update timeout on reconnection
this.revClientMapping.set(clientIdText, clientInfo)
void this.doSend([clientId, backrpcOperations.hello, this.uuid, ''])
@@ -243,7 +243,7 @@ export class BackRPCServer<ClientT extends string = ClientId> {
// We already had this request, so continue waiting for response.
continue
}
if (client.requests.size > this.requestsLimit) {
if (client.requests.size > this.requestsLimitPerClient) {
// No Client, requests are not possible
void this.doSend([clientId, backrpcOperations.retry, reqId, stringifyJSON(client.requests.size)])
continue
+1 -1
View File
@@ -30,7 +30,7 @@ export class NetworkAgentServer implements BackRPCServerHandler<ClientUuid> {
tickMgr: TickManager,
readonly endpointHost: string, // An endpoint construction host, will be used to register
host: string = '*', // A socket visibility
port: number = 3738 // If 0, port will be free random one.
port: number | 'random' = 3738
) {
this.rpcServer = new BackRPCServer<ClientUuid>(this, tickMgr, host, port)
}