1717import itertools
1818import logging
1919from collections import namedtuple
20- from typing import Any , List , Optional
20+ from typing import Any , List , Optional , Tuple
2121
2222import attr
2323
24+ from synapse .types import JsonDict
25+
2426logger = logging .getLogger (__name__ )
2527
2628
@@ -119,13 +121,12 @@ class Stream(object):
119121 """Base class for the streams.
120122
121123 Provides a `get_updates()` function that returns new updates since the last
122- time it was called up until the point `advance_current_token` was called .
124+ time it was called.
123125 """
124126
125127 NAME = None # type: str # The name of the stream
126128 # The type of the row. Used by the default impl of parse_row.
127129 ROW_TYPE = None # type: Any
128- _LIMITED = True # Whether the update function takes a limit
129130
130131 @classmethod
131132 def parse_row (cls , row ):
@@ -146,26 +147,15 @@ def __init__(self, hs):
146147 # The token from which we last asked for updates
147148 self .last_token = self .current_token ()
148149
149- # The token that we will get updates up to
150- self .upto_token = self .current_token ()
151-
152- def advance_current_token (self ):
153- """Updates `upto_token` to "now", which updates up until which point
154- get_updates[_since] will fetch rows till.
155- """
156- self .upto_token = self .current_token ()
157-
158150 def discard_updates_and_advance (self ):
159151 """Called when the stream should advance but the updates would be discarded,
160152 e.g. when there are no currently connected workers.
161153 """
162- self .upto_token = self .current_token ()
163- self .last_token = self .upto_token
154+ self .last_token = self .current_token ()
164155
165156 async def get_updates (self ):
166157 """Gets all updates since the last time this function was called (or
167- since the stream was constructed if it hadn't been called before),
168- until the `upto_token`
158+ since the stream was constructed if it hadn't been called before).
169159
170160 Returns:
171161 Deferred[Tuple[List[Tuple[int, Any]], int]:
@@ -178,44 +168,45 @@ async def get_updates(self):
178168
179169 return updates , current_token
180170
181- async def get_updates_since (self , from_token ):
171+ async def get_updates_since (
172+ self , from_token : int
173+ ) -> Tuple [List [Tuple [int , JsonDict ]], int ]:
182174 """Like get_updates except allows specifying from when we should
183175 stream updates
184176
185177 Returns:
186- Deferred[Tuple[List[Tuple[int, Any]], int]:
187- Resolves to a pair ``(updates, current_token)``, where ``updates`` is a
188- list of ``(token, row)`` entries. ``row`` will be json-serialised and
189- sent over the replication steam.
178+ Resolves to a pair `(updates, new_last_token)`, where `updates` is
179+ a list of `(token, row)` entries and `new_last_token` is the new
180+ position in stream.
190181 """
182+
191183 if from_token in ("NOW" , "now" ):
192- return [], self .upto_token
184+ return [], self .current_token ()
193185
194- current_token = self .upto_token
186+ current_token = self .current_token ()
195187
196188 from_token = int (from_token )
197189
198190 if from_token == current_token :
199191 return [], current_token
200192
201- logger .info ("get_updates_since: %s" , self .__class__ )
202- if self ._LIMITED :
203- rows = await self .update_function (
204- from_token , current_token , limit = MAX_EVENTS_BEHIND + 1
205- )
193+ rows = await self .update_function (
194+ from_token , current_token , limit = MAX_EVENTS_BEHIND + 1
195+ )
206196
207- # never turn more than MAX_EVENTS_BEHIND + 1 into updates.
208- rows = itertools .islice (rows , MAX_EVENTS_BEHIND + 1 )
209- else :
210- rows = await self .update_function (from_token , current_token )
197+ # never turn more than MAX_EVENTS_BEHIND + 1 into updates.
198+ rows = itertools .islice (rows , MAX_EVENTS_BEHIND + 1 )
211199
212200 updates = [(row [0 ], row [1 :]) for row in rows ]
213201
214202 # check we didn't get more rows than the limit.
215203 # doing it like this allows the update_function to be a generator.
216- if self . _LIMITED and len (updates ) >= MAX_EVENTS_BEHIND :
204+ if len (updates ) >= MAX_EVENTS_BEHIND :
217205 raise Exception ("stream %s has fallen behind" % (self .NAME ))
218206
207+ # The update function didn't hit the limit, so we must have got all
208+ # the updates to `current_token`, and can return that as our new
209+ # stream position.
219210 return updates , current_token
220211
221212 def current_token (self ):
@@ -227,9 +218,8 @@ def current_token(self):
227218 """
228219 raise NotImplementedError ()
229220
230- def update_function (self , from_token , current_token , limit = None ):
231- """Get updates between from_token and to_token. If Stream._LIMITED is
232- True then limit is provided, otherwise it's not.
221+ def update_function (self , from_token , current_token , limit ):
222+ """Get updates between from_token and to_token.
233223
234224 Returns:
235225 Deferred(list(tuple)): the first entry in the tuple is the token for
@@ -257,7 +247,6 @@ def __init__(self, hs):
257247
258248class PresenceStream (Stream ):
259249 NAME = "presence"
260- _LIMITED = False
261250 ROW_TYPE = PresenceStreamRow
262251
263252 def __init__ (self , hs ):
@@ -272,7 +261,6 @@ def __init__(self, hs):
272261
273262class TypingStream (Stream ):
274263 NAME = "typing"
275- _LIMITED = False
276264 ROW_TYPE = TypingStreamRow
277265
278266 def __init__ (self , hs ):
@@ -372,7 +360,6 @@ class DeviceListsStream(Stream):
372360 """
373361
374362 NAME = "device_lists"
375- _LIMITED = False
376363 ROW_TYPE = DeviceListsStreamRow
377364
378365 def __init__ (self , hs ):
@@ -462,7 +449,6 @@ class UserSignatureStream(Stream):
462449 """
463450
464451 NAME = "user_signature"
465- _LIMITED = False
466452 ROW_TYPE = UserSignatureStreamRow
467453
468454 def __init__ (self , hs ):
0 commit comments