-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for clearing doc from DocHandle #365
Conversation
…released and memory freed, without deleting document from storage
Thanks for putting a bunch of work into this and the follow up PRs. I'm going to post my comments here but this feedback also relates to the linked work. The addition of a new state to the As to how to the time based cache expiration policy. I think there are some tactical problems with it - the |
Sounds good, agree with not having another state in The intention of Thanks! |
Just a note that I'm going to let @alexjg be the reviewer for this one (unless he asks for help) since he's already up to speed. I'm mostly done refactoring in this area -- I am considering retiring xstate just to reduce dependencies (cc/ @HerbCaudill), but I'll try and wait until after this lands. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a few small comments. I have a bigger question which is bothering me. Currently it's quite common for people to depend on this idea that when a DocHandle
is ready then the application can assume it will always be ready. For example, code like this is quite common:
const handle = repo.find(...)
await handle.whenReady()
console.log(handle.docSync()) // <-- this line depends on the document never becoming unready
Introducing a transition back to IDLE
breaks this assumption and it's the reason that in #358 @yarolegovich spent a lot of time trying to find a way to avoid evicting handles which the application is known to be using.
I think that finding a solution to this problem is orthogonal to having a way to transition in the first place though so I think that - once the comments I've made are addressed - we can do that later.
@@ -426,6 +434,11 @@ export class DocHandle<T> extends EventEmitter<DocHandleEvents<T>> { | |||
this.#machine.send({ type: DELETE }) | |||
} | |||
|
|||
/** Called by the repo to free memory used by the document. */ | |||
reset() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that rather than calling this reset
we should all it idle
as I think that more clearly describes what it actally does.
JS is single threaded -- as long as you don't yield, the above code is indeed safe. This is also a benefit of the promise-oriented API. Overall though, your point that introducing non-monotonic loading behaviour could have pitfalls is true but unavoidable. I think ideally, most people will never encounter this behaviour, but we might in fact want a new handle state (or to rename the state to perhaps UNLOADED) so that we can give better error messages if it does happen. |
It does seem like we need a new state if we want to provide good error messages, apologies for making you go around in circles. I think we should go for a new state called |
Great - that sounds good, I'll make that change. Do you think |
I think we should definitely support reloading via |
Cool, @alexjg just saw your comment - I actually just pushed 3 versions:
Happy to go with any of these |
@georgewsu I likke |
Summary:
Add support for clearing doc from DocHandle so that reference can be released and memory freed, without deleting document from storage
Issue:
#330
Background:
After creating enough documents in a repo, out of memory errors will cause a synch server to crash because memory isn't being freed. Repo holds references to each DocHandle in handleCache and each DocHandle holds a reference to the automerge doc.
Proposed solution:
https://github.com/georgewsu/automerge-repo/tree/gsu/cache-eviction-test2 has code for an initial implementation of cache eviction and releasing document reference so that memory can be freed. I've tested this with a sync server running locally.
Part 1:
This PR only covers introducing a new state and event to DocHandle to allow clearing the document.