|
| 1 | +/*! |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +export interface TeenyStatisticsOptions { |
| 18 | + /** |
| 19 | + * A positive number representing when to issue a warning about the number |
| 20 | + * of concurrent requests using teeny-request. |
| 21 | + * Set to 0 to disable this warning. |
| 22 | + * Corresponds to the TEENY_REQUEST_WARN_CONCURRENT_REQUESTS environment |
| 23 | + * variable. |
| 24 | + */ |
| 25 | + concurrentRequests?: number; |
| 26 | +} |
| 27 | + |
| 28 | +type TeenyStatisticsConfig = Required<TeenyStatisticsOptions>; |
| 29 | + |
| 30 | +/** |
| 31 | + * TeenyStatisticsCounters is distinct from TeenyStatisticsOptions: |
| 32 | + * Used when dumping current counters and other internal metrics. |
| 33 | + */ |
| 34 | +export interface TeenyStatisticsCounters { |
| 35 | + concurrentRequests: number; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @class TeenyStatisticsWarning |
| 40 | + * @extends Error |
| 41 | + * @description While an error, is used for emitting warnings when |
| 42 | + * meeting certain configured thresholds. |
| 43 | + * @see process.emitWarning |
| 44 | + */ |
| 45 | +export class TeenyStatisticsWarning extends Error { |
| 46 | + static readonly CONCURRENT_REQUESTS = 'ConcurrentRequestsExceededWarning'; |
| 47 | + |
| 48 | + public threshold = 0; |
| 49 | + public type = ''; |
| 50 | + public value = 0; |
| 51 | + |
| 52 | + /** |
| 53 | + * @param {string} message |
| 54 | + */ |
| 55 | + constructor(message: string) { |
| 56 | + super(message); |
| 57 | + this.name = this.constructor.name; |
| 58 | + Error.captureStackTrace(this, this.constructor); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @class TeenyStatistics |
| 64 | + * @description Maintain various statistics internal to teeny-request. Tracking |
| 65 | + * is not automatic and must be instrumented within teeny-request. |
| 66 | + */ |
| 67 | +export class TeenyStatistics { |
| 68 | + /** |
| 69 | + * @description A default threshold representing when to warn about excessive |
| 70 | + * in-flight/concurrent requests. |
| 71 | + * @type {number} |
| 72 | + * @static |
| 73 | + * @readonly |
| 74 | + * @default 5000 |
| 75 | + */ |
| 76 | + static readonly DEFAULT_WARN_CONCURRENT_REQUESTS = 5000; |
| 77 | + |
| 78 | + /** |
| 79 | + * @type {TeenyStatisticsConfig} |
| 80 | + * @private |
| 81 | + */ |
| 82 | + private _options: TeenyStatisticsConfig; |
| 83 | + |
| 84 | + /** |
| 85 | + * @type {number} |
| 86 | + * @private |
| 87 | + * @default 0 |
| 88 | + */ |
| 89 | + private _concurrentRequests = 0; |
| 90 | + |
| 91 | + /** |
| 92 | + * @type {boolean} |
| 93 | + * @private |
| 94 | + * @default false |
| 95 | + */ |
| 96 | + private _didConcurrentRequestWarn = false; |
| 97 | + |
| 98 | + /** |
| 99 | + * @param {TeenyStatisticsOptions} [opts] |
| 100 | + */ |
| 101 | + constructor(opts?: TeenyStatisticsOptions) { |
| 102 | + this._options = TeenyStatistics._prepareOptions(opts); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Returns a copy of the current options. |
| 107 | + * @return {TeenyStatisticsOptions} |
| 108 | + */ |
| 109 | + getOptions(): TeenyStatisticsOptions { |
| 110 | + return Object.assign({}, this._options); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Change configured statistics options. This will not preserve unspecified |
| 115 | + * options that were previously specified, i.e. this is a reset of options. |
| 116 | + * @param {TeenyStatisticsOptions} [opts] |
| 117 | + * @returns {TeenyStatisticsConfig} The previous options. |
| 118 | + * @see _prepareOptions |
| 119 | + */ |
| 120 | + setOptions(opts?: TeenyStatisticsOptions): TeenyStatisticsConfig { |
| 121 | + const oldOpts = this._options; |
| 122 | + this._options = TeenyStatistics._prepareOptions(opts); |
| 123 | + return oldOpts; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @readonly |
| 128 | + * @return {TeenyStatisticsCounters} |
| 129 | + */ |
| 130 | + get counters(): TeenyStatisticsCounters { |
| 131 | + return { |
| 132 | + concurrentRequests: this._concurrentRequests, |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @description Should call this right before making a request. |
| 138 | + */ |
| 139 | + requestStarting(): void { |
| 140 | + this._concurrentRequests++; |
| 141 | + |
| 142 | + if ( |
| 143 | + this._options.concurrentRequests > 0 && |
| 144 | + this._concurrentRequests >= this._options.concurrentRequests && |
| 145 | + !this._didConcurrentRequestWarn |
| 146 | + ) { |
| 147 | + this._didConcurrentRequestWarn = true; |
| 148 | + const warning = new TeenyStatisticsWarning( |
| 149 | + 'Possible excessive concurrent requests detected. ' + |
| 150 | + this._concurrentRequests + |
| 151 | + ' requests in-flight, which exceeds the configured threshold of ' + |
| 152 | + this._options.concurrentRequests + |
| 153 | + '. Use the TEENY_REQUEST_WARN_CONCURRENT_REQUESTS environment ' + |
| 154 | + 'variable or the concurrentRequests option of teeny-request to ' + |
| 155 | + 'increase or disable (0) this warning.' |
| 156 | + ); |
| 157 | + warning.type = TeenyStatisticsWarning.CONCURRENT_REQUESTS; |
| 158 | + warning.value = this._concurrentRequests; |
| 159 | + warning.threshold = this._options.concurrentRequests; |
| 160 | + process.emitWarning(warning); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @description When using `requestStarting`, call this after the request |
| 166 | + * has finished. |
| 167 | + */ |
| 168 | + requestFinished() { |
| 169 | + // TODO negative? |
| 170 | + this._concurrentRequests--; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Configuration Precedence: |
| 175 | + * 1. Dependency inversion via defined option. |
| 176 | + * 2. Global numeric environment variable. |
| 177 | + * 3. Built-in default. |
| 178 | + * This will not preserve unspecified options previously specified. |
| 179 | + * @param {TeenyStatisticsOptions} [opts] |
| 180 | + * @returns {TeenyStatisticsOptions} |
| 181 | + * @private |
| 182 | + */ |
| 183 | + private static _prepareOptions({ |
| 184 | + concurrentRequests: diConcurrentRequests, |
| 185 | + }: TeenyStatisticsOptions = {}): TeenyStatisticsConfig { |
| 186 | + let concurrentRequests = this.DEFAULT_WARN_CONCURRENT_REQUESTS; |
| 187 | + |
| 188 | + const envConcurrentRequests = Number( |
| 189 | + process.env.TEENY_REQUEST_WARN_CONCURRENT_REQUESTS |
| 190 | + ); |
| 191 | + if (diConcurrentRequests !== undefined) { |
| 192 | + concurrentRequests = diConcurrentRequests; |
| 193 | + } else if (!Number.isNaN(envConcurrentRequests)) { |
| 194 | + concurrentRequests = envConcurrentRequests; |
| 195 | + } |
| 196 | + |
| 197 | + return {concurrentRequests}; |
| 198 | + } |
| 199 | +} |
0 commit comments