Skip to content

Commit

Permalink
상태 저장 db를 redis대신 dynamodb로 교체
Browse files Browse the repository at this point in the history
redis의 상태 체크용 코드가 redis에 의존하는건 이상한거같아서
  • Loading branch information
if1live committed Sep 30, 2023
1 parent b10d854 commit aa8e4d3
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 89 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@
},
"devDependencies": {
"@types/aws-lambda": "^8.10.122",
"@types/ioredis-mock": "^8.2.3",
"@types/node": "^18.18.1",
"@types/pg": "^8.10.3",
"cross-env": "^7.0.3",
"esbuild": "^0.14.54",
"ioredis-mock": "^8.9.0",
"madge": "^6.1.0",
"prettier": "^3.0.3",
"rimraf": "^4.4.1",
Expand Down
67 changes: 8 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ provider:
- "dynamodb:PutItem"
- "dynamodb:DeleteItem"
- "dynamodb:UpdateItem"
- "dynamodb:BatchWriteItem"
- "dynamodb:Scan"
Resource:
- arn:aws:dynamodb:${self:provider.region}:${aws:accountId}:table/AyaneKeyValue

Expand Down
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Hono } from "hono";
import { compress } from "hono/compress";
import { HTTPException } from "hono/http-exception";
import { engine, redis } from "./instances.js";
import { engine, dynamodb } from "./instances.js";
import { touch } from "./services.js";
import { deleteResult, loadSortedResults } from "./stores.js";

Expand All @@ -10,7 +10,7 @@ export const app = new Hono();
app.use("*", compress());

app.get("*", async (c) => {
const results = await loadSortedResults(redis);
const results = await loadSortedResults(dynamodb);
const entries = results.map((x) => {
return {
...x,
Expand All @@ -31,7 +31,7 @@ app.post("/touch", async (c) => {
});

app.delete("/delete", async (c) => {
await deleteResult(redis);
await deleteResult(dynamodb);
return c.json({ ok: true });
});

Expand Down
30 changes: 30 additions & 0 deletions src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
import { standalone, FunctionDefinition } from "serverless-standalone";
import {
CreateTableCommand,
ResourceInUseException,
} from "@aws-sdk/client-dynamodb";
import * as handlers from "./handlers.js";
import { dynamodb } from "./instances.js";

const prepare = async () => {
try {
const result = await dynamodb.send(
new CreateTableCommand({
TableName: "AyaneKeyValue",
AttributeDefinitions: [{ AttributeName: "label", AttributeType: "S" }],
KeySchema: [{ AttributeName: "label", KeyType: "HASH" }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
}),
);
} catch (e: unknown) {
if (e instanceof ResourceInUseException) {
// ResourceInUseException: Cannot create preexisting table
// console.log(`${e.name}: ${e.message}`);
} else {
console.error(e);
throw e;
}
}
};
await prepare();

const definitions: FunctionDefinition[] = [
{
Expand Down
30 changes: 24 additions & 6 deletions src/instances.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import { Redis } from "ioredis";
import { Liquid } from "liquidjs";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import * as settings from "./settings.js";

export const redis = new Redis(settings.REDIS_URL!, {
lazyConnect: true,
});
await redis.connect();

export const engine = new Liquid({
root: settings.viewPath,
extname: ".liquid",
cache: settings.NODE_ENV === "production",
});

const createDynamoDB_localhost = () => {
return new DynamoDBClient({
endpoint: "http://localhost:8000",
region: "ap-northeast-1",
credentials: {
accessKeyId: "local",
secretAccessKey: "local",
},
});
};

const createDynamoDB_prod = () => {
return new DynamoDBClient({});
};

const createDynamoDB = () => {
return settings.NODE_ENV === "production"
? createDynamoDB_prod()
: createDynamoDB_localhost();
};

export const dynamodb = createDynamoDB();
2 changes: 1 addition & 1 deletion src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const wrapSettled = async <T>(
try {
const value = await execute();
return { status: "fulfilled", value, at };
} catch (e: any) {
} catch (e: unknown) {
return { status: "rejected", reason: e as Error, at };
}
};
Expand Down
10 changes: 5 additions & 5 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ import {
UpstashRedisInput,
} from "./types.js";
import { providerInputs } from "./settings.js";
import { redis } from "./instances.js";
import { dynamodb } from "./instances.js";

const execute_mysql = async (input: MysqlInput) => {
const { label } = input;
const result = await touchMysqlSettled(input);
await saveResult(redis, label, result);
await saveResult(dynamodb, label, result);
return { label, result };
};

const execute_postgres = async (input: PostgresInput) => {
const { label } = input;
const result = await touchPostgresSettled(input);
await saveResult(redis, label, result);
await saveResult(dynamodb, label, result);
return { label, result };
};

const execute_redisNative = async (input: RedisNativeInput) => {
const { label } = input;
const result = await touchRedisNativeSettled(input);
await saveResult(redis, label, result);
await saveResult(dynamodb, label, result);
return { label, result };
};

const execute_upstashRedis = async (input: UpstashRedisInput) => {
const { label } = input;
const result = await touchUpstashRedisSettled(input);
await saveResult(redis, label, result);
await saveResult(dynamodb, label, result);
return { label, result };
};

Expand Down
Loading

0 comments on commit aa8e4d3

Please sign in to comment.