aiogremlin.driver.server.GremlinServer.__init__()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 24
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 24
rs 9.328
c 0
b 0
f 0
cc 2
nop 4
1
import ssl
2
3
from aiogremlin.driver import pool
4
5
6
class GremlinServer:
7
    """
8
    Class that wraps a connection pool. Currently doesn't do much, but may
9
    be useful in the future....
10
11
    :param pool.ConnectionPool pool:
12
    """
13
14
    def __init__(self, url, loop, **config):
15
        self._pool = None
16
        self._url = url
17
        self._loop = loop
18
        self._response_timeout = config['response_timeout']
19
        self._username = config['username']
20
        self._password = config['password']
21
        self._max_times_acquired = config['max_times_acquired']
22
        self._max_conns = config['max_conns']
23
        self._min_conns = config['min_conns']
24
        self._max_inflight = config['max_inflight']
25
        self._message_serializer = config['message_serializer']
26
        self._provider = config['provider']
27
        scheme = config['scheme']
28
        if scheme in ['https', 'wss']:
29
            certfile = config['ssl_certfile']
30
            keyfile = config['ssl_keyfile']
31
            ssl_password = config['ssl_password']
32
            ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
33
            ssl_context.load_cert_chain(
34
                certfile, keyfile=keyfile, password=ssl_password)
35
            self._ssl_context = ssl_context
36
        else:
37
            self._ssl_context = None
38
39
    @property
40
    def url(self):
41
        return self._url
42
43
    @property
44
    def pool(self):
45
        """
46
        Readonly property.
47
48
        :returns: :py:class:`ConnectionPool<aiogremlin.driver.pool.ConnectionPool>`
49
        """
50
        if self._pool:
51
            return self._pool
52
53
    async def close(self):
54
        """**coroutine** Close underlying connection pool."""
55
        if self._pool:
56
            await self._pool.close()
57
            self._pool = None
58
59
    async def get_connection(self):
60
        """**coroutine** Acquire a connection from the pool."""
61
        try:
62
            conn = await self._pool.acquire()
63
        except AttributeError:
64
            raise Exception("Please initialize pool")
65
        return conn
66
67
    async def initialize(self):
68
        conn_pool = pool.ConnectionPool(
69
            self._url, self._loop, self._ssl_context, self._username,
70
            self._password, self._max_conns, self._min_conns,
71
            self._max_times_acquired, self._max_inflight,
72
            self._response_timeout, self._message_serializer, self._provider)
73
        await conn_pool.init_pool()
74
        self._pool = conn_pool
75
76
    @classmethod
77
    async def open(cls, url, loop, **config):
78
        """
79
        **coroutine** Establish connection pool and host to Gremlin Server.
80
81
        :param str url: url for host Gremlin Server
82
        :param asyncio.BaseEventLoop loop:
83
        :param ssl.SSLContext ssl_context:
84
        :param str username: Username for database auth
85
        :param str password: Password for database auth
86
        :param float response_timeout: (optional) `None` by default
87
        :param int max_conns: Maximum number of conns to a host
88
        :param int min_connsd: Minimum number of conns to a host
89
        :param int max_times_acquired: Maximum number of times a conn can be
90
            shared by multiple coroutines (clients)
91
        :param int max_inflight: Maximum number of unprocessed requests at any
92
            one time on the connection
93
94
        :returns: :py:class:`GremlinServer`
95
        """
96
97
        host = cls(url, loop, **config)
98
        await host.initialize()
99
        return host
100