wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
'use strict';
 
var assert = require('assert-plus');
var GatewayTimeoutError = require('restify-errors').GatewayTimeoutError;
 
/**
 * Request Expiry can be used to throttle requests that have already exceeded
 * their client timeouts. Requests can be sent with a configurable client
 * timeout header, e.g. 'x-request-expiry-time', which gives in absolute ms
 * since epoch, when this request will be timed out by the client.
 *
 * This plugin will throttle all incoming requests via a 504 where
 * 'x-request-expiry-time' less than Date.now() -- since these incoming requests
 * have already been timed out by the client. This prevents the server from
 * processing unnecessary requests.
 *
 * Request expiry will use headers to tell if the incoming request has expired.
 * There are two options for this plugin:
 *  1. Absolute Time
 *     * Time in Milliseconds since Epoch when this request should be
 *     considered expired
 *  2. Timeout
 *     * The request start time is supplied
 *     * A timeout, in milliseconds, is given
 *     * The timeout is added to the request start time to arrive at the
 *       absolute time in which the request is considered expired
 *
 * #### Using an external storage mechanism for key/bucket mappings.
 *
 * By default, the restify throttling plugin uses an in-memory LRU to store
 * mappings between throttling keys (i.e., IP address) to the actual bucket that
 * key is consuming.  If this suits you, you can tune the maximum number of keys
 * to store in memory with `options.maxKeys`; the default is 10000.
 *
 * In some circumstances, you want to offload this into a shared system, such as
 * Redis, if you have a fleet of API servers and you're not getting steady
 * and/or uniform request distribution.  To enable this, you can pass in
 * `options.tokensTable`, which is simply any Object that supports `put` and
 * `get` with a `String` key, and an `Object` value.
 *
 * @public
 * @function requestExpiry
 * @param    {Object} opts - an options object
 * @param    {String} [opts.absoluteHeader] - The header key to be used for
 *                                   the expiry time of each request.
 * @param    {String} opts.startHeader - The header key for the start time
 *                                   of the request.
 * @param    {String} opts.timeoutHeader - The header key for the time in
 *                                   milliseconds that should ellapse before
 *                                   the request is considered expired.
 * @returns  {Function} Handler
 * @example
 * <caption>
 * The only option provided is `header` which is the request header used
 * to specify the client timeout.
 * </caption>
 * server.use(restify.plugins.requestExpiry({
 *     header: 'x-request-expiry-time'
 * });
 */
function requestExpiry(opts) {
    assert.object(opts, 'opts');
    assert.optionalString(opts.absoluteHeader, 'opts.absoluteHeader');
 
    if (!opts.absoluteHeader) {
        assert.string(opts.startHeader, 'opts.startHeader');
        assert.string(opts.timeoutHeader, 'opts.timeoutHeader');
    }
 
    var useAbsolute = opts.absoluteHeader !== undefined;
    var absoluteHeaderKey = opts.absoluteHeader;
    var startHeaderKey = opts.startHeader;
    var timeoutHeaderKey = opts.timeoutHeader;
 
    return function requestExpirationCheck(req, res, next) {
        /*
         * Add check expiry API to to req if it doesn't already exist. We only
         * add this the first time this handler is run, since this handler
         * could be used in multiple places in the handler chain.
         */
        if (!req._expiryTime) {
            // if the headers don't exist, then the request will never expire.
            req._expiryTime = Infinity;
 
            if (useAbsolute) {
                var expiryTime = parseInt(req.headers[absoluteHeaderKey], 10);
 
                if (!isNaN(expiryTime)) {
                    req._expiryTime = expiryTime;
                }
            } else {
                // Use the start time header and add the timeout header to it
                // to arrive at the expiration time
                var startTime = parseInt(req.headers[startHeaderKey], 10);
                var timeout = parseInt(req.headers[timeoutHeaderKey], 10);
 
                if (!isNaN(startTime) && !isNaN(timeout)) {
                    req._expiryTime = startTime + timeout;
                }
            }
 
            req.isExpired = function isExpired() {
                return Date.now() > req._expiryTime;
            };
        }
 
        if (req.isExpired()) {
            // The request has expired
            return next(
                new GatewayTimeoutError({
                    message: 'Request has expired',
                    context: {
                        expiryTime: req._expiryTime,
                        mode: opts.absoluteHeader ? 'absolute' : 'relative'
                    }
                })
            );
        } else {
            // Happy case
            return next();
        }
    };
}
 
module.exports = requestExpiry;