|
| 1 | +import ConfigurationRequestor from './configuration-requestor'; |
| 2 | +import { Listeners } from './listener'; |
| 3 | +import { Configuration } from './configuration'; |
| 4 | +import { randomJitterMs } from './poller'; |
| 5 | +import { logger } from './application-logger'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Polls for new configurations from the Eppo server. When a new configuration is fetched, |
| 9 | + * it is passed to the subscribers of `onConfigurationFetched`. |
| 10 | + * |
| 11 | + * The poller is created in the stopped state. Call `start` to begin polling. |
| 12 | + * |
| 13 | + * @internal |
| 14 | + */ |
| 15 | +export class ConfigurationPoller { |
| 16 | + private readonly listeners = new Listeners<[Configuration]>(); |
| 17 | + private readonly basePollingIntervalMs: number; |
| 18 | + private readonly maxPollingIntervalMs: number; |
| 19 | + private isRunning = false; |
| 20 | + |
| 21 | + public constructor( |
| 22 | + private readonly configurationRequestor: ConfigurationRequestor, |
| 23 | + options: { |
| 24 | + basePollingIntervalMs: number; |
| 25 | + maxPollingIntervalMs: number; |
| 26 | + }, |
| 27 | + ) { |
| 28 | + this.basePollingIntervalMs = options.basePollingIntervalMs; |
| 29 | + this.maxPollingIntervalMs = options.maxPollingIntervalMs; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Starts the configuration poller. |
| 34 | + * |
| 35 | + * This method will start polling for new configurations from the Eppo server. |
| 36 | + * It will continue to poll until the `stop` method is called. |
| 37 | + */ |
| 38 | + public start(): void { |
| 39 | + if (!this.isRunning) { |
| 40 | + this.isRunning = true; |
| 41 | + this.poll().finally(() => { |
| 42 | + // Just to be safe, reset isRunning if the poll() method throws an error or exits (it |
| 43 | + // shouldn't). |
| 44 | + this.isRunning = false; |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Stops the configuration poller. |
| 51 | + * |
| 52 | + * This method will stop polling for new configurations from the Eppo server. Note that it will |
| 53 | + * not interrupt the current poll cycle / active fetch, but it will make sure that configuration |
| 54 | + * listeners are not notified of any new configurations after this method is called. |
| 55 | + */ |
| 56 | + public stop(): void { |
| 57 | + this.isRunning = false; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Register a listener to be notified when new configuration is fetched. |
| 62 | + * @param listener Callback function that receives the fetched `Configuration` object |
| 63 | + * @returns A function that can be called to unsubscribe the listener. |
| 64 | + */ |
| 65 | + public onConfigurationFetched(listener: (configuration: Configuration) => void): () => void { |
| 66 | + return this.listeners.addListener(listener); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Fetch configuration immediately without waiting for the next polling cycle. |
| 71 | + * |
| 72 | + * Note: This does not coordinate with active polling - polling intervals will not be adjusted |
| 73 | + * when using this method. |
| 74 | + * |
| 75 | + * @throws If there is an error fetching the configuration |
| 76 | + */ |
| 77 | + public async fetchImmediate(): Promise<Configuration | null> { |
| 78 | + const configuration = await this.configurationRequestor.fetchConfiguration(); |
| 79 | + if (configuration) { |
| 80 | + this.listeners.notify(configuration); |
| 81 | + } |
| 82 | + return configuration; |
| 83 | + } |
| 84 | + |
| 85 | + private async poll(): Promise<void> { |
| 86 | + // Number of failures we've seen in a row. |
| 87 | + let consecutiveFailures = 0; |
| 88 | + |
| 89 | + while (this.isRunning) { |
| 90 | + try { |
| 91 | + const configuration = await this.configurationRequestor.fetchConfiguration(); |
| 92 | + if (configuration && this.isRunning) { |
| 93 | + this.listeners.notify(configuration); |
| 94 | + } |
| 95 | + // Reset failure counter on success |
| 96 | + consecutiveFailures = 0; |
| 97 | + } catch (err) { |
| 98 | + logger.warn('Eppo SDK encountered an error polling configurations', { err }); |
| 99 | + consecutiveFailures++; |
| 100 | + } |
| 101 | + |
| 102 | + if (consecutiveFailures === 0) { |
| 103 | + await timeout(this.basePollingIntervalMs + randomJitterMs(this.basePollingIntervalMs)); |
| 104 | + } else { |
| 105 | + // Exponential backoff capped at maxPollingIntervalMs. |
| 106 | + const baseDelayMs = Math.min((Math.pow(2, consecutiveFailures) * this.basePollingIntervalMs), this.maxPollingIntervalMs); |
| 107 | + const delayMs = baseDelayMs + randomJitterMs(baseDelayMs); |
| 108 | + |
| 109 | + logger.warn('Eppo SDK will try polling again', { |
| 110 | + delayMs, |
| 111 | + consecutiveFailures, |
| 112 | + }); |
| 113 | + |
| 114 | + await timeout(delayMs); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +function timeout(ms: number) { |
| 121 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 122 | +} |
| 123 | + |
0 commit comments