|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import sys |
| 5 | +import time |
| 6 | + |
| 7 | +from tarantool.connection import Connection |
| 8 | +from tarantool.const import ( |
| 9 | + SOCKET_TIMEOUT, |
| 10 | + RECONNECT_MAX_ATTEMPTS, |
| 11 | + RECONNECT_DELAY, |
| 12 | +) |
| 13 | + |
| 14 | +from tarantool.error import ( |
| 15 | + Error, |
| 16 | + DatabaseError, |
| 17 | + NetworkError, |
| 18 | + NetworkWarning, |
| 19 | +) |
| 20 | + |
| 21 | +from tarantool.schema import ( |
| 22 | + Schema, |
| 23 | + SchemaError |
| 24 | +) |
| 25 | + |
| 26 | +from tarantool.utils import ( |
| 27 | + ENCODING_DEFAULT |
| 28 | +) |
| 29 | + |
| 30 | +class MeshConnection(Connection): |
| 31 | + def __init__(self, addrs, user, password, connect_now, encoding): |
| 32 | + self.addrs = addrs |
| 33 | + self.nattempts = 2 * len(addrs) + 1 |
| 34 | + self.pos = 0 |
| 35 | + host = addrs[self.pos]['host'] |
| 36 | + port = addrs[self.pos]['port'] |
| 37 | + super(MeshConnection, self).__init__(host=host, |
| 38 | + port=port, |
| 39 | + user=user, |
| 40 | + password=password, |
| 41 | + connect_now=connect_now, |
| 42 | + encoding=encoding) |
| 43 | + |
| 44 | + def _opt_reconnect(self): |
| 45 | + ntries = self.nattempts |
| 46 | + print "QQQ MeshConnection:_opt_reconnect", ntries, self.pos, len(self.addrs) |
| 47 | + while ntries > 0: |
| 48 | + try: |
| 49 | + super(MeshConnection, self)._opt_reconnect() |
| 50 | + print "MeshConnection: success" |
| 51 | + break |
| 52 | + except NetworkError: |
| 53 | + print "MeshConnection: gocha!" |
| 54 | + ntries -= 1 |
| 55 | + self.pos = (self.pos + 1) % len(self.addrs) |
| 56 | + self.host = self.addrs[self.pos]['host'] |
| 57 | + self.port = self.addrs[self.pos]['port'] |
| 58 | + else: |
| 59 | + print "MeshConnection: failure" |
| 60 | + raise NetworkError |
| 61 | + print "MeshConnection: reconnect done" |
| 62 | + |
| 63 | +def connectmesh(addrs=({'host': 'localhost', 'port': 3301},), user=None, |
| 64 | + password=None, connect_now=True, encoding=ENCODING_DEFAULT): |
| 65 | + ''' |
| 66 | + Create a connection to the Tarantool server. |
| 67 | +
|
| 68 | + :param str host: Server hostname or IP-address |
| 69 | + :param int port: Server port |
| 70 | +
|
| 71 | + :rtype: :class:`~tarantool.connection.Connection` |
| 72 | +
|
| 73 | + :raise: `NetworkError` |
| 74 | + ''' |
| 75 | + |
| 76 | + return MeshConnection(addrs=addrs, |
| 77 | + user=user, |
| 78 | + password=password, |
| 79 | + connect_now=connect_now, |
| 80 | + encoding=encoding) |
| 81 | + |
| 82 | +addrs=( |
| 83 | + {'host': 'localhost', 'port': 3301}, |
| 84 | + {'host': 'localhost', 'port': 3304}, |
| 85 | +) |
| 86 | + |
| 87 | +con = connectmesh(addrs) |
| 88 | + |
| 89 | +while True: |
| 90 | + try: |
| 91 | + print con.ping() |
| 92 | + except NetworkError: |
| 93 | + print 'NetworkError !' |
| 94 | + break |
| 95 | + except Exception as e: |
| 96 | + print e |
| 97 | + time.sleep(1) |
0 commit comments