Skip to content

Commit

Permalink
fix: limit followChainStream to 100 to prevent OOM on host node (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks authored Aug 30, 2023
1 parent 4c0c520 commit 974c29c
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,36 @@ class LightBlockCache {
const head = await this.get("head");
const rpc = await ifClient.getClient();
const followChainStreamParams = head ? { head: head.toString() } : {};
const stream = await rpc.chain.followChainStream({
...followChainStreamParams,
serialized: true,
});

for await (const content of stream.contentStream()) {
if (content.block.sequence % 1000 === 0) {
logger.info(`Caching block ${content.block.sequence}`);
}
if (content.type === "connected") {
if (content.block.sequence % 1000 === 0) {
logger.info(
`Caching block ${content.block.sequence}`,
new Date().toLocaleString(),

// eslint-disable-next-line no-constant-condition
while (true) {
const stream = await rpc.chain.followChainStream({
...followChainStreamParams,
serialized: true,
limit: 100,
});

for await (const content of stream.contentStream()) {
if (content.type === "connected") {
if (content.block.sequence % 1000 === 0) {
logger.info(
`Caching block ${content.block.sequence}`,
new Date().toLocaleString(),
);
}
const hash = content.block.hash;
await this.db.put("head", hash);
await this.db.put(
hash,
LightBlock.encode(lightBlock(content)).finish(),
);
await this.db.put(content.block.sequence.toString(), hash);
} else if (content.type === "disconnected") {
logger.warn(`Removing block ${content.block.sequence}...`);
await this.db.put("head", content.block.previous.toString());
await this.db.del(content.block.sequence);
await this.db.del(content.block.hash);
}
const hash = content.block.hash;
await this.db.put("head", hash);
await this.db.put(
hash,
LightBlock.encode(lightBlock(content)).finish(),
);
await this.db.put(content.block.sequence.toString(), hash);
} else if (content.type === "disconnected") {
logger.warn(`Removing block ${content.block.sequence}...`);
await this.db.put("head", content.block.previous.toString());
await this.db.del(content.block.sequence);
await this.db.del(content.block.hash);
}
}
}
Expand Down

0 comments on commit 974c29c

Please sign in to comment.