Skip to content

Commit

Permalink
Throttle setting registry fetch requests at the connector level
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Mar 23, 2020
1 parent 60accd4 commit 4add6c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
24 changes: 22 additions & 2 deletions packages/apputils-extension/src/settingconnector.ts
@@ -1,8 +1,10 @@
import { PageConfig } from '@jupyterlab/coreutils';

import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { DataConnector, IDataConnector } from '@jupyterlab/statedb';

import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { Throttler } from '@lumino/polling';

/**
* A data connector for fetching settings.
Expand All @@ -19,8 +21,25 @@ export class SettingConnector extends DataConnector<
this._connector = connector;
}

/**
* Fetch settings for a plugin.
* @param id - The plugin ID
*
* #### Notes
* The REST API requests are throttled at one request per plugin per 100ms.
*/
fetch(id: string): Promise<ISettingRegistry.IPlugin | undefined> {
return this._connector.fetch(id);
const throttlers = this._throttlers;
if (!(id in throttlers)) {
throttlers[id] = new Throttler(async () => {
const fetched = await this._connector.fetch(id);
console.log('fetched', id);
throttlers[id].dispose();
delete throttlers[id];
return fetched;
}, 100);
}
return throttlers[id].invoke();
}

async list(
Expand All @@ -44,4 +63,5 @@ export class SettingConnector extends DataConnector<
}

private _connector: IDataConnector<ISettingRegistry.IPlugin, string>;
private _throttlers: { [key: string]: Throttler } = Object.create(null);
}
13 changes: 1 addition & 12 deletions packages/docmanager-extension/src/index.ts
Expand Up @@ -44,8 +44,6 @@ import { IDisposable } from '@lumino/disposable';

import { Widget } from '@lumino/widgets';

import { Throttler } from '@lumino/polling';

/**
* The command IDs used by the document manager plugin.
*/
Expand Down Expand Up @@ -244,18 +242,9 @@ ${fileTypes}`;
}
});

// callback to registry change that ensures not to invoke reload method when there is already a promise that is pending
let reloadSettingsRegistry = () => {
let reloadThrottle = new Throttler(() =>
settingRegistry.reload(pluginId)
);

return reloadThrottle.invoke.bind(reloadThrottle);
};

// If the document registry gains or loses a factory or file type,
// regenerate the settings description with the available options.
registry.changed.connect(reloadSettingsRegistry());
registry.changed.connect(() => settingRegistry.reload(pluginId));

return docManager;
}
Expand Down

0 comments on commit 4add6c7

Please sign in to comment.