-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce in-memory HTTP cached copying from the spike branch
Refs: #2186, spike/effect-http-client
- Loading branch information
Showing
7 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
Headers, | ||
HttpClient, | ||
HttpClientResponse, | ||
UrlParams, | ||
type HttpClientError, | ||
type HttpClientRequest, | ||
} from '@effect/platform' | ||
import { diff } from 'deep-object-diff' | ||
import { DateTime, Effect, pipe, type Scope } from 'effect' | ||
import * as HttpCache from './HttpCache.js' | ||
|
||
export const CachingHttpClient: Effect.Effect< | ||
HttpClient.HttpClient, | ||
never, | ||
HttpCache.HttpCache | HttpClient.HttpClient | ||
> = Effect.gen(function* () { | ||
const httpClient = yield* HttpClient.HttpClient | ||
const cache = yield* HttpCache.HttpCache | ||
|
||
const cachingBehaviour = ( | ||
request: Effect.Effect<HttpClientRequest.HttpClientRequest>, | ||
): Effect.Effect<HttpClientResponse.HttpClientResponse, HttpClientError.HttpClientError, Scope.Scope> => | ||
Effect.gen(function* () { | ||
const timestamp = yield* DateTime.now | ||
const req = yield* request | ||
const key = keyForRequest(req) | ||
const response = yield* cache.get(key) | ||
|
||
if (response) { | ||
if (DateTime.lessThan(timestamp, response.staleAt)) { | ||
yield* Effect.logDebug('Cache hit') | ||
} else { | ||
yield* Effect.logDebug('Cache stale') | ||
yield* Effect.forkDaemon( | ||
Effect.gen(function* () { | ||
yield* pipe( | ||
req, | ||
httpClient.execute, | ||
Effect.tap(response => | ||
cache.set(key, { staleAt: DateTime.addDuration(timestamp, '10 seconds'), response }), | ||
), | ||
Effect.scoped, | ||
) | ||
}), | ||
) | ||
} | ||
return HttpClientResponse.fromWeb( | ||
req, | ||
new Response(response.response.body, { | ||
status: response.response.status, | ||
headers: Headers.fromInput(response.response.headers), | ||
}), | ||
) | ||
} else { | ||
yield* Effect.logDebug('Cache miss') | ||
} | ||
|
||
return yield* pipe( | ||
req, | ||
httpClient.execute, | ||
Effect.tap(response => | ||
pipe(cache.set(key, { staleAt: DateTime.addDuration(timestamp, '10 seconds'), response }), Effect.ignore), | ||
), | ||
Effect.tap(response => | ||
Effect.gen(function* () { | ||
const cachedValue = yield* cache.get(key) | ||
if (cachedValue === undefined) { | ||
return yield* Effect.logError('cache entry not found') | ||
} | ||
const origResponse = { | ||
status: response.status, | ||
headers: { ...response.headers }, | ||
body: yield* response.text, | ||
} | ||
const cachedResponse = { | ||
status: cachedValue.response.status, | ||
headers: cachedValue.response.headers, | ||
body: cachedValue.response.body, | ||
} | ||
const difference = diff(origResponse, cachedResponse) | ||
function replacer(_: unknown, value: unknown) { | ||
if (value == undefined) { | ||
return null | ||
} | ||
return value | ||
} | ||
if (Object.keys(difference).length !== 0) { | ||
return yield* Effect.logError('cached response does not equal original').pipe( | ||
Effect.annotateLogs({ diff: JSON.parse(JSON.stringify(difference, replacer)) }), | ||
) | ||
} | ||
}), | ||
), | ||
) | ||
}) | ||
|
||
return HttpClient.makeWith(cachingBehaviour, Effect.succeed) | ||
}) | ||
|
||
const keyForRequest = (request: HttpClientRequest.HttpClientRequest): HttpCache.CacheKey => { | ||
const url = new URL(request.url) | ||
url.search = UrlParams.toString(request.urlParams) | ||
|
||
return url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Headers, type HttpClientResponse } from '@effect/platform' | ||
import { Context, type DateTime, Effect, Layer, pipe, Schema } from 'effect' | ||
|
||
interface CacheValue { | ||
staleAt: DateTime.DateTime | ||
response: StoredResponse | ||
} | ||
|
||
interface CacheInput { | ||
staleAt: DateTime.DateTime | ||
response: HttpClientResponse.HttpClientResponse | ||
} | ||
|
||
export type CacheKey = URL | ||
|
||
type StoredResponse = typeof StoredResponseSchema.Encoded | ||
|
||
const StoredResponseSchema = Schema.Struct({ | ||
status: Schema.Number, | ||
headers: Headers.schema, | ||
body: Schema.String, | ||
}) | ||
|
||
export class HttpCache extends Context.Tag('HttpCache')< | ||
HttpCache, | ||
{ | ||
get: (key: CacheKey) => Effect.Effect<CacheValue | undefined> | ||
set: (key: CacheKey, value: CacheInput) => Effect.Effect<void, Error> | ||
delete: (key: CacheKey) => Effect.Effect<void> | ||
} | ||
>() {} | ||
|
||
export const layer = Layer.sync(HttpCache, () => { | ||
const cache = new Map<string, CacheValue>() | ||
return { | ||
get: key => Effect.succeed(cache.get(key.href)), | ||
set: (key, input) => | ||
pipe( | ||
Effect.gen(function* () { | ||
return { | ||
status: input.response.status, | ||
headers: input.response.headers, | ||
body: yield* input.response.text, | ||
} | ||
}), | ||
Effect.andThen(Schema.encode(StoredResponseSchema)), | ||
Effect.andThen(storedResponse => { | ||
cache.set(key.href, { staleAt: input.staleAt, response: storedResponse }) | ||
}), | ||
), | ||
delete: key => Effect.succeed(cache.delete(key.href)), | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters