Skip to content

Commit

Permalink
[api] add parameter num_retries
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Jun 29, 2016
1 parent 0e9bd98 commit 731f4d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ steem-readme:
piston edit "@xeroc/python-graphenelib-readme" --file README.md

steem-changelog:
git tag -l -n100 $(TAG) | piston post --author xeroc --permlink "python-graphenelib-changelog-$(TAGSTEEM)" --category steem --title "[Changelog] python-graphenelib $(TAG)" --file "-"
git tag -l -n100 $(TAG) | piston post --author xeroc --permlink "python-graphenelib-changelog-$(TAGSTEEM)" --category graphene --title "[Changelog] python-graphenelib $(TAG)" --file "-"
35 changes: 31 additions & 4 deletions grapheneapi/graphenewsrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class RPCError(Exception):
pass


class NumRetriesReached(Exception):
pass


class GrapheneWebsocketRPC(object):
""" This class allows to call API methods synchronously, without
callbacks. It logs in and registers to the APIs:
Expand All @@ -21,6 +25,7 @@ class GrapheneWebsocketRPC(object):
:param str user: Username for Authentication
:param str password: Password for Authentication
:param Array apis: List of APIs to register to (default: ["database", "network_broadcast"])
:param int num_tries: Try x times to num_tries to a node on disconnect, -1 for indefinitely
Available APIs
Expand All @@ -41,12 +46,14 @@ class GrapheneWebsocketRPC(object):
subsystem, please use ``GrapheneWebsocket`` instead.
"""
def __init__(self, url, user="", password=""):
def __init__(self, url, user="", password="", **kwargs):
self.api_id = {}
self._request_id = 0
self.url = url
self.user = user
self.password = password
self.num_tries = kwargs.get("num_tries", -1)

self.wsconnect()
self.register_apis()

Expand All @@ -55,14 +62,23 @@ def get_request_id(self):
return self._request_id

def wsconnect(self):
cnt = 0
while True:
cnt += 1
try:
self.ws = create_connection(self.url)
break
except KeyboardInterrupt:
break
except:
log.warning("Lost connection to node: %s. Retrying in 10 seconds" % self.url)
if (self.num_tries >= 0 and cnt > self.num_tries):
raise NumRetriesReached()

log.warning(
"Lost connection to node: %s (%d/%d) "
% (self.url, cnt, self.num_tries) +
"Retrying in 10 seconds"
)
time.sleep(10)
self.login(self.user, self.password, api_id=1)

Expand Down Expand Up @@ -284,14 +300,25 @@ def rpcexec(self, payload):
"""
try:
log.debug(json.dumps(payload))
cnt = 0
while True:
cnt += 1

try:
self.ws.send(json.dumps(payload))
ret = json.loads(self.ws.recv())
break
except KeyboardInterrupt:
break
except:
log.warning("Cannot connect to WS node: %s" % self.url)
# retry after reconnect
if (self.num_tries > 0 and cnt > self.num_tries):
raise NumRetriesReached()

log.warning(
"Cannot connect to WS node: %s (%d/%d)"
% (self.url, cnt, self.num_tries)
)
# retry
try:
self.ws.close()
self.wsconnect()
Expand Down

0 comments on commit 731f4d3

Please sign in to comment.