Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/stellar/stellar.rpc-timeout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { ConfigService } from '@nestjs/config';
import { Keypair } from '@stellar/stellar-sdk';
import { createServer } from 'node:http';
import type { AddressInfo } from 'node:net';
import { TEST_CONTRACT_ID } from '../../test/helpers/stellar-harness';
import { StellarService } from './stellar.service';

describe('StellarService RPC timeout', () => {
it('times out a stalled HTTP response using the configured milliseconds', async () => {
let method: string | undefined;
const http = createServer((request, response) => {
method = request.method;
const fallback = setTimeout(() => response.writeHead(503).end(), 3_000);
response.once('close', () => clearTimeout(fallback));
});
await new Promise<void>((resolve, reject) => {
http.once('error', reject);
http.listen(0, '127.0.0.1', resolve);
});

try {
const { port } = http.address() as AddressInfo;
const service = new StellarService(
new ConfigService({
SOROBAN_RPC_URL: `http://127.0.0.1:${port}`,
SOROBAN_RPC_TIMEOUT_MS: 500,
STELLAR_NETWORK: 'testnet',
TICKETING_CONTRACT_ID: TEST_CONTRACT_ID,
PLATFORM_SIGNER_SECRET: Keypair.random().secret(),
}),
);

const { up, latencyMs } = await service.pingRpc();
expect(method).toBe('POST');
expect(up).toBe(false);
expect(latencyMs).toBeLessThan(2_500);
} finally {
await new Promise<void>((resolve) => {
http.close(() => resolve());
http.closeAllConnections();
});
}
});
});
3 changes: 2 additions & 1 deletion src/stellar/stellar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ export class StellarService {
DEFAULT_RPC_TIMEOUT_MS;
this.server = new rpc.Server(rpcUrl, {
allowHttp: rpcUrl.startsWith('http://'),
timeout: rpcTimeoutMs,
});
// SDK 16.2 declares a server timeout but does not forward it to HTTP.
this.server.httpClient.defaults.timeout = rpcTimeoutMs;
this.contractId = this.config.getOrThrow<string>('TICKETING_CONTRACT_ID');
this.contract = new Contract(this.contractId);
this.networkPassphrase = NETWORK_PASSPHRASES[network];
Expand Down