-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-sync): fetch and store logs (#2003)
- Loading branch information
Showing
4 changed files
with
94 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@latticexyz/store-sync": major | ||
--- | ||
|
||
Previously, all `store-sync` strategies were susceptible to a potential memory leak where the stream that fetches logs from the RPC would get ahead of the stream that stores the logs in the provided storage adapter. We saw this most often when syncing to remote Postgres servers, where inserting records was much slower than we retrieving them from the RPC. In these cases, the stream would build up a backlog of items until the machine ran out of memory. | ||
|
||
This is now fixed by waiting for logs to be stored before fetching the next batch of logs from the RPC. To make this strategy work, we no longer return `blockLogs$` (stream of logs fetched from RPC but before they're stored) and instead just return `storedBlockLogs$` (stream of logs fetched from RPC after they're stored). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FetchLogsOptions, fetchLogs, groupLogsByBlockNumber } from "@latticexyz/block-logs-stream"; | ||
import { StoreEventsAbi } from "@latticexyz/store"; | ||
import { StorageAdapter, StorageAdapterBlock, StoreEventsLog } from "./common"; | ||
|
||
type FetchAndStoreLogsOptions = FetchLogsOptions<StoreEventsAbi> & { | ||
storageAdapter: StorageAdapter; | ||
logFilter?: (log: StoreEventsLog) => boolean; | ||
}; | ||
|
||
export async function* fetchAndStoreLogs({ | ||
storageAdapter, | ||
logFilter, | ||
...fetchLogsOptions | ||
}: FetchAndStoreLogsOptions): AsyncGenerator<StorageAdapterBlock> { | ||
for await (const { logs, toBlock } of fetchLogs(fetchLogsOptions)) { | ||
const blocks = groupLogsByBlockNumber(logFilter ? logs.filter(logFilter) : logs, toBlock); | ||
for (const block of blocks) { | ||
await storageAdapter(block); | ||
yield block; | ||
} | ||
} | ||
} |