Skip to content

Memory leak in content.queryFull #98

Description

@lukechinworth

Seems to be a memory leak in the queryFull method on the content collection. I'm guessing it's due to unresolved promises in populateAll middleware

Here is a version of populateAll that I cleaned up. The memory leak persists in this version but seems to be lessened by half or so.

module.exports = function(kontx, next) {
    var contentKey = kontx._content.results ? 'results' : 'fields';

    return getContentReferences(kontx._content[contentKey])
        .then(function(populatedContent) {
            // add populated content to kontx
            kontx._content[contentKey] = populatedContent;
        })
        .then(next)
        .catch(function(error) {
            logger.error('Query all error');
            logger.error(error);
        });
};

function getContentReferences(value) {
    if (_.isArray(value)) {

        return BB.map(value, getContentReferences)

    } else if (_.isObject(value)) {

        return BB.props(_.reduce(value, function(acc, v, key) {
            acc[key] = getContentReferences(v);

            return acc;
        }, {}));

    } else if (_.isString(value) && maybeMongodId.test(value)) {

        return queryReference(value);

    } else {
        return value;
    }
}

function queryReference(value) {

    return db.content.getById(value)
        .then(function(response) {
            return getContentReferences(response.fields);
        })
        .catch(function() {
            // if something goes wrong, leave the reference untouched
            logger.error('Query Full: could not find - ' + value);
        });
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions