-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse_aperture.ts
More file actions
79 lines (67 loc) · 2.08 KB
/
use_aperture.ts
File metadata and controls
79 lines (67 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
ApertureClient,
Flow,
FlowStatus,
LookupStatus,
} from "@fluxninja/aperture-js";
import grpc from "@grpc/grpc-js";
import express from "express";
// Create aperture client
export const apertureClient = new ApertureClient({
address:
process.env.APERTURE_AGENT_ADDRESS !== undefined
? process.env.APERTURE_AGENT_ADDRESS
: "localhost:8089",
apiKey: process.env.APERTURE_API_KEY || undefined,
// if process.env.APERTURE_AGENT_INSECURE set channelCredentials to insecure
channelCredentials:
process.env.APERTURE_AGENT_INSECURE !== undefined
? grpc.credentials.createInsecure()
: grpc.credentials.createSsl(),
});
export const apertureRoute = express.Router();
apertureRoute.get("/", async (_: express.Request, res: express.Response) => {
const labels: Record<string, string> = { user: "kenobi" };
const startTimestamp = Date.now();
let flow: Flow;
flow = await apertureClient.startFlow("awesomeFeature", {
labels: labels,
grpcCallOptions: {
deadline: Date.now() + 30000,
},
rampMode: false,
resultCacheKey: "cache",
});
const endTimestamp = Date.now();
console.log(`Flow took ${endTimestamp - startTimestamp}ms`);
if (flow.shouldRun()) {
await sleep(200);
if (flow.resultCache().getLookupStatus() === LookupStatus.Hit) {
console.log("Cache hit:", flow.resultCache().getValue()?.toString());
} else {
console.log("Cache miss:", flow.resultCache().getError()?.message);
const resString = "awesomeString";
// create a new buffer
const buffer = Buffer.from(resString);
// set cache value
const setResult = await flow.setResultCache({
value: buffer,
ttl: {
seconds: 30,
nanos: 0,
},
});
if (setResult.getError()) {
console.log(`Error setting cache value: ${setResult.getError()}`);
}
}
res.sendStatus(202);
} else {
flow.setStatus(FlowStatus.Error);
res.sendStatus(403);
}
flow.end();
});
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}