Use Promise chaining to enforce read/write queue#699
Merged
Conversation
delucis
reviewed
May 16, 2020
Member
delucis
left a comment
There was a problem hiding this comment.
Thanks @jabza! Do you think we could add a layer of abstraction to make this less verbose?
Something like:
this.request = <T extends any = any>(action: () => Promise<T>): Promise<T> => {
this.requests = this.requests.then(action, action);
return this.requests
}So we can use it like this:
result.state = (await this.request(() => this.games.getItem(gameID))) as State;Or even better in terms of code clarity might be if you created a separate wrapper for get and set, so we could just do:
result.state = (await this.getRequest(gameID)) as State;What do you think?
Contributor
Author
|
Went with your first option. Pushed that change now |
jabza
pushed a commit
to jabza/boardgame.io
that referenced
this pull request
May 20, 2020
Use Promise chaining to enforce read/write queue (boardgameio#699)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A problem in node-persist, used by FlatFile, means multiple async read/writes to the same file are allowed (no file-locking) and can and does cause corruption (which may or may not be caught) in higher frequency situations.
This PR uses a Promise chain to ensure only one async read/write is performed at a time. The queue is rather crude, but does guarantee no multiple writes (if only one FlatFile instance is being used on that directory).
In the future it would be nice to have Promise chain per-file, or seek alternatives to node-persist.
However, for now, this solution has been working for my own project and it's probably worth the added protection vs the potential performance loss.
Checklist
master).Closes #694