Files
opensearch-pyd/elasticsearch/connection/thrift.py
T

80 lines
2.8 KiB
Python
Raw Normal View History

2013-08-25 17:58:30 +02:00
from __future__ import absolute_import
2013-08-25 16:39:56 +02:00
import time
try:
from .esthrift import Rest
from .esthrift.ttypes import Method, RestRequest
2013-09-25 21:31:10 +02:00
from thrift.transport import TTransport, TSocket, TSSLSocket
2013-08-25 16:39:56 +02:00
from thrift.protocol import TBinaryProtocol
from thrift.Thrift import TException
THRIFT_AVAILABLE = True
except ImportError:
THRIFT_AVAILABLE = False
2013-08-25 18:01:45 +02:00
from ..exceptions import ConnectionError, ImproperlyConfigured
from .pooling import PoolingConnection
2013-08-25 16:39:56 +02:00
class ThriftConnection(PoolingConnection):
2013-09-24 16:58:37 +02:00
"""
Connection using the `thrift` protocol to communicate with elasticsearch.
See https://github.com/elasticsearch/elasticsearch-transport-thrift for additional info.
"""
2013-08-25 16:39:56 +02:00
transport_schema = 'thrift'
2013-09-25 21:31:10 +02:00
def __init__(self, host='localhost', port=9500, framed_transport=False, use_ssl=False, **kwargs):
2013-09-24 16:58:37 +02:00
"""
:arg framed_transport: use `TTransport.TFramedTransport` instead of
`TTransport.TBufferedTransport`
"""
2013-08-25 16:39:56 +02:00
if not THRIFT_AVAILABLE:
raise ImproperlyConfigured("Thrift is not available.")
super(ThriftConnection, self).__init__(host=host, port=port, **kwargs)
self._framed_transport = framed_transport
2013-09-25 21:31:10 +02:00
self._tsocket_class = TSocket.TSocket
if use_ssl:
self._tsocket_class = TSSLSocket.TSSLSocket
self._tsocket_args = (host, port)
def _make_connection(self):
2013-09-25 21:31:10 +02:00
socket = self._tsocket_class(*self._tsocket_args)
2013-08-25 16:39:56 +02:00
socket.setTimeout(self.timeout * 1000.0)
if self._framed_transport:
2013-08-25 16:39:56 +02:00
transport = TTransport.TFramedTransport(socket)
else:
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
client = Rest.Client(protocol)
transport.open()
return client
2013-08-25 16:39:56 +02:00
2013-09-25 23:09:50 +02:00
def perform_request(self, method, url, params=None, body=None, timeout=None, ignore=()):
2013-08-25 16:39:56 +02:00
request = RestRequest(method=Method._NAMES_TO_VALUES[method.upper()], uri=url,
parameters=params, body=body)
start = time.time()
tclient = self._get_connection()
2013-08-25 16:39:56 +02:00
try:
response = tclient.execute(request)
2013-08-25 16:39:56 +02:00
duration = time.time() - start
except TException as e:
self.log_request_fail(method, url, time.time() - start, exception=e)
raise ConnectionError('N/A', str(e), e)
finally:
self._release_connection(tclient)
2013-08-25 16:39:56 +02:00
2013-09-25 23:09:50 +02:00
if not (200 <= response.status < 300) and response.status not in ignore:
2013-08-25 16:39:56 +02:00
self.log_request_fail(method, url, duration, response.status)
self._raise_error(response.status, response.body)
self.log_request_success(method, url, url, body, response.status,
response.body, duration)
return response.status, response.body