From e42cb59661b0981a4f33d21dcead257e85018ea5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 4 Feb 2020 00:21:14 +0000 Subject: [PATCH] feat: added config variable for perspective timeout (#2817) Added a configuration timeout variable for changing the perspective timeout number. --- src/core/server/config.ts | 18 ++++++++++++++++-- .../services/comments/pipeline/phases/toxic.ts | 9 +++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/core/server/config.ts b/src/core/server/config.ts index 0e6034c17..812a7e430 100644 --- a/src/core/server/config.ts +++ b/src/core/server/config.ts @@ -51,9 +51,15 @@ convict.addFormat({ convict.addFormat({ name: "ms", validate: (val: number) => { - Joi.assert(val, Joi.number().min(0)); + Joi.assert( + val, + Joi.number() + .positive() + .integer() + .required() + ); }, - coerce: (val: string) => ms(val), + coerce: (val: string): number => ms(val), }); const algorithms = [ @@ -255,6 +261,14 @@ const config = convict({ env: "SCRAPE_TIMEOUT", arg: "scrapeTimeout", }, + perspective_timeout: { + doc: + "The request timeout (in ms) for perspective comment checking operations.", + format: "ms", + default: "800 milliseconds", + env: "PERSPECTIVE_TIMEOUT", + arg: "perspectiveTimeout", + }, disable_force_ssl: { doc: "Disables forcing SSL in production environments. Should not be used except for testing.", diff --git a/src/core/server/services/comments/pipeline/phases/toxic.ts b/src/core/server/services/comments/pipeline/phases/toxic.ts index afd924096..9b81fdd5f 100644 --- a/src/core/server/services/comments/pipeline/phases/toxic.ts +++ b/src/core/server/services/comments/pipeline/phases/toxic.ts @@ -1,5 +1,4 @@ import { isNil } from "lodash"; -import ms from "ms"; import fetch from "node-fetch"; import path from "path"; import { URL } from "url"; @@ -32,12 +31,13 @@ export const toxic: IntermediateModerationPhase = async ({ nudge, log, htmlStripped, + config, // FEATURE_FLAG:DISABLE_WARN_USER_OF_TOXIC_COMMENT comment: { body }, }: Pick< ModerationPhaseContext, // FEATURE_FLAG:DISABLE_WARN_USER_OF_TOXIC_COMMENT - "tenant" | "nudge" | "log" | "htmlStripped" | "comment" + "tenant" | "nudge" | "log" | "htmlStripped" | "comment" | "config" >): Promise => { if (!htmlStripped) { return; @@ -89,8 +89,9 @@ export const toxic: IntermediateModerationPhase = async ({ ); } - // TODO: (wyattjoh) replace hardcoded default with config. - const timeout = ms("800ms"); + // This typecast is needed because the custom `ms` format does not return the + // desired `number` type even though that's the only type it can output. + const timeout = (config.get("perspective_timeout") as unknown) as number; try { // FEATURE_FLAG:DISABLE_WARN_USER_OF_TOXIC_COMMENT