Skip to content

Commit e470e27

Browse files
dariusstefanrazvancrainea
authored andcommitted
add stop mechanism for bad connection
1 parent 5f7f6c6 commit e470e27

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

docs/event.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ except OpenSIPSEventException as e:
4444
# handle the exception
4545
```
4646

47+
If `callback` function is called with `None` as a parameter, it means that there was an error while receiving the event and no JSON object could be parsed from the received data after 10 retries.
48+
4749
## Subscribing
4850

4951
By default, the subscription will be permanent. If you want to set a timeout, you can use the `expires` parameter. The value should be an integer representing the number of seconds the subscription will be active.

opensips/event/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ def main():
107107

108108
def event_handler(message):
109109
""" Event handler callback """
110+
if message is None:
111+
ev.unsubscribe()
112+
sys.exit(1)
113+
110114
try:
111115
print(json.dumps(message, indent=4))
112116
except json.JSONDecodeError as e:

opensips/event/event.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __init__(self, handler, name: str, callback, expire=None):
4040
self.thread_stop.clear()
4141
self.buf = b""
4242
self.json_queue = []
43+
self.retries = 0
4344

4445
try:
4546
self.socket = self._handler.__new_socket__()
@@ -63,7 +64,16 @@ def handle(self, callback):
6364

6465
self.buf += data
6566
self.json_queue, self.buf = extract_json(self.json_queue, self.buf)
67+
68+
if not self.json_queue:
69+
self.retries += 1
70+
71+
if self.retries > 10:
72+
callback(None)
73+
break
74+
6675
while self.json_queue:
76+
self.retries = 0
6777
json_obj = self.json_queue.pop(0)
6878
callback(json_obj)
6979

0 commit comments

Comments
 (0)