Skip to content

Commit e1423b8

Browse files
committed
Add very partial sharding
1 parent 50d8735 commit e1423b8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

clamor/gateway/connector.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ def __init__(self, url: str, **kwargs):
7373
self._running = False
7474
self._tg = None
7575

76+
# Sharding
77+
self.shard_id = kwargs.get('shard_id')
78+
self.shard_count = kwargs.get('shard_count')
79+
7680
# Heartbeat stuff
7781
self._interval = 0
7882
self._last_sequence = None
@@ -177,6 +181,8 @@ async def _identify(self):
177181
'compress': True, # i guess?
178182
'large_threshold': 250,
179183
}
184+
if self.shard_id and self.shard_count:
185+
identify['shard'] = [self.shard_id, self.shard_count]
180186
await self._send('IDENTIFY', identify)
181187

182188
async def on_open(self):

tests/test_gateway_sharding.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import unittest
4+
import os
5+
6+
import anyio
7+
8+
from clamor import gateway, HTTP, Routes
9+
10+
11+
class GatewayTests(unittest.TestCase):
12+
def test_gateway_connect(self):
13+
async def main():
14+
http = HTTP(os.environ['TEST_BOT_TOKEN'])
15+
url = await http.make_request(Routes.GET_GATEWAY)
16+
17+
gw_one = gateway.DiscordWebsocketClient(url['url'], shard_id=0, shard_count=2)
18+
gw_two = gateway.DiscordWebsocketClient(url['url'], shard_id=1, shard_count=2)
19+
20+
self.assertIsInstance(gw_one, gateway.DiscordWebsocketClient)
21+
self.assertIsInstance(gw_two, gateway.DiscordWebsocketClient)
22+
23+
async def stop_gatways(after):
24+
await anyio.sleep(after)
25+
await gw_one.close()
26+
await gw_two.close()
27+
28+
async with anyio.create_task_group() as tg:
29+
await tg.spawn(gw_one.start, os.environ['TEST_BOT_TOKEN'])
30+
await tg.spawn(gw_two.start, os.environ['TEST_BOT_TOKEN'])
31+
await tg.spawn(stop_gatways, 10)
32+
33+
anyio.run(main)

0 commit comments

Comments
 (0)