allow to disable self.running_games as L1 Cache into Worker when useing Redis Backend
Build and Push Docker Container / build-and-push (push) Successful in 54s
Build and Push Docker Container / build-and-push (push) Successful in 54s
This commit is contained in:
+12
-6
@@ -21,7 +21,7 @@ class Server:
|
||||
'version': '1.0.0',
|
||||
}
|
||||
|
||||
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, game_state_backend:str='memory', game_state_redis_url:str='redis://localhost:6379/0', game_state_ttl_sec:int=900):
|
||||
def __init__(self, data_path:str, snake_type:str, storage_type:str, debug:bool=False, check_tls_security:bool=False, game_state_backend:str='memory', game_state_redis_url:str='redis://localhost:6379/0', game_state_ttl_sec:int=900, game_state_local_cache:bool=True):
|
||||
self.debug = debug
|
||||
self.snake_type = snake_type
|
||||
self.storage_type = storage_type
|
||||
@@ -31,6 +31,8 @@ class Server:
|
||||
self.check_tls_security = check_tls_security
|
||||
|
||||
self.store_game_state = False
|
||||
normalized_backend = (game_state_backend or 'memory').strip().lower()
|
||||
self.game_state_local_cache = (game_state_local_cache and normalized_backend != 'memory')
|
||||
self.game_state_store = GameBoardStoreBuilder.build(
|
||||
backend=game_state_backend,
|
||||
redis_url=game_state_redis_url,
|
||||
@@ -71,6 +73,7 @@ class Server:
|
||||
'last_game_end_unix': 0,
|
||||
'last_move_unix': 0,
|
||||
'games_stuck_removed': 0,
|
||||
'game_state_local_cache_enabled': bool(self.game_state_local_cache),
|
||||
}
|
||||
self.logger = build_logger('Battlesnake', debug_env_var='DEBUG_SERVER')
|
||||
self.snake_version = self._get_snake_version()
|
||||
@@ -241,6 +244,7 @@ class Server:
|
||||
)
|
||||
await new_game_board.start_game(game_state)
|
||||
|
||||
if self.game_state_local_cache:
|
||||
self.running_games[game_id] = new_game_board
|
||||
await self.game_state_store.save(game_id, new_game_board)
|
||||
self.game_move_counts[game_id] = 0
|
||||
@@ -248,12 +252,13 @@ class Server:
|
||||
self.metrics['games_started'] += 1
|
||||
self.metrics['active_games_peak'] = max(
|
||||
self.metrics['active_games_peak'],
|
||||
len(self.running_games),
|
||||
len(self.game_last_seen_unix),
|
||||
)
|
||||
self.metrics['last_game_start_unix'] = int(time.time())
|
||||
return new_game_board
|
||||
|
||||
async def _persist_game_board(self, game_id:str, game_board:GameBoard):
|
||||
if self.game_state_local_cache:
|
||||
self.running_games[game_id] = game_board
|
||||
await self.game_state_store.save(game_id, game_board)
|
||||
|
||||
@@ -267,12 +272,13 @@ class Server:
|
||||
async def _get_game_board(self, game_state:dict, end:bool=False) -> GameBoard:
|
||||
game_id = game_state['game']['id']
|
||||
game_board: GameBoard
|
||||
try:
|
||||
if self.game_state_local_cache and game_id in self.running_games:
|
||||
game_board = self.running_games[game_id]
|
||||
except KeyError:
|
||||
else:
|
||||
persisted_board = await self.game_state_store.load(game_id)
|
||||
if persisted_board is not None:
|
||||
game_board = cast(GameBoard, persisted_board)
|
||||
if self.game_state_local_cache:
|
||||
self.running_games[game_id] = game_board
|
||||
else:
|
||||
game_board = await self._create_game_board(game_state)
|
||||
@@ -300,7 +306,7 @@ class Server:
|
||||
return storage.cleanup()
|
||||
|
||||
def _prune_stale_games(self):
|
||||
if not self.running_games:
|
||||
if not self.game_last_seen_unix:
|
||||
return
|
||||
|
||||
now = int(time.time())
|
||||
@@ -351,7 +357,7 @@ class Server:
|
||||
|
||||
return {
|
||||
**self.metrics,
|
||||
'active_games': len(self.running_games),
|
||||
'active_games': len(self.game_last_seen_unix),
|
||||
'tracked_games': len(self.game_move_counts),
|
||||
'avg_turns_per_game': round(avg_turns, 2),
|
||||
'win_rate': round(win_rate, 4),
|
||||
|
||||
@@ -26,6 +26,7 @@ def build_server_from_env(default_snake_type:str) -> Server:
|
||||
game_state_backend=os.environ.get('GAME_STATE_BACKEND', 'memory'),
|
||||
game_state_redis_url=os.environ.get('GAME_STATE_REDIS_URL', 'redis://localhost:6379/0'),
|
||||
game_state_ttl_sec=int(os.environ.get('GAME_STATE_TTL_SEC', '900')),
|
||||
game_state_local_cache=env_bool('GAME_STATE_LOCAL_CACHE', default=True),
|
||||
)
|
||||
|
||||
if env_bool('STORE_GAME_HISTORY'):
|
||||
|
||||
Reference in New Issue
Block a user