|
| 1 | +import { EventID, StreamID } from '@ceramicnetwork/streamid' |
| 2 | +import { DiagnosticsLogger } from '@ceramicnetwork/common' |
| 3 | +import { Repository } from './state-management/repository.js' |
| 4 | +import * as ReconClient from 'zcgen-client' |
| 5 | +import { Dispatcher } from './dispatcher.js' |
| 6 | +import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance' |
| 7 | +import { from, repeat, timer, switchMap, tap, Subscription } from 'rxjs' |
| 8 | +import { retry } from 'rxjs/operators' |
| 9 | + |
| 10 | +/** |
| 11 | + * Recon Event |
| 12 | + */ |
| 13 | +export interface Event { |
| 14 | + eventId: string |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Describes Recon Service API |
| 19 | + */ |
| 20 | +export interface ReconApi { |
| 21 | + readonly networkName: string |
| 22 | + /** |
| 23 | + * Recon subscription, subscribe by model |
| 24 | + */ |
| 25 | + subscribe(model: string): Subscription |
| 26 | + /** |
| 27 | + * Add event to recon |
| 28 | + */ |
| 29 | + addEvent(event: Event): Promise<void> |
| 30 | + /** |
| 31 | + * Unsubscribe to subscription by model |
| 32 | + */ |
| 33 | + unsubscribe(model: string): void |
| 34 | + /** |
| 35 | + * Close and unsubscribe to all |
| 36 | + */ |
| 37 | + close(): void |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Recon subscription manager, manages simple map of models to subscriptions |
| 42 | + */ |
| 43 | +export interface SubManager { |
| 44 | + /** |
| 45 | + * Add active subscription |
| 46 | + */ |
| 47 | + add(model: string, sub: Subscription): void |
| 48 | + /** |
| 49 | + * Get subscription by model |
| 50 | + */ |
| 51 | + get(model: string): Subscription | undefined |
| 52 | + /** |
| 53 | + * Unsubscribe |
| 54 | + */ |
| 55 | + unsubscribe(model: string): void |
| 56 | + /** |
| 57 | + * Unsubscribe to all known subscriptions |
| 58 | + */ |
| 59 | + close(): void |
| 60 | +} |
| 61 | + |
| 62 | +export class ReconSubManager implements SubManager { |
| 63 | + private readonly subscriptions: Record<string, Subscription> |
| 64 | + |
| 65 | + constructor(private readonly logger: DiagnosticsLogger) { |
| 66 | + this.subscriptions = {} |
| 67 | + } |
| 68 | + |
| 69 | + add(model: string, sub: Subscription): void { |
| 70 | + this.subscriptions[model] = sub |
| 71 | + this.logger.verbose(`Recon: subscription for model ${model} added`) |
| 72 | + } |
| 73 | + |
| 74 | + get(model: string): Subscription | undefined { |
| 75 | + return this.subscriptions[model] |
| 76 | + } |
| 77 | + |
| 78 | + unsubscribe(model: string): void { |
| 79 | + const sub = this.get(model) |
| 80 | + if (!sub) return |
| 81 | + sub.unsubscribe() |
| 82 | + delete this.subscriptions[model] |
| 83 | + this.logger.verbose(`Recon: unsubscribed for model ${model}`) |
| 84 | + } |
| 85 | + |
| 86 | + close(): void { |
| 87 | + Object.keys(this.subscriptions).forEach((model) => { |
| 88 | + this.unsubscribe(model) |
| 89 | + }) |
| 90 | + this.logger.verbose(`Recon: closing, unsubscribed to all`) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Recon API |
| 96 | + */ |
| 97 | +export class ReconApiHTTP implements ReconApi { |
| 98 | + private readonly api: ReconClient.DefaultApi |
| 99 | + private readonly subscriptions: ReconSubManager |
| 100 | + |
| 101 | + constructor( |
| 102 | + url: string, |
| 103 | + readonly networkName: string, |
| 104 | + private readonly repository: Repository, |
| 105 | + private readonly dispatcher: Dispatcher, |
| 106 | + private readonly logger: DiagnosticsLogger |
| 107 | + ) { |
| 108 | + const baseServer = new ReconClient.ServerConfiguration(url, {}) |
| 109 | + const config = ReconClient.createConfiguration({ baseServer }) |
| 110 | + this.api = new ReconClient.DefaultApi(config) |
| 111 | + this.subscriptions = new ReconSubManager(logger) |
| 112 | + } |
| 113 | + |
| 114 | + subscribe(model: string): Subscription { |
| 115 | + if (this.subscriptions.get(model)) return this.subscriptions.get(model) |
| 116 | + |
| 117 | + let offset = 0 |
| 118 | + const increaseOffset = (val: number): void => { |
| 119 | + offset += val |
| 120 | + } |
| 121 | + |
| 122 | + const obv$ = from( |
| 123 | + this.api.ceramicSubscribeSortKeySortValueGet( |
| 124 | + 'model', |
| 125 | + model, |
| 126 | + undefined, |
| 127 | + undefined, |
| 128 | + offset, |
| 129 | + 1000 |
| 130 | + ) |
| 131 | + ).pipe( |
| 132 | + tap((arr) => increaseOffset(arr.length)), |
| 133 | + switchMap(from), |
| 134 | + repeat({ delay: 200 }), |
| 135 | + retry({ |
| 136 | + delay: (error, count) => { |
| 137 | + this.logger.warn(`Recon: subscription failed for model ${model}, attempting to retry`) |
| 138 | + // exp backoff, max 3 minutes |
| 139 | + return timer(count > 11 ? 3 * 60 * 1000 : 2 ^ (count * 100)) |
| 140 | + }, |
| 141 | + resetOnSuccess: true, |
| 142 | + }) |
| 143 | + ) |
| 144 | + |
| 145 | + // in future could return observable, handler added here to keep recon code together for now |
| 146 | + const sub = obv$.subscribe(this._eventHandler) |
| 147 | + this.subscriptions.add(model, sub) |
| 148 | + return sub |
| 149 | + } |
| 150 | + |
| 151 | + unsubscribe(model: string): void { |
| 152 | + this.subscriptions.unsubscribe(model) |
| 153 | + } |
| 154 | + |
| 155 | + close(): void { |
| 156 | + this.subscriptions.close() |
| 157 | + } |
| 158 | + |
| 159 | + // messy here, so that recon changes are minimized for now and uses existing apis, |
| 160 | + // model/streamids used for lots of caching, but could later be implemented w/o or recon based |
| 161 | + async _eventHandler(event: string): Promise<void> { |
| 162 | + const eventId = EventID.fromString(event) |
| 163 | + const commit = await this.dispatcher.retrieveCommit(eventId.event) |
| 164 | + |
| 165 | + let header, gcid |
| 166 | + if (commit.proof) { |
| 167 | + gcid = commit.id |
| 168 | + } else if (commit.id) { |
| 169 | + const genesis = await this.dispatcher.retrieveCommit(commit.id) |
| 170 | + header = genesis.header |
| 171 | + gcid = commit.id |
| 172 | + } else { |
| 173 | + header = commit.header |
| 174 | + gcid = eventId.event |
| 175 | + } |
| 176 | + |
| 177 | + const model = header ? StreamID.fromBytes(header.model) : undefined |
| 178 | + // assumes model instance |
| 179 | + const streamid = new StreamID(ModelInstanceDocument.STREAM_TYPE_ID, gcid) |
| 180 | + |
| 181 | + this.logger.verbose(`Recon: received eventID ${eventId.toString()} for streamId ${streamid}`) |
| 182 | + await this.repository.stateManager.handleUpdate(streamid, eventId.event, model) |
| 183 | + } |
| 184 | + |
| 185 | + async addEvent(event: Event): Promise<void> { |
| 186 | + try { |
| 187 | + await this.api.ceramicEventsPost(event) |
| 188 | + this.logger.verbose(`Recon: added event ${event.eventId}`) |
| 189 | + } catch (err) { |
| 190 | + this.logger.err(`Recon: failed to add event ${event.eventId}`) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments