Cache contract lookups from `Client.get_con()`

rekt_pps
Tyler Goodlet 2023-03-22 13:23:23 -04:00
parent 1c576d72d1
commit 3e5da64571
1 changed files with 15 additions and 5 deletions

View File

@ -24,7 +24,10 @@ from contextlib import asynccontextmanager as acm
from contextlib import AsyncExitStack from contextlib import AsyncExitStack
from dataclasses import asdict, astuple from dataclasses import asdict, astuple
from datetime import datetime from datetime import datetime
from functools import partial from functools import (
partial,
lru_cache,
)
import itertools import itertools
from math import isnan from math import isnan
from typing import ( from typing import (
@ -328,7 +331,7 @@ class Client:
self.ib.RaiseRequestErrors = True self.ib.RaiseRequestErrors = True
# contract cache # contract cache
self._feeds: dict[str, trio.abc.SendChannel] = {} self._cons: dict[str, Contract] = {}
# NOTE: the ib.client here is "throttled" to 45 rps by default # NOTE: the ib.client here is "throttled" to 45 rps by default
@ -612,13 +615,20 @@ class Client:
return con return con
# TODO: make this work with our `MethodProxy`..
# @lru_cache(maxsize=None)
async def get_con( async def get_con(
self, self,
conid: int, conid: int,
) -> Contract: ) -> Contract:
return await self.ib.qualifyContractsAsync( try:
ibis.Contract(conId=conid) return self._cons[conid]
) except KeyError:
con: Contract = await self.ib.qualifyContractsAsync(
ibis.Contract(conId=conid)
)
self._cons[conid] = con
return con
def parse_patt2fqsn( def parse_patt2fqsn(
self, self,