Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jawn auth redis cache #1930

Merged
merged 2 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 47 additions & 19 deletions valhalla/jawn/src/lib/db/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PromiseGenericResult, err, ok } from "../shared/result";
import { Database } from "./database.types";
import { hashAuth } from "./hash";
import { HeliconeAuth } from "../requestWrapper";
import { Result } from "../shared/result";
import { redisClient } from "../clients/redisClient";

// SINGLETON
class SupabaseAuthCache extends InMemoryCache {
Expand Down Expand Up @@ -203,45 +203,69 @@ export class SupabaseConnector {
authorization: HeliconeAuth,
organizationId?: string
): AuthResult {
const cachedResult = this.authCache.get<AuthParams>(
await hashAuth(JSON.stringify(authorization) + organizationId)
const cacheKey = await hashAuth(
JSON.stringify(authorization) + organizationId
);

const cachedResult = await redisClient?.get(cacheKey);

if (cachedResult) {
return ok(cachedResult);
try {
const parsedResult: AuthParams = JSON.parse(cachedResult);
return ok(parsedResult);
} catch (e) {
console.error("Failed to parse cached result:", e);
}
}

if (authorization.token.includes("sk-helicone-proxy")) {
authorization._type = "bearerProxy";
}

const result = await this.getAuthParams(authorization);

if (result.error) {
if (result.error || !result.data) {
return err(result.error);
}
const { organizationId: orgId } = result.data!;

const { organizationId: orgId, userId, heliconeApiKeyId } = result.data;

if (!orgId) {
return err("No organization ID");
}

this.authCache.set(
await hashAuth(JSON.stringify(authorization) + organizationId),
{ organizationId: orgId }
const authParamsResult: AuthParams = {
organizationId: orgId,
userId,
heliconeApiKeyId,
};

await redisClient?.set(
cacheKey,
JSON.stringify(authParamsResult),
"EX",
900 // 900 seconds = 15 minutes
);

await redisClient?.set(cacheKey, JSON.stringify(result.data), "EX", 900); // 900 seconds = 15 minutes

return ok({
organizationId: orgId,
});
}

async getOrganization(authParams: AuthParams): OrgResult {
const cachedResult = this.orgCache.get<{
tier: string;
id: string;
percentLog: number;
}>(authParams.organizationId);
async getOrganization(authParams: AuthParams): Promise<OrgResult> {
const cacheKey = `org:${authParams.organizationId}`;

const cachedResult = await redisClient?.get(cacheKey);

if (cachedResult) {
return ok(cachedResult);
try {
const parsedResult: OrgParams = JSON.parse(cachedResult);
return ok(parsedResult);
} catch (e) {
console.error("Failed to parse cached result:", e);
}
}

const { data, error } = await this.client
Expand All @@ -251,14 +275,18 @@ export class SupabaseConnector {
.single();

if (error || !data) {
return err(error.message);
return err(error?.message || "Unknown error");
}

return ok({
const orgResult: OrgParams = {
tier: data.tier ?? "free",
id: data.id ?? "",
percentLog: data.percent_to_log ?? 100_000,
});
};

await redisClient?.set(cacheKey, JSON.stringify(orgResult), "EX", 900); // 900 seconds = 15 minutes

return ok(orgResult);
}
}

Expand Down
Loading