Skip to content

Commit

Permalink
Improve event buffer cleanup (#10)
Browse files Browse the repository at this point in the history
* Improve event buffer cleanup

* Ignore event loop when in offline mode

* improve implied offline mode
  • Loading branch information
bpapillon authored Aug 12, 2024
1 parent eab3fe6 commit c2f20f3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
31 changes: 26 additions & 5 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ interface EventBufferOptions {
interval?: number;
logger?: Logger;
maxSize?: number;
offline?: boolean;
}

class EventBuffer {
private events: CreateEventRequestBody[] = [];
private eventsApi: Events;
private interval: number;
private intervalId: NodeJS.Timeout | null = null;
private logger: Logger;
private maxSize: number;
private offline: boolean;
private shutdown: boolean = false;
private stopped: boolean = false;

constructor(eventsApi: Events, opts?: EventBufferOptions) {
const { logger, maxSize, interval } = opts || {};
const {
logger = new ConsoleLogger(),
maxSize = DEFAULT_MAX_SIZE,
interval = DEFAULT_FLUSH_INTERVAL,
offline = false,
} = opts || {};
this.eventsApi = eventsApi;
this.interval = interval || DEFAULT_FLUSH_INTERVAL;
this.logger = logger || new ConsoleLogger();
this.maxSize = maxSize || DEFAULT_MAX_SIZE;
this.interval = interval;
this.logger = logger;
this.maxSize = maxSize;
this.offline = offline;

this.startPeriodicFlush();
}
Expand All @@ -46,6 +55,10 @@ class EventBuffer {
}

public async push(event: CreateEventRequestBody): Promise<void> {
if (this.offline) {
return;
}

if (this.stopped) {
this.logger.error("Event buffer is stopped, not accepting new events");
return;
Expand All @@ -61,11 +74,19 @@ class EventBuffer {
public async stop(): Promise<void> {
this.shutdown = true;
this.stopped = true;
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
await this.flush();
}

private startPeriodicFlush(): void {
setInterval(async () => {
if (this.offline || this.intervalId) {
return;
}

this.intervalId = setInterval(async () => {
if (this.shutdown) return;
await this.flush();
}, this.interval);
Expand Down
45 changes: 28 additions & 17 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,50 @@ export class SchematicClient extends BaseClient {
private offline: boolean;

constructor(opts?: SchematicOptions) {
const { apiKey = "", offline = false } = opts ?? {};
const {
apiKey = "",
basePath,
eventBufferInterval,
flagDefaults = {},
logger = new ConsoleLogger(),
} = opts ?? {};
let { offline = false } = opts ?? {};

// Set headers
const headers: Record<string, string> = {};
if (opts?.environmentId) {
headers["X-Schematic-Environment-Id"] = opts.environmentId;
}
if (opts?.headers) {
Object.assign(headers, opts.headers);
}

// Handle implied offline mode
if (offline) {
if (apiKey !== "") {
logger.debug("Offline mode enabled, ignoring API key");
}
} else if (apiKey === "" && !offline) {
logger.warn("No API key was provided, running in offline mode");
offline = true;
}

// Initialize wrapped client
super({
apiKey,
environment: opts?.basePath ? opts.basePath : undefined,
environment: basePath,
fetcher: offline ? offlineFetcher : provideFetcher(headers),
});
const { flagDefaults = {} } = opts ?? {};

this.logger = logger;
this.eventBuffer = new EventBuffer(this.events, {
interval: opts?.eventBufferInterval,
interval: eventBufferInterval,
logger,
offline,
});

this.flagCheckCacheProviders = opts?.cacheProviders?.flagChecks ?? [new LocalCache<boolean>()];
this.logger = opts?.logger || new ConsoleLogger();

if (offline) {
if (apiKey !== "") {
this.logger.debug("Offline mode enabled, ignoring API key");
}
} else if (apiKey === "") {
this.logger.warn("No API key was provided, running in offline mode");
this.offline = true;
}

this.offline = offline;
this.flagDefaults = flagDefaults;
this.offline = offline;
}

async checkFlag(evalCtx: api.CheckFlagRequestBody, key: string): Promise<boolean> {
Expand Down

0 comments on commit c2f20f3

Please sign in to comment.