All files / centro/lib/server_extensions backoff.js

100% Statements 46/46
100% Branches 20/20
100% Functions 18/18
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                  1x 1x                         1x   88x   22x     22x   1501x           66x     66x   190348x     66x   70x         87780x   87780x   12045x   12045x   12045x       75735x       66x   22x   66x     22x   88x     22x   22x     22x   87604x           66x   22x   22x   87582x   87582x     2407x   2407x     2407x     85175x         44x   22x   22x   192701x   192701x     105119x   105119x     105119x       87582x         66x                                                
/*eslint-env node */
 
/**
 * Centro extension for defining {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsbackoff|backoff} event behaviour.
 *
 * @module centro-js/lib/server_extensions/backoff
 */
"use strict";
 
var wu = require('wu'),
    bo_prop = 'centro_server_extension_backoff';
 
/**
 * Attach behaviour to the {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsbackoff|backoff} event.
 *
 * @param {Object} config - Configuration options.
 * @param {boolean} [config.close_conn] - Close connection when a `backoff` event occurs.
 * @param {boolean} [config.delay_responses] - When a `backoff` event occurs, delay responses to subscribe, unsubscribe and publish requests from the client until a {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsdrain|drain} event occurs.
 * @param {boolean} [config.skip_message] - When a `backoff` event occurs, drop messages that would be sent to the client until a `drain` event occurs.
 * @param {centro-js/lib/server_extensions/backoff.skipCallback} [config.on_skip] - Called when a message is dropped.
 * @param {boolean} [config.delay_message] - When a `backoff` event occurs, postpone messages that would be sent to the client until a `drain` event occurs.
 * @param {centro-js/lib/server_extensions/backoff.delayCallback} [config.on_delay] - Called when a message is postponed.
 */
exports.backoff = function (config)
{
    if (config.close_conn)
    {
        return {
            pre_connect: function (info)
            {
                info.mqserver.on('backoff', function ()
                {
                    info.destroy();
                });
            }
        };
    }
 
    var r = {
        pre_connect: function (info)
        {
            info.mqserver.on('backoff', function ()
            {
                this[bo_prop] = true;
            });
 
            info.mqserver.on('drain', function ()
            {
                this[bo_prop] = false;
            });
 
            function delay_response(cb)
            {
                return function ()
                {
                    if (info.mqserver[bo_prop])
                    {
                        var args = Array.prototype.slice.call(arguments);
 
                        return info.mqserver.once('drain', function ()
                        {
                            cb.apply(this, args);
                        });
                    }
 
                    cb.apply(this, arguments);
                };
            }
 
            if (config.delay_responses)
            {
                this.pipeline(info.mqserver, 'subscribe_requested', function (topic, cb, next)
                {
                    next(topic, delay_response(cb));
                });
 
                this.pipeline(info.mqserver, 'unsubscribe_requested', function (topic, cb, next)
                {
                    next(topic, delay_response(cb));
                });
 
                this.pipeline(info.mqserver, 'unsubscribe_all_requested', function (cb, next)
                {
                    next(delay_response(cb));
                });
 
                this.pipeline(info.mqserver, 'publish_requested', function (topic, stream, options, cb, next)
                {
                    next(topic, stream, options, delay_response(cb));
                });
            }
        }
    };
 
    if (config.skip_message)
    {
        r.ready = function ()
        {
            this.fsq.filters.push(function (info, handlers, cb)
            {
                cb(null, true, wu(handlers).filter(function (h)
                {
                    if (h.mqlobber_server && h.mqlobber_server[bo_prop])
                    {
                        /* istanbul ignore else */
                        if (config.on_skip)
                        {
                            config.on_skip(info, handlers, h);
                        }
 
                        return false;
                    }
 
                    return true;
                }));
            });
        };
    }
    else if (config.delay_message)
    {
        r.ready = function ()
        {
            this.fsq.filters.push(function (info, handlers, cb)
            {
                for (var h of handlers)
                {
                    if (h.mqlobber_server && h.mqlobber_server[bo_prop])
                    {
                        /* istanbul ignore else */
                        if (config.on_delay)
                        {
                            config.on_delay(info, handlers, h);
                        }
 
                        return cb(null, false);
                    }
                }
 
                return cb(null, true, handlers);
            });
        };
    }
 
    return r;
};
 
/*eslint-disable no-unused-vars */
 
/**
 * Callback type for dropping a message.
 *
 * @callback skipCallback
 * @memberof centro-js/lib/server_extensions/backoff
 * @param {Object} info - Metadata for the message. See {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsmessagestream-info-multiplex-done|MQlobber.events.message} for details.
 */
/* istanbul ignore next */
exports._skipCallback = function (info) {};
 
/**
 * Callback type for postponing a message.
 *
 * @callback delayCallback
 * @memberof centro-js/lib/server_extensions/backoff
 * @param {Object} info - Metadata for the message. See {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsmessagestream-info-multiplex-done|MQlobber.events.message} for details.
 */
/* istanbul ignore next */
exports._delayCallback = function (info) {};