Skip to content

Commit

Permalink
Properly initialize WebSocketsClient
Browse files Browse the repository at this point in the history
WebSocketsClient json-rpc client should be initialized only once per
session, not per request.
  • Loading branch information
bitphage committed May 20, 2019
1 parent a9f72b8 commit ccc0f9c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions grapheneasync/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(self, url, *args, **kwargs):
self.api_id = {}
self._request_id = 0
self.url = url
self.loop = kwargs.get('loop')

def __getattr__(self, name):
""" Map all methods to RPC calls and pass through the arguments
Expand Down
8 changes: 5 additions & 3 deletions grapheneasync/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import json

from jsonrpcclient.clients.websockets_client import WebSocketsClient
from grapheneasync.rpc import Rpc

from .rpc import Rpc

log = logging.getLogger(__name__)

Expand All @@ -13,10 +14,11 @@ class Websocket(Rpc):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ws = None
self.loop = kwargs.get('loop')
self.client = None

async def connect(self):
self.ws = await websockets.connect(self.url, ssl=True, loop=self.loop)
self.client = WebSocketsClient(self.ws)

async def disconnect(self):
await self.ws.close()
Expand All @@ -32,7 +34,7 @@ async def rpcexec(self, *args):

log.debug(json.dumps(args))

response = await WebSocketsClient(self.ws).request('call', *args)
response = await self.client.request('call', *args)

# Return raw response (jsonrpcclient does own parsing)
return response.text

0 comments on commit ccc0f9c

Please sign in to comment.