Skip to content

Commit

Permalink
feature: add Cache-Control for fxManager
Browse files Browse the repository at this point in the history
  • Loading branch information
186526 committed Oct 2, 2024
1 parent 443ba8a commit 1d83b91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
32 changes: 26 additions & 6 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -61294,7 +61294,7 @@ var fxmManager = class extends JSONRPCRouter {
this.fxms = {};
this.fxmStatus = {};
this.fxRateGetter = {};
this.intervalIDs = [];
this.intervalIDs = {};
this.rpcHandlers = {
instanceInfo: () => useInternalRestAPI("info", this),
listCurrencies: ({ source }) => {
Expand Down Expand Up @@ -61352,7 +61352,7 @@ var fxmManager = class extends JSONRPCRouter {
rep.body = JSON.stringify({
status: "ok",
sources: Object.keys(this.fxms),
version: `fxrate@${"ba8f9f4"} ${"2024-10-02T18:16:32+08:00"}`,
version: `fxrate@${"443ba8a"} ${"2024-10-02T19:04:48+08:00"}`,
apiVersion: "v1",
environment: import_node_process.default.env.NODE_ENV || "development"
});
Expand Down Expand Up @@ -61380,6 +61380,7 @@ var fxmManager = class extends JSONRPCRouter {
const fxRates = await this.fxRateGetter[source](this);
fxRates.forEach((f) => this.fxms[source].update(f));
this.fxmStatus[source] = "ready";
this.intervalIDs[source].refreshDate = /* @__PURE__ */ new Date();
this.log(`${source} is updated, now is ready.`);
return;
}
Expand All @@ -61395,9 +61396,14 @@ var fxmManager = class extends JSONRPCRouter {
this.fxmStatus[source] = "pending";
this.mountFXMRouter(source);
this.log(`Registered ${source}.`);
this.intervalIDs.push(
setInterval(() => this.updateFXManager(source), 1e3 * 60 * 30)
);
const refreshDate = /* @__PURE__ */ new Date();
this.intervalIDs[source] = {
timeout: setInterval(
() => this.updateFXManager(source),
1e3 * 60 * 30
),
refreshDate
};
}
registerFXM(source, fxManager2) {
this.fxms[source] = fxManager2;
Expand All @@ -61411,6 +61417,16 @@ var fxmManager = class extends JSONRPCRouter {
}
getFXMRouter(source) {
const fxmRouter = new router();
const useCache = (response3) => {
response3.headers.set(
"Cache-Control",
`public, max-age=${30 * 60 - Math.round(
Math.abs(
(this.intervalIDs[source].refreshDate.getTime() - (/* @__PURE__ */ new Date()).getTime()) / 1e3
) % 1800
)}`
);
};
const handlerSourceInfo = async (request3, response3) => {
if (request3.params[0] && request3.params[0] != source) {
return response3;
Expand All @@ -61424,6 +61440,7 @@ var fxmManager = class extends JSONRPCRouter {
date: (/* @__PURE__ */ new Date()).toUTCString()
});
useJson(response3, request3);
useCache(response3);
throw response3;
};
const handlerCurrencyAllFXRates = async (request3, response3) => {
Expand All @@ -61450,6 +61467,7 @@ var fxmManager = class extends JSONRPCRouter {
}
response3.body = JSON.stringify(result);
useJson(response3, request3);
useCache(response3);
return response3;
};
const handlerCurrencyConvert = async (request3, response3) => {
Expand All @@ -61473,6 +61491,7 @@ var fxmManager = class extends JSONRPCRouter {
to
)).toUTCString()
);
useCache(response3);
return response3;
};
const handlerCurrencyConvertAmount = async (request3, response3) => {
Expand All @@ -61498,6 +61517,7 @@ var fxmManager = class extends JSONRPCRouter {
to
)).toUTCString()
);
useCache(response3);
return response3;
};
fxmRouter.binding("/", new handler("GET", [handlerSourceInfo]));
Expand All @@ -61521,7 +61541,7 @@ var fxmManager = class extends JSONRPCRouter {
}
stopAllInterval() {
for (const id of this.intervalIDs) {
clearInterval(id);
clearInterval(id.timeout);
}
}
};
Expand Down
41 changes: 36 additions & 5 deletions src/fxmManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
[source: string]: (fxmManager?: fxmManager) => Promise<FXRate[]>;
} = {};

public intervalIDs: NodeJS.Timeout[] = [];
public intervalIDs: [key: { timeout: NodeJS.Timeout; refreshDate: Date }] =
{} as any;

protected rpcHandlers = {
instanceInfo: () => useInternalRestAPI('info', this),
Expand Down Expand Up @@ -246,6 +247,7 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
const fxRates = await this.fxRateGetter[source](this);
fxRates.forEach((f) => this.fxms[source].update(f));
this.fxmStatus[source] = 'ready';
this.intervalIDs[source].refreshDate = new Date();
this.log(`${source} is updated, now is ready.`);
return;
}
Expand All @@ -266,9 +268,16 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
this.fxmStatus[source] = 'pending';
this.mountFXMRouter(source);
this.log(`Registered ${source}.`);
this.intervalIDs.push(
setInterval(() => this.updateFXManager(source), 1000 * 60 * 30),
);

const refreshDate = new Date();

this.intervalIDs[source] = {
timeout: setInterval(
() => this.updateFXManager(source),
1000 * 60 * 30,
),
refreshDate: refreshDate,
};
}

public registerFXM(source: string, fxManager: fxManager): void {
Expand All @@ -286,6 +295,22 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
private getFXMRouter(source: string): router {
const fxmRouter = new router();

const useCache = (response: response<any>) => {
response.headers.set(
'Cache-Control',
`public, max-age=${
30 * 60 -
Math.round(
Math.abs(
(this.intervalIDs[source].refreshDate.getTime() -
new Date().getTime()) /
1000,
) % 1800,
)
}`,
);
};

const handlerSourceInfo = async (
request: request<any>,
response: response<any>,
Expand All @@ -302,6 +327,7 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
date: new Date().toUTCString(),
});
useJson(response, request);
useCache(response);
throw response;
};

Expand Down Expand Up @@ -341,6 +367,7 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
}
response.body = JSON.stringify(result);
useJson(response, request);
useCache(response);
return response;
};

Expand Down Expand Up @@ -374,6 +401,8 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
)
).toUTCString(),
);
useCache(response);

return response;
};

Expand Down Expand Up @@ -409,6 +438,8 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {
)
).toUTCString(),
);
useCache(response);

return response;
};

Expand Down Expand Up @@ -439,7 +470,7 @@ class fxmManager extends JSONRPCRouter<any, any, JSONRPCMethods> {

public stopAllInterval(): void {
for (const id of this.intervalIDs) {
clearInterval(id);
clearInterval(id.timeout);
}
}
}
Expand Down

0 comments on commit 1d83b91

Please sign in to comment.