|
3 | 3 | import asyncio |
4 | 4 | import dataclasses |
5 | 5 | import heapq |
| 6 | +import itertools |
6 | 7 | import logging |
7 | 8 | import math |
8 | 9 | import queue |
9 | 10 | import threading |
10 | | -import time |
11 | 11 | from collections.abc import Iterable |
| 12 | +from collections.abc import Iterator |
12 | 13 | from functools import partial |
13 | 14 | from typing import Final |
14 | 15 |
|
@@ -459,6 +460,7 @@ def __init__( |
459 | 460 | self._transport: asyncio.Transport | None = None |
460 | 461 | self._session_id: str | None = None |
461 | 462 |
|
| 463 | + self._request_seqnum = itertools.count() |
462 | 464 | self._subscriptions: list[tuple[int, tuple[SubscriptionRequest, ...]]] = [] |
463 | 465 | self._unsubscriptions: list[tuple[int, tuple[UnsubscribeRequest, ...]]] = [] |
464 | 466 | self._reconnect_policy = ReconnectPolicy(reconnect_policy) |
@@ -642,7 +644,7 @@ def subscribe( |
642 | 644 | subscription_id=subscription_id, |
643 | 645 | ), |
644 | 646 | ) |
645 | | - self._subscriptions.append((time.time_ns(), subscription)) |
| 647 | + self._subscriptions.append((next(self._request_seqnum), subscription)) |
646 | 648 | return subscription_id |
647 | 649 |
|
648 | 650 | def unsubscribe( |
@@ -680,7 +682,7 @@ def unsubscribe( |
680 | 682 | stype_in=stype_in, |
681 | 683 | ), |
682 | 684 | ) |
683 | | - self._unsubscriptions.append((time.time_ns(), unsubscription)) |
| 685 | + self._unsubscriptions.append((next(self._request_seqnum), unsubscription)) |
684 | 686 |
|
685 | 687 | def terminate(self) -> None: |
686 | 688 | with self._lock: |
@@ -899,44 +901,83 @@ async def _reconnect(self) -> None: |
899 | 901 |
|
900 | 902 | def _replay_requests(self) -> None: |
901 | 903 | """ |
902 | | - Resend subscriptions and unsubscriptions in the order they were sent so |
903 | | - the gateway ends up with the same symbols. |
| 904 | + Recreate the session's subscriptions on the gateway using as few |
| 905 | + subscribe and unsubscribe commands as possible. |
904 | 906 |
|
905 | | - Subscriptions are resent without their original `start`. Ties keep the |
906 | | - subscription first. |
| 907 | + Requests are combined as far as possible without changing the |
| 908 | + symbols the gateway ends up with: |
| 909 | +
|
| 910 | + - An unsubscribe only affects subscriptions for its own schema, so |
| 911 | + each schema is replayed separately in the order its requests were |
| 912 | + sent. |
| 913 | + - Subscriptions are additive, so the subscriptions sent between two |
| 914 | + unsubscribes are combined when their `stype_in` and `snapshot` |
| 915 | + match. |
| 916 | + - Unsubscribes do not commute with each other across symbology types, |
| 917 | + so only consecutive unsubscribes with the same `stype_in` are |
| 918 | + combined. |
| 919 | + - A request for all symbols is never combined with other symbols. |
| 920 | +
|
| 921 | + Subscriptions are resent without their original `start` so data that |
| 922 | + was already received is not replayed, and without their `id` because |
| 923 | + a combined subscription no longer corresponds to one call to |
| 924 | + `subscribe`. |
907 | 925 |
|
908 | 926 | """ |
909 | | - requests = heapq.merge( |
910 | | - ( |
911 | | - (sent_at, partial(self._resend_subscriptions, batch)) |
912 | | - for sent_at, batch in self._subscriptions |
913 | | - ), |
914 | | - ( |
915 | | - (sent_at, partial(self._resend_unsubscriptions, batch)) |
916 | | - for sent_at, batch in self._unsubscriptions |
917 | | - ), |
918 | | - key=lambda entry: entry[0], |
| 927 | + subscriptions: Iterator[tuple[int, SubscriptionRequest | UnsubscribeRequest]] = ( |
| 928 | + (sequence, sub) for sequence, batch in self._subscriptions for sub in batch |
919 | 929 | ) |
920 | | - for _, replay in requests: |
921 | | - replay() |
| 930 | + unsubscriptions: Iterator[tuple[int, SubscriptionRequest | UnsubscribeRequest]] = ( |
| 931 | + (sequence, unsub) for sequence, batch in self._unsubscriptions for unsub in batch |
| 932 | + ) |
| 933 | + |
| 934 | + requests_by_schema: dict[ |
| 935 | + Schema | str, |
| 936 | + list[SubscriptionRequest | UnsubscribeRequest], |
| 937 | + ] = {} |
922 | 938 |
|
923 | | - def _resend_subscriptions(self, subscriptions: tuple[SubscriptionRequest, ...]) -> None: |
| 939 | + for _, request in heapq.merge(subscriptions, unsubscriptions, key=lambda entry: entry[0]): |
| 940 | + requests_by_schema.setdefault(request.schema, []).append(request) |
| 941 | + |
| 942 | + for schema, requests in requests_by_schema.items(): |
| 943 | + for request_type, commands_to_replay in itertools.groupby(requests, key=type): |
| 944 | + if request_type is SubscriptionRequest: |
| 945 | + self._replay_subscriptions(schema, commands_to_replay) # type: ignore [arg-type] |
| 946 | + else: |
| 947 | + self._replay_unsubscriptions(schema, commands_to_replay) # type: ignore [arg-type] |
| 948 | + |
| 949 | + def _replay_subscriptions( |
| 950 | + self, |
| 951 | + schema: Schema | str, |
| 952 | + subscriptions: Iterable[SubscriptionRequest], |
| 953 | + ) -> None: |
| 954 | + symbols: dict[tuple[SType, int, bool], dict[str, None]] = {} |
924 | 955 | for sub in subscriptions: |
| 956 | + key = (sub.stype_in, sub.snapshot, sub.symbols == ALL_SYMBOLS) |
| 957 | + symbols.setdefault(key, {}).update(dict.fromkeys(sub.symbols.split(","))) |
| 958 | + |
| 959 | + for (stype_in, snapshot, _is_all_symbols), group in symbols.items(): |
925 | 960 | self._protocol.subscribe( |
926 | | - schema=sub.schema, |
927 | | - symbols=sub.symbols, |
928 | | - stype_in=sub.stype_in, |
929 | | - snapshot=bool(sub.snapshot), |
930 | | - start=None, |
931 | | - subscription_id=sub.id, |
| 961 | + schema=schema, |
| 962 | + symbols=list(group), |
| 963 | + stype_in=stype_in, |
| 964 | + snapshot=bool(snapshot), |
932 | 965 | ) |
933 | 966 |
|
934 | | - def _resend_unsubscriptions(self, unsubscriptions: tuple[UnsubscribeRequest, ...]) -> None: |
935 | | - for unsub in unsubscriptions: |
| 967 | + def _replay_unsubscriptions( |
| 968 | + self, |
| 969 | + schema: Schema | str, |
| 970 | + unsubscriptions: Iterable[UnsubscribeRequest], |
| 971 | + ) -> None: |
| 972 | + for (stype_in, _is_all_symbols), run in itertools.groupby( |
| 973 | + unsubscriptions, |
| 974 | + key=lambda unsub: (unsub.stype_in, unsub.symbols == ALL_SYMBOLS), |
| 975 | + ): |
| 976 | + symbols = dict.fromkeys(symbol for unsub in run for symbol in unsub.symbols.split(",")) |
936 | 977 | self._protocol.unsubscribe( |
937 | | - schema=unsub.schema, |
938 | | - symbols=unsub.symbols, |
939 | | - stype_in=unsub.stype_in, |
| 978 | + schema=schema, |
| 979 | + symbols=list(symbols), |
| 980 | + stype_in=stype_in, |
940 | 981 | ) |
941 | 982 |
|
942 | 983 | def _dispatch_reconnect_callbacks( |
|
0 commit comments