Skip to content

Conversation

@voith
Copy link
Contributor

@voith voith commented Jun 20, 2018

What was wrong?

The current implementation of WebsocketProvider did not allow configuring extra configurations that a websockets connection takes
fixes #913

How was it fixed?

Allowed passing websockets connection kwargs

Cute Animal Picture

@voith
Copy link
Contributor Author

voith commented Jun 20, 2018

In case this PR needs a test case then:

import pytest
from websockets.exceptions import ConnectionClosed
from web3 import Web3

w3 = Web3(Web3.WebsocketProvider(max_size=1))
with pytest.raises(ConnectionClosed):
    w3.eth.getBlock(0)

However, I'm not sure if web3 should testing functionality that should be tested by websockets!

Copy link
Contributor

@carver carver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like having the example case you commented with. It provides an example usage, and makes sure the API doesn't change without an explicit test change.

_loop = None

def __init__(self, endpoint_uri=None):
def __init__(self, endpoint_uri=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not wild about letting the websockets connection parameters dictate the whole keyword argument namespace here. How about adding a single dict variable, like connection_params={'max_size': 1}?

That way there is no chance of a future namespace conflict or confusing mismatch. (we already have one mismatch: a value is called uri in websockets, but endpoint_uri here).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose the name websocket_kwargs instead, to be consistent with request_kwargs in HttpProvider.

class PersistentWebSocket:

def __init__(self, endpoint_uri, loop):
def __init__(self, endpoint_uri, loop, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here with the kwargs.

async def __aenter__(self):
if self.ws is None:
self.ws = await websockets.connect(uri=self.endpoint_uri, loop=self.loop)
self.ws = await websockets.connect(uri=self.endpoint_uri, loop=self.loop, **self.kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe guard against people passing in loop or uri as one of the connection_params here.

@voith voith force-pushed the allow-passing-websocket-kwargs branch from 02ccadc to b675e5c Compare June 21, 2018 10:11
@voith voith force-pushed the allow-passing-websocket-kwargs branch from b675e5c to ca4a336 Compare June 21, 2018 10:30
Copy link
Contributor

@carver carver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@carver carver merged commit 55b5f5e into ethereum:master Jun 21, 2018
@voith voith deleted the allow-passing-websocket-kwargs branch June 21, 2018 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Erorr in websockets.py: '<=' not supported between instances of 'int' and 'NoneType'

2 participants