From d72b15680ccb819f9d0ef177547aa2b19065f045 Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Thu, 8 Aug 2013 18:06:36 +0200 Subject: [PATCH] Switch to urllib3 for more performance --- elasticsearch/connection.py | 35 +++++++++++++++++++++++++++++++++++ elasticsearch/transport.py | 4 ++-- requirements.txt | 1 + setup.py | 1 + 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/elasticsearch/connection.py b/elasticsearch/connection.py index 7aaa0ce5..dda925b0 100644 --- a/elasticsearch/connection.py +++ b/elasticsearch/connection.py @@ -2,6 +2,11 @@ import logging import time import requests import json +import urllib3 +try: + from urllib import urlencode +except ImportError: + from urllib.parse import urlencode from .exceptions import TransportError, HTTP_EXCEPTIONS, ConnectionError @@ -113,3 +118,33 @@ class RequestsHttpConnection(Connection): return response.status_code, raw_data +class Urllib3HttpConnection(Connection): + def __init__(self, host='localhost', port=9200, **kwargs): + super(Urllib3HttpConnection, self).__init__(host=host, port=port, **kwargs) + self.pool = urllib3.HTTPConnectionPool(host, port=port) + + def perform_request(self, method, url, params=None, body=None): + url = self.url_prefix + url + if params: + url = '%s?%s' % (url, urlencode(params or {})) + full_url = self.host + url + + start = time.time() + try: + response = self.pool.urlopen(method, url, body) + duration = time.time() - start + raw_data = response.data.decode('utf-8') + except Exception as e: + self.log_request_fail(method, full_url, time.time() - start, exception=e) + raise ConnectionError('N/A', str(e), e) + + if not (200 <= response.status < 300): + self.log_request_fail(method, url, duration, response.status) + self._raise_error(response.status, raw_data) + + self.log_request_success(method, full_url, url, body, response.status, + raw_data, duration) + + return response.status, raw_data + + diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index c790f803..b6f5adef 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -1,7 +1,7 @@ import re import time -from .connection import RequestsHttpConnection +from .connection import Urllib3HttpConnection from .connection_pool import ConnectionPool from .serializer import JSONSerializer from .exceptions import ConnectionError, TransportError, SerializationError @@ -32,7 +32,7 @@ class Transport(object): Main interface is the `perform_request` method. """ - def __init__(self, hosts, connection_class=RequestsHttpConnection, + def __init__(self, hosts, connection_class=Urllib3HttpConnection, connection_pool_class=ConnectionPool, host_info_callback=get_host_info, sniff_on_start=False, sniffer_timeout=None, sniff_on_connection_fail=False, serializer=JSONSerializer(), diff --git a/requirements.txt b/requirements.txt index f2293605..804abb1b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ requests +urllib3 diff --git a/setup.py b/setup.py index d9c37e8d..96b19fa4 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ f.close() install_requires = [ 'requests', + 'urllib3', ] tests_require = [ 'nose',