Skip to content

Commit

Permalink
feat(sdk): use @shopify/semaphore instead of async-sema
Browse files Browse the repository at this point in the history
  • Loading branch information
icepeng committed Dec 15, 2022
1 parent 75f395d commit 7010c4e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-scissors-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mokoko/sdk": patch
---

use @shopify/semaphore instead of async-sema
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"typescript": "4.9.3"
},
"dependencies": {
"async-sema": "^3.1.1"
"@shopify/semaphore": "^3.0.1"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 4 additions & 5 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
RequestInit as NodeRequestInit,
Response as NodeResponse,
} from "node-fetch";
import { Sema } from "async-sema";
import { Semaphore } from "@shopify/semaphore";
import { Reporter } from "./reporter";

const baseUrl = "https://developer-lostark.game.onstove.com";
Expand Down Expand Up @@ -75,12 +75,12 @@ export function getSDK({
maxRetry = 3,
reporter,
}: SdkProps) {
const sema = new Sema(limit);
const sema = new Semaphore(limit);
const RPS = 60 * 1000;

async function rateLimit() {
await sema.acquire();
setTimeout(() => sema.release(), RPS);
const permit = await sema.acquire();
setTimeout(() => permit.release(), RPS);
}

async function _request({
Expand All @@ -103,7 +103,6 @@ export function getSDK({
const url = baseUrl + path + queryStr;

reporter?.info(`Request ${url}`);
reporter?.info(`Queued jobs ${sema.nrWaiting()}`);
const res = await fetchFn(url, {
method,
body: body ? JSON.stringify(body) : undefined,
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@mokoko/sdk@workspace:packages/sdk"
dependencies:
async-sema: ^3.1.1
"@shopify/semaphore": ^3.0.1
node-fetch: ^3.3.0
tsup: ^6.5.0
typescript: 4.9.3
Expand Down Expand Up @@ -413,6 +413,13 @@ __metadata:
languageName: node
linkType: hard

"@shopify/semaphore@npm:^3.0.1":
version: 3.0.1
resolution: "@shopify/semaphore@npm:3.0.1"
checksum: ea2cab85150c41705ffc8117192900457e3c1dbbbbd7d72c72bad8c9e2c1fb88eb0d1e027920d2517588b13083744c3057cbdb8f145883b2b04b17246e23254d
languageName: node
linkType: hard

"@tootallnate/once@npm:2":
version: 2.0.0
resolution: "@tootallnate/once@npm:2.0.0"
Expand Down Expand Up @@ -618,13 +625,6 @@ __metadata:
languageName: node
linkType: hard

"async-sema@npm:^3.1.1":
version: 3.1.1
resolution: "async-sema@npm:3.1.1"
checksum: 07b8c51f6cab107417ecdd8126b7a9fe5a75151b7f69fdd420dcc8ee08f9e37c473a217247e894b56e999b088b32e902dbe41637e4e9b594d3f8dfcdddfadc5e
languageName: node
linkType: hard

"balanced-match@npm:^1.0.0":
version: 1.0.2
resolution: "balanced-match@npm:1.0.2"
Expand Down

0 comments on commit 7010c4e

Please sign in to comment.