From a99ccd57f1dad6490f230f695218f786768424ed Mon Sep 17 00:00:00 2001 From: manolodewiner Date: Fri, 31 Aug 2018 14:33:21 +0200 Subject: [PATCH] remove boolean comparison for numbers & add docs --- docs/source/usage.rst | 27 ++++++++++++++++++++++++++- src/connection.js | 4 ++-- src/transport.js | 4 ++-- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 1d119b1..44d823f 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -45,11 +45,36 @@ To do so, you need to pass the **app_id and app_key**. .. code-block:: js - let conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { + const conn = new driver.Connection('https://test.bigchaindb.com/api/v1/', { app_id: 'Get one from testnet.bigchaindb.com', app_key: 'Get one from testnet.bigchaindb.com' }) +A more complex connection can be created if the intention is to connect to +different nodes of a BigchainDB network. +The connection strategy will be the one specified in the BEP-14_ + +.. _BEP-14: https://github.com/bigchaindb/BEPs/tree/master/14#connection-strategy + +.. code-block:: js + + const conn = new driver.Connection([ + 'https://test.bigchaindb.com', // the first node does not use custom headers, only common headers + {endpoint: 'https://test.bigchaindb.com/api/v1/', + headers: {app_id: 'your_app_id', + app_key: 'your_app_key'}}, + {endpoint: 'https://test2.bigchaindb.com/api/v1/', + headers: {app_id: 'your_app_id', + app_key: 'your_app_key', + extra_header: 'extra value'}}, + {endpoint: 'https://test3.bigchaindb.com/api/v1/', + headers: {app_id: 'your_app_id', + app_key: 'your_app_key', + other_header: 'other value'}}, + {endpoint: 'https://test4.bigchaindb.com/api/v1/', + headers: {custom_auth: 'custom token'}], + {'Content-Type': 'application/json'}, // this header is used by all nodes) + Cryptographic Identities Generation ----------------------------------- Alice and Bob are represented by public/private key pairs. The private key is diff --git a/src/connection.js b/src/connection.js index d657ea6..90f09b5 100644 --- a/src/connection.js +++ b/src/connection.js @@ -19,7 +19,7 @@ const DEFAULT_TIMEOUT = 20000 // The default value is 20 seconds */ export default class Connection { - // 20 seconds is the default value for a timeout if not specified + // This driver implements the BEP-14 https://github.com/bigchaindb/BEPs/tree/master/14 constructor(nodes, headers = {}, timeout = DEFAULT_TIMEOUT) { // Copy object this.headers = Object.assign({}, headers) @@ -42,7 +42,7 @@ export default class Connection { this.normalizedNodes.push(Connection.normalizeNode(nodes, this.headers)) } - this.transport = new Transport(this.normalizedNodes, timeout) // TODO + this.transport = new Transport(this.normalizedNodes, timeout) } static normalizeNode(node, headers) { diff --git a/src/transport.js b/src/transport.js index 03eadca..2136595 100644 --- a/src/transport.js +++ b/src/transport.js @@ -42,7 +42,7 @@ export default class Transport { let response let connection // A new request will be executed until there is a valid response or timeout < 0 - while (!this.timeout || this.timeout > 0) { + while (this.timeout >= 0) { connection = this.pickConnection() // Date in milliseconds const startTime = Date.now() @@ -55,7 +55,7 @@ export default class Transport { this.maxBackoffTime ) const elapsed = Date.now() - startTime - if (connection.backoffTime && this.timeout) { + if (connection.backoffTime > 0 && this.timeout > 0) { this.timeout -= elapsed } else { // No connection error, the response is valid