[test-publisher]: Set cap on dyn added symbols (#371)

* [test-publisher]: Set cap on dyn added symbols

Currently the test publisher creates a new symbol every two minutes
without any cap and running tilt for a
long time will eventually drain all the resources.

This change sets a cap of PYTH_TEST_SYMBOL_COUNT
on the dynamically added symbols. It means that the publisher
starts with PYTH_TEST_SYMBOL_COUNT and will add at most
PYTH_TEST_SYMBOL_COUNT new symbols after that

* Update third_party/pyth/pyth_publisher.py

Co-authored-by: Stanisław Drozd <stan@nexantic.com>

* Update third_party/pyth/pyth_publisher.py

Co-authored-by: Stanisław Drozd <stan@nexantic.com>

* Update third_party/pyth/pyth_publisher.py

Co-authored-by: Stanisław Drozd <stan@nexantic.com>

Co-authored-by: Stanisław Drozd <stan@nexantic.com>
This commit is contained in:
Ali Behjati 2022-11-03 13:58:44 +01:00 committed by GitHub
parent 1a9dfb6c0d
commit 61b84c84ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions

View File

@ -147,18 +147,22 @@ next_new_symbol_id = PYTH_TEST_SYMBOL_COUNT
last_new_sym_added_at = time.monotonic()
with ThreadPoolExecutor() as executor: # Used for async adding of products and prices
dynamically_added_symbols = 0
while True:
for sym in HTTP_ENDPOINT_DATA["symbols"]:
publisher_random_update(sym["price"])
# Add a symbol if new symbol interval configured
if PYTH_NEW_SYMBOL_INTERVAL_SECS > 0:
# Add a symbol if new symbol interval configured. This will add a new symbol if PYTH_NEW_SYMBOL_INTERVAL_SECS
# is passed since adding the previous symbol. The second constraint ensures that
# at most PYTH_TEST_SYMBOL_COUNT new price symbols are created.
if PYTH_NEW_SYMBOL_INTERVAL_SECS > 0 and dynamically_added_symbols < PYTH_TEST_SYMBOL_COUNT:
# Do it if enough time passed
now = time.monotonic()
if (now - last_new_sym_added_at) >= PYTH_NEW_SYMBOL_INTERVAL_SECS:
executor.submit(add_symbol, next_new_symbol_id) # Returns immediately, runs in background
last_sym_added_at = now
next_new_symbol_id += 1
dynamically_added_symbols += 1
time.sleep(PYTH_PUBLISHER_INTERVAL_SECS)
sys.stdout.flush()