From f63764a691fd4636780c7b4e404d56f22bddebdc Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Dec 2024 16:06:38 +0000 Subject: [PATCH] Make calculations easier to read Refs #1834 --- src/ExpressServer.ts | 4 ++-- src/Redis.ts | 7 +++++-- src/WebApp.ts | 4 ++-- src/fetch.ts | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/ExpressServer.ts b/src/ExpressServer.ts index 0fcfca1ae..aef380f5f 100644 --- a/src/ExpressServer.ts +++ b/src/ExpressServer.ts @@ -1,6 +1,6 @@ import { FetchHttpClient } from '@effect/platform' import KeyvRedis from '@keyv/redis' -import { Effect, Redacted } from 'effect' +import { Duration, Effect, Redacted } from 'effect' import Keyv from 'keyv' import { app } from './app.js' import { DeprecatedEnvVars, DeprecatedLoggerEnv, DeprecatedSleepEnv, ExpressConfig, SessionSecret } from './Context.js' @@ -102,7 +102,7 @@ export const ExpressConfigLive = Effect.gen(function* () { emitErrors: false, namespace: 'sessions', store: createKeyvStore(), - ttl: 1000 * 60 * 60 * 24 * 30, + ttl: Duration.toMillis('30 days'), }), slackOauth: { authorizeUrl: new URL('https://slack.com/oauth/v2/authorize'), diff --git a/src/Redis.ts b/src/Redis.ts index 895eda540..a7e451167 100644 --- a/src/Redis.ts +++ b/src/Redis.ts @@ -1,10 +1,13 @@ -import { Config, Context, Effect, flow, Inspectable, Layer, pipe, Redacted, Runtime } from 'effect' +import { Config, Context, Duration, Effect, flow, Inspectable, Layer, pipe, Redacted, Runtime } from 'effect' import { Redis as IoRedis } from 'ioredis' const makeRedis = (url: Redacted.Redacted) => Effect.gen(function* () { const runtime = yield* Effect.runtime() - const redis = new IoRedis(Redacted.value(url).href, { commandTimeout: 2 * 1000, enableAutoPipelining: true }) + const redis = new IoRedis(Redacted.value(url).href, { + commandTimeout: Duration.toMillis('2 seconds'), + enableAutoPipelining: true, + }) const runSync = Runtime.runSync(runtime) diff --git a/src/WebApp.ts b/src/WebApp.ts index 20d7eb6cc..d356346fc 100644 --- a/src/WebApp.ts +++ b/src/WebApp.ts @@ -10,7 +10,7 @@ import { Path, } from '@effect/platform' import cookieSignature from 'cookie-signature' -import { Cause, Config, Effect, flow, Layer, Option, pipe, Redacted, Schema } from 'effect' +import { Cause, Config, Duration, Effect, flow, Layer, Option, pipe, Redacted, Schema } from 'effect' import { StatusCodes } from 'http-status-codes' import { Express, ExpressConfig, FlashMessage, Locale, SessionSecret } from './Context.js' import { ExpressHttpApp } from './ExpressHttpApp.js' @@ -61,7 +61,7 @@ const serveStaticFiles = HttpMiddleware.make(app => return yield* HttpServerResponse.setHeader( response, 'Cache-Control', - `public, max-age=${60 * 60 * 24 * 365}, immutable`, + `public, max-age=${Duration.toSeconds('365 days')}, immutable`, ) }), ) diff --git a/src/fetch.ts b/src/fetch.ts index 704694333..58096ff01 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,3 +1,4 @@ +import { Duration } from 'effect' import type * as F from 'fetch-fp-ts' import { constVoid } from 'fp-ts/lib/function.js' import type { Json } from 'fp-ts/lib/Json.js' @@ -32,7 +33,7 @@ export function revalidateIfStale(): (env: E) = openRequests.add(url) void env - .sleep(Math.min(200 * openRequests.size, 30_000))() + .sleep(Duration.toMillis(Duration.min(Duration.times('0.2 seconds', openRequests.size), '30 seconds')))() .then(() => env.fetch(url, { ...init, cache: 'no-cache' })) .then(response => response.text()) .catch(constVoid)