From 8bed4a85ec7583bfd06b898c1ecec107e0284fc3 Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 5 Jan 2016 10:04:24 -0700 Subject: [PATCH 1/3] [config][connection] allow overwriting connections agents --- docs/configuration.asciidoc | 19 +++++++++++++++++++ .../templates/configuration_docs.tmpl | 19 +++++++++++++++++++ src/lib/connectors/http.js | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index 0cbecef4f..df5774975 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -343,3 +343,22 @@ var client = new elasticsearch.Client({ } }) ----- + + + +`createNodeAgent`[[config-createNodeAgent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. ++ +The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. + +Default::: `HttpConnector#createAgent()` + +Disable Agent creation::: ++ +[source,js] +----- +var client = new elasticsearch.Client({ + createNodeAgent: function () { + return false; + } +}); +----- diff --git a/scripts/generate/templates/configuration_docs.tmpl b/scripts/generate/templates/configuration_docs.tmpl index bfa56b471..acd992f63 100644 --- a/scripts/generate/templates/configuration_docs.tmpl +++ b/scripts/generate/templates/configuration_docs.tmpl @@ -334,3 +334,22 @@ var client = new elasticsearch.Client({ } }) ----- + + + +`createNodeAgent`[[config-createNodeAgent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. ++ +The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. + +Default::: `HttpConnector#createAgent()` + +Disable Agent creation::: ++ +[source,js] +----- +var client = new elasticsearch.Client({ + createNodeAgent: function () { + return false; + } +}); +----- diff --git a/src/lib/connectors/http.js b/src/lib/connectors/http.js index 7d972a1cd..9feca60ae 100644 --- a/src/lib/connectors/http.js +++ b/src/lib/connectors/http.js @@ -44,7 +44,7 @@ function HttpConnector(host, config) { maxSockets: 11 }); - this.agent = this.createAgent(config); + this.agent = config.createNodeAgent ? config.createNodeAgent(this, config) : this.createAgent(config); } _.inherits(HttpConnector, ConnectionAbstract); From 04dfa9deef77201960fc596db2799eb7c9e0bd78 Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 5 Jan 2016 10:09:13 -0700 Subject: [PATCH 2/3] add some tests to make sure the createNodeAgent config works --- test/unit/specs/http_connector.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/unit/specs/http_connector.js b/test/unit/specs/http_connector.js index 1a9b3c9e0..c9ee86f2b 100644 --- a/test/unit/specs/http_connector.js +++ b/test/unit/specs/http_connector.js @@ -64,6 +64,17 @@ describe('Http Connector', function () { var con = new HttpConnection(new Host('thrifty://es.com/stuff')); }).to.throwError(/invalid protocol/i); }); + + it('allows defining a custom agent', function () { + var football = {}; + var con = new HttpConnection(new Host(), { createNodeAgent: _.constant(football) }); + expect(con.agent).to.be(football); + }); + + it('allows setting agent to false', function () { + var con = new HttpConnection(new Host(), { createNodeAgent: _.constant(false) }); + expect(con.agent).to.be(false); + }); }); describe('#makeReqParams', function () { From 809f545dee261547a0849cba6c83ca8c5e351918 Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 5 Jan 2016 10:12:43 -0700 Subject: [PATCH 3/3] [config/docs] move createNodeAgent section up --- docs/configuration.asciidoc | 40 ++++++++++--------- .../templates/configuration_docs.tmpl | 40 ++++++++++--------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index df5774975..0041730c4 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -242,6 +242,27 @@ see https://github.com/elasticsearch/elasticsearch-js/blob/master/src/lib/nodes_ + +`createNodeAgent`[[config-create-node-agent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. ++ +The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. + +Default::: `HttpConnector#createAgent()` + +Disable Agent creation::: ++ +[source,js] +----- +var client = new elasticsearch.Client({ + createNodeAgent: function () { + return false; + } +}); +----- + + + + === Examples Connect to just a single seed node, and use sniffing to find the rest of the cluster. @@ -343,22 +364,3 @@ var client = new elasticsearch.Client({ } }) ----- - - - -`createNodeAgent`[[config-createNodeAgent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. -+ -The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. - -Default::: `HttpConnector#createAgent()` - -Disable Agent creation::: -+ -[source,js] ------ -var client = new elasticsearch.Client({ - createNodeAgent: function () { - return false; - } -}); ------ diff --git a/scripts/generate/templates/configuration_docs.tmpl b/scripts/generate/templates/configuration_docs.tmpl index acd992f63..f554c5cca 100644 --- a/scripts/generate/templates/configuration_docs.tmpl +++ b/scripts/generate/templates/configuration_docs.tmpl @@ -233,6 +233,27 @@ see https://github.com/elasticsearch/elasticsearch-js/blob/master/src/lib/nodes_ + +`createNodeAgent`[[config-create-node-agent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. ++ +The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. + +Default::: `HttpConnector#createAgent()` + +Disable Agent creation::: ++ +[source,js] +----- +var client = new elasticsearch.Client({ + createNodeAgent: function () { + return false; + } +}); +----- + + + + === Examples Connect to just a single seed node, and use sniffing to find the rest of the cluster. @@ -334,22 +355,3 @@ var client = new elasticsearch.Client({ } }) ----- - - - -`createNodeAgent`[[config-createNodeAgent]]:: `Function` -- Override the way that the client creates node.js `Agent`[https://nodejs.org/api/http.html#http_class_http_agent] objects. The value of this property will be executed every time a new Node is added to the client (either from the initial seed or from sniffing) and can return any value that node's http(s) module accepts as `agent:` configuration. -+ -The function is called with two arguments, first an `HttpConnector`[http://github.com/spalger/elasticsearch-js/blob/master/src/lib/connectors/http.js] object and the second the config object initially passed when creating the client. - -Default::: `HttpConnector#createAgent()` - -Disable Agent creation::: -+ -[source,js] ------ -var client = new elasticsearch.Client({ - createNodeAgent: function () { - return false; - } -}); ------