From 3b2a9e5c7501ea575c6f0b3c73b7f6fdd728c6aa Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 15 Mar 2018 15:03:01 -0600 Subject: [PATCH 1/2] improved context perf --- graph/context.js | 36 ++++++++++++++++++++++++++++++++---- services/logging.js | 20 ++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/graph/context.js b/graph/context.js index 52bb7fdfc..dcbbc5f6b 100644 --- a/graph/context.js +++ b/graph/context.js @@ -42,6 +42,32 @@ const decorateContextPlugins = (context, contextPlugins) => { ); }; +/** + * Some pieces of the Context are quite complex to setup, using multiple merges + * and other lodash functions. This proxies that access such that it is only + * loaded if it is used. Helpful for a query that only uses a loader, and not a + * mutator. + * + * @param {Object} ctx the graph proxy + * @param {Function} loader the loadable component that should be proxied + */ +const proxyContextLoader = (ctx, loader) => + new Proxy( + { loaded: false, data: null }, + { + get: (obj, prop) => { + if (obj.loaded) { + return obj.data[prop]; + } + + obj.data = loader(ctx); + obj.loaded = true; + + return obj.data[prop]; + }, + } + ); + /** * Stores the request context. */ @@ -61,16 +87,18 @@ class Context { this.connectors = connectors; // Create the loaders. - this.loaders = loaders(this); + this.loaders = proxyContextLoader(this, loaders); // Create the mutators. - this.mutators = mutators(this); + this.mutators = proxyContextLoader(this, mutators); // Decorate the plugin context. - this.plugins = decorateContextPlugins(this, contextPlugins); + this.plugins = proxyContextLoader(this, () => + decorateContextPlugins(this, contextPlugins) + ); // Bind the publish/subscribe to the context. - this.pubsub = getBroker(); + this.pubsub = proxyContextLoader(this, () => getBroker()); // Bind the parent context. this.parent = ctx; diff --git a/services/logging.js b/services/logging.js index 00e9c523b..47ef93af8 100644 --- a/services/logging.js +++ b/services/logging.js @@ -1,18 +1,18 @@ const { version } = require('../package.json'); const Logger = require('bunyan'); const { LOGGING_LEVEL, REVISION_HASH } = require('../config'); +const logger = new Logger({ + src: true, + name: 'talk', + version, + revision: REVISION_HASH, + level: LOGGING_LEVEL, + serializers: Logger.stdSerializers, +}); // Create the logging instance that all logger's are branched from. function createLogger(name, traceID) { - return new Logger({ - src: true, - name, - traceID, - version, - revision: REVISION_HASH, - level: LOGGING_LEVEL, - serializers: Logger.stdSerializers, - }); + return logger.child({ origin: name, traceID }); } -module.exports = { createLogger }; +module.exports = { logger, createLogger }; From 3415163cfb1cea003b419fe9c19ea3f76c9fd4b4 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 20 Mar 2018 16:07:42 -0600 Subject: [PATCH 2/2] changed name based on review --- graph/context.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/graph/context.js b/graph/context.js index dcbbc5f6b..e9ac7ea61 100644 --- a/graph/context.js +++ b/graph/context.js @@ -51,7 +51,7 @@ const decorateContextPlugins = (context, contextPlugins) => { * @param {Object} ctx the graph proxy * @param {Function} loader the loadable component that should be proxied */ -const proxyContextLoader = (ctx, loader) => +const createLazyContextLoader = (ctx, loader) => new Proxy( { loaded: false, data: null }, { @@ -87,18 +87,18 @@ class Context { this.connectors = connectors; // Create the loaders. - this.loaders = proxyContextLoader(this, loaders); + this.loaders = createLazyContextLoader(this, loaders); // Create the mutators. - this.mutators = proxyContextLoader(this, mutators); + this.mutators = createLazyContextLoader(this, mutators); // Decorate the plugin context. - this.plugins = proxyContextLoader(this, () => + this.plugins = createLazyContextLoader(this, () => decorateContextPlugins(this, contextPlugins) ); // Bind the publish/subscribe to the context. - this.pubsub = proxyContextLoader(this, () => getBroker()); + this.pubsub = createLazyContextLoader(this, () => getBroker()); // Bind the parent context. this.parent = ctx;