Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
geropl committed Nov 14, 2023
1 parent 04a6032 commit 3624764
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions components/server/src/authorization/spicedb-authorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as grpc from "@grpc/grpc-js";
import { isFgaChecksEnabled, isFgaWritesEnabled } from "./authorizer";
import { base64decode } from "@jmondi/oauth2-server";
import { DecodedZedToken } from "@gitpod/spicedb-impl/lib/impl/v1/impl.pb";
import { ctxGetCache, ctxSetCache } from "../util/request-context";
import { ctxTryGetCache, ctxTrySetCache } from "../util/request-context";
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";

async function tryThree<T>(errMessage: string, code: (attempt: number) => Promise<T>): Promise<T> {
Expand Down Expand Up @@ -242,10 +242,10 @@ interface ZedTokenCache {
// "contribute" a cache shape to the request context
type ZedTokenCacheType = StoredZedToken;
const ctxCacheSetZedToken = (zedToken: StoredZedToken | undefined): void => {
ctxSetCache<ZedTokenCacheType>("zedToken", zedToken);
ctxTrySetCache<ZedTokenCacheType>("zedToken", zedToken);
};
const ctxCacheGetZedToken = (): StoredZedToken | undefined => {
return ctxGetCache<ZedTokenCacheType>("zedToken");
return ctxTryGetCache<ZedTokenCacheType>("zedToken");
};

/**
Expand Down
15 changes: 10 additions & 5 deletions components/server/src/util/request-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,22 @@ export function ctxSignal() {

/** Encode cache keys in type to avoid clashes at compile time */
type CacheKey = "zedToken";
export function ctxGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
return ctxGet().cache[key] || d;
export function ctxTryGetCache<T extends Object>(key: CacheKey, d: T | undefined = undefined): T | undefined {
return ctxTryGet()?.cache[key] || d;
}

type UpdateCache<T> = (prev: T | undefined) => T | undefined;
export function ctxSetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
export function ctxTrySetCache<T extends Object>(key: CacheKey, value: T | undefined | UpdateCache<T>) {
const cache = ctxTryGet()?.cache;
if (!cache) {
return;
}

if (typeof value === "function") {
const prev = ctxGetCache<T>(key);
const prev = ctxTryGetCache<T>(key);
value = value(prev);
}
ctxGet().cache[key] = value;
cache[key] = value;
}

export type RequestContextSeed = Omit<RequestContext, "requestId" | "startTime" | "cache"> & {
Expand Down

0 comments on commit 3624764

Please sign in to comment.