Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 6fe0b58

Browse files
authored
FABN-1668 Add query timeout setting (#380)
Add a Query Timeout and not use the commit timeout Signed-off-by: Bret Harrison <beharrison@nc.rr.com>
1 parent a788705 commit 6fe0b58

File tree

9 files changed

+47
-12
lines changed

9 files changed

+47
-12
lines changed

docs/tutorials/query-peers.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ by default. This will evaluate all transactions on the first peer from
1616
which is can obtain a response, and only switch to another peer if this
1717
peer fails.
1818

19+
The default query timeout may be changed along with the query strategy.
20+
This value will be in seconds.
21+
1922
```javascript
2023
const { Gateway, DefaultQueryHandlerStrategies } = require('fabric-network');
2124

2225
const connectOptions = {
2326
queryHandlerOptions: {
27+
timeout: 60,
2428
strategy: DefaultQueryHandlerStrategies.MSPID_SCOPE_SINGLE
2529
}
2630
}

fabric-network/lib/contract.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ class Contract {
115115
return this.gateway.getOptions().eventHandlerOptions;
116116
}
117117

118+
/**
119+
* Get query handler options specified by the user when creating the gateway.
120+
* @private
121+
* @returns {Object} Query handler options.
122+
*/
123+
getQueryHandlerOptions() {
124+
return this.gateway.getOptions().queryHandlerOptions;
125+
}
126+
118127
/**
119128
* Create an object representing a specific invocation of a transaction
120129
* function implemented by this contract, and provides more control over

fabric-network/lib/gateway.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const logger = require('./logger').getLogger('Gateway');
8585

8686
/**
8787
* @typedef {Object} Gateway~DefaultQueryHandlerOptions
88-
* @memberof module:fabric-network
88+
* @memberof module:fabric-
89+
* @property {number} [timeout = 30] The timeout period in seconds to wait for the query operation to
90+
* complete.
8991
* @property {module:fabric-network.Gateway~QueryHandlerFactory} [strategy=MSPID_SCOPE_SINGLE] Query handling strategy
9092
* used to evaluate queries. The default is [MSPID_SCOPE_SINGLE]{@link module:fabric-network.DefaultQueryHandlerStrategies}.
9193
*/
@@ -147,6 +149,7 @@ class Gateway {
147149
// default options
148150
this.options = {
149151
queryHandlerOptions: {
152+
timeout: 30, // 30 seconds
150153
strategy: QueryStrategies.MSPID_SCOPE_SINGLE
151154
},
152155
eventHandlerOptions: {

fabric-network/lib/transaction.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,9 @@ class Transaction {
312312
const channel = this._contract.getNetwork().getChannel();
313313
const request = this._buildRequest(args);
314314

315-
const commitTimeout = this._contract.getEventHandlerOptions().commitTimeout * 1000; // in ms
316-
const timeout = this._contract.gateway.getClient().getConfigSetting('request-timeout', commitTimeout);
317-
if (timeout < commitTimeout) {
318-
request.request_timeout = commitTimeout;
319-
}
315+
// queryHandlerOption.timeout default 30 seconds
316+
let timeout = this._contract.getQueryHandlerOptions().timeout ? this._contract.getQueryHandlerOptions().timeout : 30;
317+
request.request_timeout = timeout * 1000; // in ms
320318

321319
const query = new Query(channel, request);
322320

fabric-network/test/contract.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ describe('Contract', () => {
152152
});
153153
});
154154

155+
describe('#getQuertHandlerOptions', () => {
156+
it('returns query handler options from the gateway', () => {
157+
const result = contract.getQueryHandlerOptions();
158+
const expected = mockGateway.getOptions().queryHandlerOptions;
159+
result.should.deep.equal(expected);
160+
});
161+
});
162+
155163
describe('#createTransaction', () => {
156164
it('returns a transaction with only a name', () => {
157165
const name = 'name';

fabric-network/test/gateway.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ describe('Gateway', () => {
139139
commitTimeout: 300, // 5 minutes
140140
strategy: strategy
141141
},
142+
queryHandlerOptions: {
143+
timeout: 70,
144+
strategy: strategy
145+
},
142146
discovery: {
143147
enabled: true,
144148
asLocalhost: true
@@ -157,6 +161,10 @@ describe('Gateway', () => {
157161
commitTimeout: 300, // 5 minutes
158162
strategy: strategy
159163
},
164+
queryHandlerOptions: {
165+
timeout: 70,
166+
strategy: strategy
167+
},
160168
discovery: {
161169
enabled: true,
162170
asLocalhost: false
@@ -353,6 +361,7 @@ describe('Gateway', () => {
353361
wallet: mockWallet,
354362
identity: 'admin',
355363
queryHandlerOptions: {
364+
timeout: 30,
356365
strategy: QueryStrategies.MSPID_SCOPE_SINGLE
357366
}
358367
};

fabric-network/test/network.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ describe('Network', () => {
9595
strategy: EventStrategies.MSPID_SCOPE_ALLFORTX
9696
},
9797
queryHandlerOptions: {
98+
timeout: 30,
9899
strategy: (theNetwork) => {
99100
stubQueryHandler.network = theNetwork;
100101
return stubQueryHandler;

fabric-network/test/transaction.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('Transaction', () => {
9898

9999
stubContract.getChaincodeId.returns(chaincodeId);
100100
stubContract.getEventHandlerOptions.returns({commitTimeout: 418});
101+
stubContract.getQueryHandlerOptions.returns({timeout: 30});
101102

102103
const mockClient = sinon.createStubInstance(Client);
103104
const mockGateway = sinon.createStubInstance(Gateway);
@@ -433,8 +434,8 @@ describe('Transaction', () => {
433434
return expect(promise).to.be.rejectedWith('Transaction has already been invoked');
434435
});
435436

436-
it('builds correct request for invocation with long timeout', async () => {
437-
stubContract.getEventHandlerOptions.returns({commitTimeout: 999});
437+
it('builds correct request for invocation with non default timeout', async () => {
438+
stubContract.getQueryHandlerOptions.returns({timeout: 999});
438439

439440
await transaction.evaluate();
440441

@@ -444,13 +445,14 @@ describe('Transaction', () => {
444445
});
445446
});
446447

447-
it('builds correct request for invocation with short timeout', async () => {
448-
stubContract.getEventHandlerOptions.returns({commitTimeout: 3});
449-
448+
it('builds correct request for invocation with default timeout', async () => {
449+
stubContract.getQueryHandlerOptions.returns({});
450450
await transaction.evaluate();
451451

452452
const query = stubQueryHandler.evaluate.lastArg;
453-
expect(query._request.request_timeout).to.be.undefined;
453+
expect(query._request).to.deep.include({
454+
request_timeout: 30000
455+
});
454456
});
455457

456458
});

fabric-network/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface TxEventHandler {
7171
}
7272

7373
export interface DefaultQueryHandlerOptions {
74+
timeout?: number;
7475
strategy?: QueryHandlerFactory;
7576
}
7677

0 commit comments

Comments
 (0)