-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathsequencer.cpp
More file actions
299 lines (243 loc) · 6.77 KB
/
sequencer.cpp
File metadata and controls
299 lines (243 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2016-2021 Fanout, Inc.
*
* This file is part of Pushpin.
*
* $FANOUT_BEGIN_LICENSE:APACHE2$
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $FANOUT_END_LICENSE$
*/
#include "sequencer.h"
#include <QDateTime>
#include <QTimer>
#include "log.h"
#include "defercall.h"
#include "publishitem.h"
#include "publishlastids.h"
#define CHANNEL_PENDING_MAX 100
#define DEFAULT_PENDING_EXPIRE 5000
#define EXPIRE_INTERVAL 1000
class Sequencer::Private : public QObject
{
Q_OBJECT
public:
class PendingItem
{
public:
qint64 time;
PublishItem item;
};
class ChannelPendingItems
{
public:
QHash<QString, PendingItem*> itemsByPrevId;
~ChannelPendingItems()
{
qDeleteAll(itemsByPrevId);
}
};
class CachedId
{
public:
qint64 expireTime;
QString channel;
QString id;
};
Sequencer *q;
PublishLastIds *lastIds;
QHash<QString, ChannelPendingItems> pendingItemsByChannel;
QMap<QPair<qint64, PendingItem*>, PendingItem*> pendingItemsByTime;
QTimer *expireTimer;
int pendingExpireMSecs;
int idCacheTtl;
QHash<QPair<QString, QString>, CachedId*> idCacheById;
QMap<QPair<qint64, CachedId*>, CachedId*> idCacheByExpireTime;
Private(Sequencer *_q, PublishLastIds *_publishLastIds) :
QObject(_q),
q(_q),
lastIds(_publishLastIds),
pendingExpireMSecs(DEFAULT_PENDING_EXPIRE),
idCacheTtl(-1)
{
expireTimer = new QTimer(this);
connect(expireTimer, &QTimer::timeout, this, &Private::expireTimer_timeout);
}
~Private()
{
expireTimer->disconnect(this);
expireTimer->setParent(0);
DeferCall::deleteLater(expireTimer);
qDeleteAll(idCacheById);
}
void addItem(const PublishItem &item, bool seq)
{
qint64 now = QDateTime::currentMSecsSinceEpoch();
while(!idCacheByExpireTime.isEmpty())
{
QMap<QPair<qint64, CachedId*>, CachedId*>::iterator it = idCacheByExpireTime.begin();
CachedId *i = it.value();
if(i->expireTime > now)
break;
idCacheById.remove(QPair<QString, QString>(i->channel, i->id));
idCacheByExpireTime.erase(it);
delete i;
}
if(!item.id.isNull() && idCacheTtl > 0)
{
QPair<QString, QString> idKey(item.channel, item.id);
if(idCacheById.contains(idKey))
{
// we've seen this ID recently, drop the message
return;
}
CachedId *i = new CachedId;
i->expireTime = now + (idCacheTtl * 1000);
i->channel = item.channel;
i->id = item.id;
idCacheById.insert(idKey, i);
idCacheByExpireTime.insert(QPair<qint64, CachedId*>(i->expireTime, i), i);
}
if(!seq)
{
q->itemReady(item);
return;
}
QString lastId = lastIds->value(item.channel);
if(!lastId.isNull() && !item.prevId.isNull() && lastId != item.prevId)
{
ChannelPendingItems &channelPendingItems = pendingItemsByChannel[item.channel];
if(channelPendingItems.itemsByPrevId.contains(item.prevId))
{
log_debug("sequencer: already have item for channel [%s] depending on prev-id [%s], dropping", qPrintable(item.channel), qPrintable(item.prevId));
return;
}
if(channelPendingItems.itemsByPrevId.count() >= CHANNEL_PENDING_MAX)
{
log_debug("sequencer: too many pending items for channel [%s], dropping", qPrintable(item.channel));
return;
}
PendingItem *i = new PendingItem;
i->time = now;
i->item = item;
channelPendingItems.itemsByPrevId.insert(item.prevId, i);
pendingItemsByTime.insert(QPair<qint64, PendingItem*>(i->time, i), i);
if(!expireTimer->isActive())
expireTimer->start(EXPIRE_INTERVAL);
return;
}
sendItem(item);
}
void clear(const QString &channel)
{
if(!pendingItemsByChannel.contains(channel))
return;
ChannelPendingItems &channelPendingItems = pendingItemsByChannel[channel];
QHashIterator<QString, PendingItem*> it(channelPendingItems.itemsByPrevId);
while(it.hasNext())
{
it.next();
PendingItem *i = it.value();
pendingItemsByTime.remove(QPair<qint64, PendingItem*>(i->time, i));
}
pendingItemsByChannel.remove(channel);
}
void sendItem(const PublishItem &item)
{
if(!item.id.isNull())
lastIds->set(item.channel, item.id);
else
lastIds->remove(item.channel);
q->itemReady(item);
if(pendingItemsByChannel.contains(item.channel))
{
ChannelPendingItems &channelPendingItems = pendingItemsByChannel[item.channel];
QString id = item.id;
while(!id.isNull() && !channelPendingItems.itemsByPrevId.isEmpty())
{
PendingItem *i = channelPendingItems.itemsByPrevId.value(id);
if(!i)
break;
PublishItem pitem = i->item;
channelPendingItems.itemsByPrevId.remove(i->item.prevId);
pendingItemsByTime.remove(QPair<qint64, PendingItem*>(i->time, i));
delete i;
if(!pitem.id.isNull())
lastIds->set(pitem.channel, pitem.id);
else
lastIds->remove(pitem.channel);
q->itemReady(pitem);
id = pitem.id;
}
if(channelPendingItems.itemsByPrevId.isEmpty())
{
pendingItemsByChannel.remove(item.channel);
if(pendingItemsByChannel.isEmpty())
expireTimer->stop();
}
}
}
private slots:
void expireTimer_timeout()
{
qint64 now = QDateTime::currentMSecsSinceEpoch();
qint64 threshold = now - pendingExpireMSecs;
while(!pendingItemsByTime.isEmpty())
{
QMap<QPair<qint64, PendingItem*>, PendingItem*>::iterator it = pendingItemsByTime.begin();
PendingItem *i = it.value();
if(i->time > threshold)
break;
log_debug("timing out item channel=[%s] id=[%s]", qPrintable(i->item.channel), qPrintable(i->item.id));
PublishItem item = i->item;
ChannelPendingItems &channelPendingItems = pendingItemsByChannel[i->item.channel];
channelPendingItems.itemsByPrevId.remove(i->item.prevId);
pendingItemsByTime.erase(it);
delete i;
if(channelPendingItems.itemsByPrevId.isEmpty())
{
pendingItemsByChannel.remove(item.channel);
if(pendingItemsByChannel.isEmpty())
expireTimer->stop();
}
sendItem(item);
}
}
};
Sequencer::Sequencer(PublishLastIds *publishLastIds, QObject *parent) :
QObject(parent)
{
d = new Private(this, publishLastIds);
}
Sequencer::~Sequencer()
{
delete d;
}
void Sequencer::setWaitMax(int msecs)
{
d->pendingExpireMSecs = msecs;
}
void Sequencer::setIdCacheTtl(int secs)
{
d->idCacheTtl = secs;
}
void Sequencer::addItem(const PublishItem &item, bool seq)
{
d->addItem(item, seq);
}
void Sequencer::clearPendingForChannel(const QString &channel)
{
d->clear(channel);
}
#include "sequencer.moc"