|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from filelock import FileLock |
| 5 | + |
| 6 | +""" |
| 7 | +The IV service is responsible for generating and storing an IV counter. It is used to generate unique IVs for each message that is sent. |
| 8 | +This means that the IV counter must return a unique number each time. We do this by "reserving" a block of counters that can be used. |
| 9 | +Once we hit the low water mark for that block, we reserve a new block and store the new low water mark. This way, we can never (?) use |
| 10 | +a duplicate counter, but we can miss counters in case of a crash. That is not a problem though. |
| 11 | +
|
| 12 | +Note we don't case about thread safety here, as we assume that the IV service is only used by one thread at a time. |
| 13 | +
|
| 14 | +
|
| 15 | +Counter Low Water Mark |
| 16 | +-------------------------- |
| 17 | + 100 95 |
| 18 | + 99 95 |
| 19 | + 98 95 |
| 20 | + 97 95 |
| 21 | + 96 95 |
| 22 | +
|
| 23 | + Here we hit the low water mark, so we reserve a new block and store the LWM to disk |
| 24 | + |
| 25 | + 95 90 |
| 26 | + 94 90 |
| 27 | + ... |
| 28 | +""" |
| 29 | + |
| 30 | +class IvError(Exception): |
| 31 | + pass |
| 32 | + |
| 33 | +class IvService: |
| 34 | + def __init__(self, filename="iv.json", block_size = 100) -> None: |
| 35 | + self.filename = filename |
| 36 | + self.block_size = block_size |
| 37 | + |
| 38 | + try: |
| 39 | + self.remaining = self.low_watermark = self.load_counter() |
| 40 | + except IvError: |
| 41 | + self.remaining = self.low_watermark = 0 |
| 42 | + |
| 43 | + def set_iv_counter(self, counter: int, if_not_exists: bool = True) -> None: |
| 44 | + """ |
| 45 | + Manually set the IV counter. |
| 46 | + """ |
| 47 | + if if_not_exists and os.path.exists(self.filename): |
| 48 | + return |
| 49 | + self.store_counter(counter) |
| 50 | + self.low_watermark = self.remaining = counter |
| 51 | + |
| 52 | + def get_iv_counter(self) -> int: |
| 53 | + """ |
| 54 | + Return the current IV counter |
| 55 | + """ |
| 56 | + if self.remaining <= self.low_watermark: |
| 57 | + self.low_watermark -= self.block_size |
| 58 | + self.store_counter(self.low_watermark) |
| 59 | + |
| 60 | + self.remaining -= 1 |
| 61 | + return self.remaining |
| 62 | + |
| 63 | + def load_counter(self) -> int: |
| 64 | + """ |
| 65 | + Load counter from disk |
| 66 | + """ |
| 67 | + try: |
| 68 | + lock = FileLock(self.filename + ".lock", timeout=1) |
| 69 | + with lock: |
| 70 | + with open(self.filename, 'r') as f: |
| 71 | + data = json.load(f) |
| 72 | + return data['remaining'] |
| 73 | + except Exception as e: |
| 74 | + raise IvError(e) |
| 75 | + |
| 76 | + def store_counter(self, counter: int) -> None: |
| 77 | + """ |
| 78 | + Store counter to disk |
| 79 | + """ |
| 80 | + try: |
| 81 | + lock = FileLock(self.filename + ".lock", timeout=1) |
| 82 | + with lock: |
| 83 | + with open(self.filename, 'w') as f: |
| 84 | + json.dump({'remaining': counter}, f) |
| 85 | + except Exception as e: |
| 86 | + raise IvError(e) |
| 87 | + |
0 commit comments