-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export {NodeKit} from './nodekit'; | ||
export {AppContext} from './lib/context'; | ||
export {AppConfig} from './types'; | ||
export {AppContextParams} from './types'; | ||
export {AppConfig, AppContextParams, AppDynamicConfig} from './types'; | ||
export {AppError} from './lib/app-error'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import axios, {AxiosError} from 'axios'; | ||
import type {AppContext} from './context'; | ||
|
||
const DYNAMIC_CONFIG_POLL_INTERVAL = 30000; | ||
|
||
export interface DynamicConfigSetup { | ||
url: string; | ||
interval?: number; | ||
} | ||
|
||
export class DynamicConfigPoller { | ||
ctx: AppContext; | ||
|
||
private namespace: string; | ||
private dynamicConfigSetup: DynamicConfigSetup; | ||
|
||
constructor(ctx: AppContext, namespace: string, dynamicConfigSetup: DynamicConfigSetup) { | ||
this.ctx = ctx; | ||
this.namespace = namespace; | ||
this.dynamicConfigSetup = dynamicConfigSetup; | ||
} | ||
|
||
startPolling = () => { | ||
const {dynamicConfigSetup, namespace} = this; | ||
|
||
if (process.env.APP_DEBUG_DYNAMIC_CONFIG) { | ||
this.ctx.log('Dynamic config: fetching started', { | ||
namespace, | ||
}); | ||
} | ||
|
||
axios | ||
.get(`${dynamicConfigSetup.url}?cacheInvalidation=${Date.now()}`) | ||
.then(this.onSuccess, this.onError); | ||
}; | ||
|
||
private getPollTimeout() { | ||
return this.dynamicConfigSetup.interval || DYNAMIC_CONFIG_POLL_INTERVAL; | ||
} | ||
|
||
private onSuccess = (response: {data: Record<string, boolean>}) => { | ||
const {namespace} = this; | ||
|
||
if (process.env.APP_DEBUG_DYNAMIC_CONFIG) { | ||
this.ctx.log('Dynamic config: fetch complete', { | ||
oldDynamicConfig: (this.ctx.dynamicConfig as Record<string, any>)[namespace], | ||
fetchedDynamicConfig: response.data, | ||
namespace, | ||
}); | ||
} | ||
|
||
(this.ctx.dynamicConfig as Record<string, any>)[namespace] = response.data; | ||
|
||
setTimeout(this.startPolling, this.getPollTimeout()); | ||
}; | ||
|
||
private onError = (error: AxiosError) => { | ||
const timeout = this.getPollTimeout(); | ||
const {namespace} = this; | ||
|
||
this.ctx.logError('Dynamic config: fetch failed', error, { | ||
timeout, | ||
namespace, | ||
}); | ||
|
||
setTimeout(this.startPolling, timeout); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters