|
1 | | -import chronos, chronicles |
| 1 | +import std/[sets, tables, options, strutils], chronos, chronicles, results |
2 | 2 | import |
3 | 3 | waku/[ |
4 | 4 | waku_core, |
5 | 5 | waku_core/topics, |
| 6 | + waku_core/topics/sharding, |
6 | 7 | events/message_events, |
7 | 8 | waku_node, |
| 9 | + waku_relay, |
8 | 10 | common/broker/broker_context, |
9 | 11 | ] |
10 | 12 |
|
11 | 13 | type SubscriptionService* = ref object of RootObj |
12 | | - brokerCtx: BrokerContext |
13 | 14 | node: WakuNode |
| 15 | + shardSubs: HashSet[PubsubTopic] |
| 16 | + contentTopicSubs: Table[PubsubTopic, HashSet[ContentTopic]] |
| 17 | + relayHandler: WakuRelayHandler |
14 | 18 |
|
15 | 19 | proc new*(T: typedesc[SubscriptionService], node: WakuNode): T = |
16 | | - ## The storeClient will help to acquire any possible missed messages |
| 20 | + let service = SubscriptionService( |
| 21 | + node: node, |
| 22 | + shardSubs: initHashSet[PubsubTopic](), |
| 23 | + contentTopicSubs: initTable[PubsubTopic, HashSet[ContentTopic]](), |
| 24 | + ) |
17 | 25 |
|
18 | | - return SubscriptionService(brokerCtx: node.brokerCtx, node: node) |
| 26 | + service.relayHandler = proc( |
| 27 | + topic: PubsubTopic, msg: WakuMessage |
| 28 | + ): Future[void] {.async, gcsafe.} = |
| 29 | + if not service.contentTopicSubs.hasKey(topic) or |
| 30 | + not service.contentTopicSubs[topic].contains(msg.contentTopic): |
| 31 | + return |
| 32 | + |
| 33 | + let msgHash = computeMessageHash(topic, msg).to0xHex() |
| 34 | + info "MessageReceivedEvent", |
| 35 | + pubsubTopic = topic, contentTopic = msg.contentTopic, msgHash = msgHash |
| 36 | + |
| 37 | + MessageReceivedEvent.emit(service.node.brokerCtx, msgHash, msg) |
| 38 | + |
| 39 | + return service |
| 40 | + |
| 41 | +proc getShardForContentTopic( |
| 42 | + self: SubscriptionService, topic: ContentTopic |
| 43 | +): Result[PubsubTopic, string] = |
| 44 | + if self.node.wakuAutoSharding.isSome(): |
| 45 | + let shardObj = ?self.node.wakuAutoSharding.get().getShard(topic) |
| 46 | + return ok($shardObj) |
| 47 | + |
| 48 | + return |
| 49 | + err("Manual sharding is not supported in this API. Autosharding must be enabled.") |
| 50 | + |
| 51 | +proc doSubscribe(self: SubscriptionService, shard: PubsubTopic): Result[void, string] = |
| 52 | + self.node.subscribe((kind: PubsubSub, topic: shard), self.relayHandler).isOkOr: |
| 53 | + error "Failed to subscribe to Relay shard", shard = shard, error = error |
| 54 | + return err("Failed to subscribe: " & error) |
| 55 | + return ok() |
| 56 | + |
| 57 | +proc doUnsubscribe( |
| 58 | + self: SubscriptionService, shard: PubsubTopic |
| 59 | +): Result[void, string] = |
| 60 | + self.node.unsubscribe((kind: PubsubUnsub, topic: shard)).isOkOr: |
| 61 | + error "Failed to unsubscribe from Relay shard", shard = shard, error = error |
| 62 | + return err("Failed to unsubscribe: " & error) |
| 63 | + return ok() |
19 | 64 |
|
20 | 65 | proc isSubscribed*( |
21 | 66 | self: SubscriptionService, topic: ContentTopic |
22 | 67 | ): Result[bool, string] = |
23 | | - var isSubscribed = false |
24 | | - if self.node.wakuRelay.isNil() == false: |
25 | | - return self.node.isSubscribed((kind: ContentSub, topic: topic)) |
| 68 | + if self.node.wakuRelay.isNil(): |
| 69 | + return err("SubscriptionService currently only supports Relay (Core) mode.") |
26 | 70 |
|
27 | | - # TODO: Add support for edge mode with Filter subscription management |
28 | | - return ok(isSubscribed) |
| 71 | + let shard = ?self.getShardForContentTopic(topic) |
29 | 72 |
|
30 | | -#TODO: later PR may consider to refactor or place this function elsewhere |
31 | | -# The only important part is that it emits MessageReceivedEvent |
32 | | -proc getReceiveHandler(self: SubscriptionService): WakuRelayHandler = |
33 | | - return proc(topic: PubsubTopic, msg: WakuMessage): Future[void] {.async, gcsafe.} = |
34 | | - let msgHash = computeMessageHash(topic, msg).to0xHex() |
35 | | - info "API received message", |
36 | | - pubsubTopic = topic, contentTopic = msg.contentTopic, msgHash = msgHash |
| 73 | + if self.contentTopicSubs.hasKey(shard) and self.contentTopicSubs[shard].contains( |
| 74 | + topic |
| 75 | + ): |
| 76 | + return ok(true) |
37 | 77 |
|
38 | | - MessageReceivedEvent.emit(self.brokerCtx, msgHash, msg) |
| 78 | + return ok(false) |
39 | 79 |
|
40 | 80 | proc subscribe*(self: SubscriptionService, topic: ContentTopic): Result[void, string] = |
41 | | - let isSubscribed = self.isSubscribed(topic).valueOr: |
42 | | - error "Failed to check subscription status: ", error = error |
43 | | - return err("Failed to check subscription status: " & error) |
| 81 | + if self.node.wakuRelay.isNil(): |
| 82 | + return err("SubscriptionService currently only supports Relay (Core) mode.") |
44 | 83 |
|
45 | | - if isSubscribed == false: |
46 | | - if self.node.wakuRelay.isNil() == false: |
47 | | - self.node.subscribe((kind: ContentSub, topic: topic), self.getReceiveHandler()).isOkOr: |
48 | | - error "Failed to subscribe: ", error = error |
49 | | - return err("Failed to subscribe: " & error) |
| 84 | + let shard = ?self.getShardForContentTopic(topic) |
50 | 85 |
|
51 | | - # TODO: Add support for edge mode with Filter subscription management |
| 86 | + let needShardSub = |
| 87 | + not self.shardSubs.contains(shard) and not self.contentTopicSubs.hasKey(shard) |
| 88 | + |
| 89 | + if needShardSub: |
| 90 | + ?self.doSubscribe(shard) |
| 91 | + |
| 92 | + self.contentTopicSubs.mgetOrPut(shard, initHashSet[ContentTopic]()).incl(topic) |
52 | 93 |
|
53 | 94 | return ok() |
54 | 95 |
|
55 | 96 | proc unsubscribe*( |
56 | 97 | self: SubscriptionService, topic: ContentTopic |
57 | 98 | ): Result[void, string] = |
58 | | - if self.node.wakuRelay.isNil() == false: |
59 | | - self.node.unsubscribe((kind: ContentSub, topic: topic)).isOkOr: |
60 | | - error "Failed to unsubscribe: ", error = error |
61 | | - return err("Failed to unsubscribe: " & error) |
| 99 | + if self.node.wakuRelay.isNil(): |
| 100 | + return err("SubscriptionService currently only supports Relay (Core) mode.") |
| 101 | + |
| 102 | + let shard = ?self.getShardForContentTopic(topic) |
| 103 | + |
| 104 | + if self.contentTopicSubs.hasKey(shard) and self.contentTopicSubs[shard].contains( |
| 105 | + topic |
| 106 | + ): |
| 107 | + let isLastTopic = self.contentTopicSubs[shard].len == 1 |
| 108 | + let needShardUnsub = isLastTopic and not self.shardSubs.contains(shard) |
| 109 | + |
| 110 | + if needShardUnsub: |
| 111 | + ?self.doUnsubscribe(shard) |
| 112 | + |
| 113 | + self.contentTopicSubs[shard].excl(topic) |
| 114 | + if self.contentTopicSubs[shard].len == 0: |
| 115 | + self.contentTopicSubs.del(shard) |
| 116 | + |
| 117 | + return ok() |
| 118 | + |
| 119 | +proc subscribeShard*( |
| 120 | + self: SubscriptionService, shards: seq[PubsubTopic] |
| 121 | +): Result[void, string] = |
| 122 | + if self.node.wakuRelay.isNil(): |
| 123 | + return err("SubscriptionService currently only supports Relay (Core) mode.") |
| 124 | + |
| 125 | + var errors: seq[string] = @[] |
| 126 | + |
| 127 | + for shard in shards: |
| 128 | + if not self.shardSubs.contains(shard): |
| 129 | + let needShardSub = not self.contentTopicSubs.hasKey(shard) |
| 130 | + |
| 131 | + if needShardSub: |
| 132 | + let res = self.doSubscribe(shard) |
| 133 | + if res.isErr(): |
| 134 | + errors.add("Shard " & shard & " failed: " & res.error) |
| 135 | + continue |
| 136 | + |
| 137 | + self.shardSubs.incl(shard) |
| 138 | + |
| 139 | + if errors.len > 0: |
| 140 | + return err("Batch subscribe had errors: " & errors.join("; ")) |
| 141 | + |
| 142 | + return ok() |
| 143 | + |
| 144 | +proc unsubscribeShard*( |
| 145 | + self: SubscriptionService, shards: seq[PubsubTopic] |
| 146 | +): Result[void, string] = |
| 147 | + if self.node.wakuRelay.isNil(): |
| 148 | + return err("SubscriptionService currently only supports Relay (Core) mode.") |
| 149 | + |
| 150 | + var errors: seq[string] = @[] |
| 151 | + |
| 152 | + for shard in shards: |
| 153 | + if self.shardSubs.contains(shard): |
| 154 | + let needShardUnsub = not self.contentTopicSubs.hasKey(shard) |
| 155 | + |
| 156 | + if needShardUnsub: |
| 157 | + let res = self.doUnsubscribe(shard) |
| 158 | + if res.isErr(): |
| 159 | + errors.add("Shard " & shard & " failed: " & res.error) |
| 160 | + continue |
| 161 | + |
| 162 | + self.shardSubs.excl(shard) |
| 163 | + |
| 164 | + if errors.len > 0: |
| 165 | + return err("Batch unsubscribe had errors: " & errors.join("; ")) |
62 | 166 |
|
63 | | - # TODO: Add support for edge mode with Filter subscription management |
64 | 167 | return ok() |
0 commit comments