async def test_room_data_is_stored(room_store: RoomStore, rate_limiter: RateLimiter) -> None: gss_one = GameStateServer(room_store, rate_limiter, NoopRateLimiter()) responses = await collect_responses( gss_one, requests=[ Request( request_id='first-request-id', actions=[VALID_ACTION, ANOTHER_VALID_ACTION], ) ], response_count=2, ) assert responses == [ ConnectionResponse([]), UpdateResponse([VALID_ACTION, ANOTHER_VALID_ACTION], 'first-request-id'), ] gss_two = GameStateServer(room_store, rate_limiter, NoopRateLimiter()) responses = await collect_responses(gss_two, requests=[], response_count=1) assert responses == [ ConnectionResponse([VALID_TOKEN, ANOTHER_VALID_TOKEN]) ]
def main(): websocket_port = int(os.environ['PORT']) room_store_dir = os.environ['ROOM_STORE_DIR'] room_store = FileRoomStore(room_store_dir) gss = GameStateServer(room_store) signal.signal(signal.SIGTERM, lambda *_: gss.save_all()) start_websocket(websocket_port, gss)
async def app() -> Starlette: room_store = MemoryRoomStore(MemoryRoomStorage()) rate_limiter = MemoryRateLimiter( 'server-id', MemoryRateLimiterStorage(), ) gss = GameStateServer(room_store, rate_limiter, NoopRateLimiter()) ws = WebsocketManager(gss, rate_limiter, TEST_BYPASS_RATE_LIMIT_KEY) return Starlette(routes=routes(ws), debug=True)
async def collect_responses( gss: GameStateServer, requests: List[Request], response_count: int, room_id: str = TEST_ROOM_ID, ) -> List[Response]: disconnect_event = asyncio.Event() try: responses = await async_collect( gss.handle_connection( room_id, '127.0.0.1', to_async_until(requests, disconnect_event), ), response_count, ) return responses finally: disconnect_event.set()
async def make_app() -> Starlette: worker_id = str(uuid4()) timber.context(server={'server_id': server_id, 'worker_id': worker_id}) redis = await create_redis_pool(config.redis_address, config.redis_ssl_validation) room_store = await create_redis_room_store(redis) rate_limiter = await create_redis_rate_limiter(server_id, redis) compactor = Compactor(room_store, worker_id) gss = GameStateServer(room_store, rate_limiter, NoopRateLimiter()) ws = WebsocketManager(gss, rate_limiter, config.bypass_rate_limit_key) def liveness_failed(fut: Future) -> NoReturn: logger.critical('Maintain liveness task failed, shutting down', exc_info=fut.exception()) sys.exit(1) liveness_task = asyncio.create_task(ws.maintain_liveness(), name='maintain_liveness') liveness_task.add_done_callback(liveness_failed) def compaction_failed(fut: Future) -> NoReturn: logger.critical('Compaction task failed, shutting down', exc_info=fut.exception()) sys.exit(1) compaction_task = asyncio.create_task(compactor.maintain_compaction(), name='maintain_compaction') compaction_task.add_done_callback(compaction_failed) async def shutdown() -> None: redis.close() await redis.wait_closed() return Starlette( routes=routes(ws), on_shutdown=[shutdown], debug=config.environment == Environment.DEV, )
def gss_with_client(): rs = MemoryRoomStore('/my/path/to/room/storage/') gss = GameStateServer(rs) gss.new_connection_request(TEST_CLIENT_ID, TEST_ROOM_ID) return gss
def gss(): rs = MemoryRoomStore('/my/path/to/room/storage/') return GameStateServer(rs)
def gss(room_store: RoomStore, rate_limiter: RateLimiter) -> GameStateServer: return GameStateServer(room_store, rate_limiter, NoopRateLimiter())