Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/controller/GroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ export class GroundController {
tokenConfig = new TokenConfiguration();
tokenConfig.token = body.token;
tokenConfig.os = body.os;
await this.tokenConfigurationRepository.save(tokenConfig);
try {
await this.tokenConfigurationRepository.save(tokenConfig);
} catch (error) {
if (error?.code !== "ER_DUP_ENTRY") throw error;
// lost a create race: a concurrent request already inserted this (token, os); use that row
tokenConfig = await this.tokenConfigurationRepository.findOneBy({ token: body.token, os: body.os });
}
}

const config: paths["/getTokenConfiguration"]["post"]["responses"]["200"]["content"]["application/json"] = {
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ dataSource
(app as any)[route.method](route.route, (req: Request, res: Response, next: Function) => {
const result = new (route.controller as any)(c)[route.action](req, res, next);
if (result instanceof Promise) {
result.then((result) => (result !== null && result !== undefined ? res.send(result) : undefined));
result
.then((result) => (result !== null && result !== undefined ? res.send(result) : undefined))
.catch((error) => {
// without this catch a rejected handler promise becomes an unhandled rejection and kills the process
console.error(`error handling ${route.route}:`, error);
if (!res.headersSent) {
res.status(500).send("");
}
});
} else if (result !== null && result !== undefined) {
res.json(result);
}
Expand Down
43 changes: 43 additions & 0 deletions src/tests/GroundController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,5 +439,48 @@ describe("GroundController", () => {
app_version: "1.0.0",
});
});

it("should return existing configuration when concurrent request created it first (ER_DUP_ENTRY)", async () => {
const concurrentlyCreatedConfig = {
level_all: true,
level_transactions: false,
level_price: true,
level_news: false,
level_tips: true,
redacted: false,
lang: "es",
app_version: "2.0.0",
};
const duplicateError: any = new Error("Duplicate entry 'test-token:ios' for key 'IDX_40ed4dd221d0ff99647ddfa6ec'");
duplicateError.code = "ER_DUP_ENTRY";
duplicateError.errno = 1062;

mockRepository.findOneBy.mockResolvedValueOnce(null).mockResolvedValueOnce(concurrentlyCreatedConfig);
mockRepository.save.mockRejectedValueOnce(duplicateError);

const result = await groundController.getTokenConfiguration(mockRequest, mockResponse, mockNext);

expect(mockRepository.findOneBy).toHaveBeenCalledTimes(2);
expect(result).toEqual({
level_all: true,
level_transactions: false,
level_price: true,
level_news: false,
level_tips: true,
redacted: false,
lang: "es",
app_version: "2.0.0",
});
});

it("should rethrow non-duplicate save errors", async () => {
const dbError: any = new Error("Connection lost");
dbError.code = "PROTOCOL_CONNECTION_LOST";

mockRepository.findOneBy.mockResolvedValue(null);
mockRepository.save.mockRejectedValueOnce(dbError);

await expect(groundController.getTokenConfiguration(mockRequest, mockResponse, mockNext)).rejects.toThrow("Connection lost");
});
});
});
8 changes: 7 additions & 1 deletion src/worker-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ dataSource
tokenConfig = new TokenConfiguration();
tokenConfig.os = payload.os;
tokenConfig.token = payload.token;
await tokenConfigurationRepository.save(tokenConfig);
try {
await tokenConfigurationRepository.save(tokenConfig);
} catch (error) {
if (error?.code !== "ER_DUP_ENTRY") throw error;
// lost a create race with the API or another worker; use the existing row
tokenConfig = await tokenConfigurationRepository.findOneBy({ os: payload.os, token: payload.token });
}
}

let unsubscribed = false;
Expand Down
Loading