Allow webserver mode to cache multiple exchanges

This commit is contained in:
Matthias
2023-06-02 09:50:40 +02:00
parent e2594e7494
commit af16ce874c
3 changed files with 18 additions and 7 deletions
@@ -98,7 +98,6 @@ def pairlists_evaluate(payload: PairListsPayload, background_tasks: BackgroundTa
'result': {}, 'result': {},
'error': None, 'error': None,
} }
ApiBG.running_jobs.append(job_id)
background_tasks.add_task(__run_pairlist, job_id, config_loc) background_tasks.add_task(__run_pairlist, job_id, config_loc)
ApiBG.pairlist_running = True ApiBG.pairlist_running = True
+13 -3
View File
@@ -3,6 +3,7 @@ from uuid import uuid4
from fastapi import Depends, HTTPException from fastapi import Depends, HTTPException
from freqtrade.constants import Config
from freqtrade.enums import RunMode from freqtrade.enums import RunMode
from freqtrade.persistence import Trade from freqtrade.persistence import Trade
from freqtrade.persistence.models import _request_id_ctx_var from freqtrade.persistence.models import _request_id_ctx_var
@@ -43,12 +44,21 @@ def get_api_config() -> Dict[str, Any]:
return ApiServer._config['api_server'] return ApiServer._config['api_server']
def _generate_exchange_key(config: Config) -> str:
"""
Exchange key - used for caching the exchange object.
"""
return f"{config['exchange']['name']}_{config.get('trading_mode', 'spot')}"
def get_exchange(config=Depends(get_config)): def get_exchange(config=Depends(get_config)):
if not ApiBG.exchange: exchange_key = _generate_exchange_key(config)
if not (exchange := ApiBG.exchanges.get(exchange_key)):
from freqtrade.resolvers import ExchangeResolver from freqtrade.resolvers import ExchangeResolver
ApiBG.exchange = ExchangeResolver.load_exchange( exchange = ExchangeResolver.load_exchange(
config, load_leverage_tiers=False) config, load_leverage_tiers=False)
return ApiBG.exchange ApiBG.exchanges[exchange_key] = exchange
return exchange
def get_message_stream(): def get_message_stream():
+5 -3
View File
@@ -1,7 +1,9 @@
from typing import Any, Dict, List, Literal, Optional, TypedDict from typing import Any, Dict, Literal, Optional, TypedDict
from uuid import uuid4 from uuid import uuid4
from freqtrade.exchange.exchange import Exchange
class JobsContainer(TypedDict): class JobsContainer(TypedDict):
category: Literal['pairlist'] category: Literal['pairlist']
@@ -23,10 +25,10 @@ class ApiBG():
} }
bgtask_running: bool = False bgtask_running: bool = False
# Exchange - only available in webserver mode. # Exchange - only available in webserver mode.
exchange = None exchanges: Dict[str, Exchange] = {}
# Generic background jobs # Generic background jobs
running_jobs: List[str] = []
# TODO: Change this to TTLCache # TODO: Change this to TTLCache
jobs: Dict[str, JobsContainer] = {} jobs: Dict[str, JobsContainer] = {}
# Pairlist evaluate things # Pairlist evaluate things