Skip to content

Commit 8e2daae

Browse files
BagritsevichStepanromange
authored andcommitted
chore: Limit expiry value to 32 bits.
We use adaptive precision, where we keep "millis" precision if we can, and switch to seconds precision if the deadline is too large. This actually was before but now I reduced the cut-off to 4B ms, or 49 days. Also, the ttl in seconds is now limited to 4B sec, with larger values quietly rounded down to this limit. Signed-off-by: Roman Gershman <[email protected]>
1 parent b4a5c87 commit 8e2daae

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/core/expire_period.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace dfly {
1010

1111
class ExpirePeriod {
1212
public:
13-
static constexpr size_t kMaxGenId = 15;
13+
static constexpr uint32_t kMaxGenId = 15;
1414

1515
ExpirePeriod() : val_(0), gen_(0), precision_(0) {
1616
static_assert(sizeof(ExpirePeriod) == 8); // TODO
@@ -35,28 +35,30 @@ class ExpirePeriod {
3535

3636
void Set(uint64_t ms);
3737

38-
bool is_second_precision() { return precision_ == 1;}
38+
bool is_second_precision() const {
39+
return precision_ == 1;
40+
}
3941

4042
private:
41-
uint64_t val_ : 59;
42-
uint64_t gen_ : 4;
43-
uint64_t precision_ : 1; // 0 - ms, 1 - sec.
43+
uint32_t val_;
44+
uint8_t gen_ : 2; // generation id.
45+
uint8_t precision_ : 1; // 0 - ms, 1 - sec.
4446
};
4547

4648
inline void ExpirePeriod::Set(uint64_t ms) {
47-
constexpr uint64_t kBarrier = (1ULL << 48);
48-
49-
if (ms < kBarrier) {
50-
val_ = ms;
51-
precision_ = 0; // ms
49+
if (ms <= UINT32_MAX) {
50+
val_ = ms; // about 49 days in ms.
51+
precision_ = 0; // ms
5252
return;
5353
}
5454

55-
precision_ = 1;
56-
if (ms < kBarrier << 10) {
57-
ms = (ms + 500) / 1000; // seconds
55+
precision_ = 1; // seconds
56+
if (ms < UINT64_MAX / 2) {
57+
ms = (ms + 500) / 1000;
58+
val_ = ms > UINT32_MAX ? UINT32_MAX : ms;
59+
} else {
60+
val_ = UINT32_MAX;
5861
}
59-
val_ = ms >= kBarrier ? kBarrier - 1 : ms;
6062
}
6163

6264
} // namespace dfly

0 commit comments

Comments
 (0)