forked from raiden-network/raiden
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run matrix client / room listener callbacks in separate greenlets
Also add workaround for matrix-org/matrix-python-sdk#193
- Loading branch information
Showing
4 changed files
with
120 additions
and
10 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from matrix_client.room import Room as MatrixRoom | ||
|
||
from raiden.network.matrix.utils import _geventify_callback | ||
|
||
|
||
class Room(MatrixRoom): | ||
def add_listener(self, callback, event_type=None): | ||
return super().add_listener(_geventify_callback(callback), event_type) | ||
|
||
def add_ephemeral_listener(self, callback, event_type=None): | ||
return super().add_ephemeral_listener(_geventify_callback(callback), event_type) | ||
|
||
def add_state_listener(self, callback, event_type=None): | ||
super().add_state_listener(_geventify_callback(callback), event_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
|
||
import gevent | ||
from requests.adapters import HTTPAdapter | ||
|
||
|
||
def _geventify_callback(callback): | ||
def inner(*args, **kwargs): | ||
gevent.spawn(callback, *args, **kwargs) | ||
|
||
return inner | ||
|
||
|
||
class Fix429HTTPAdapter(HTTPAdapter): | ||
""" Temporary workaround for https://github.com/matrix-org/matrix-python-sdk/issues/193 """ | ||
|
||
_fallback_retry_timeout = 1000 | ||
|
||
def build_response(self, req, resp): | ||
response = super().build_response(req, resp) | ||
if response.status_code == 429: | ||
resp_json = response.json() | ||
if 'retry_after_ms' not in resp_json: | ||
if 'error' in resp_json: | ||
try: | ||
error = json.loads(resp_json['error']) | ||
resp_json['retry_after_ms'] = error.get('retry_after_ms', | ||
self._fallback_retry_timeout) | ||
except json.JSONDecodeError: | ||
resp_json['retry_after_ms'] = self._fallback_retry_timeout | ||
else: | ||
resp_json['retry_after_ms'] = self._fallback_retry_timeout | ||
|
||
response._content = json.dumps(resp_json).encode( | ||
response.encoding or response.apparent_encoding) | ||
print("Fixing response json to ", response._content) | ||
return response |