Skip to content
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

Do not store context inside structs #190

Closed
rolznz opened this issue Mar 7, 2024 · 4 comments · Fixed by getAlby/nostr-wallet-connect-next#141
Closed

Do not store context inside structs #190

rolznz opened this issue Mar 7, 2024 · 4 comments · Fixed by getAlby/nostr-wallet-connect-next#141
Assignees

Comments

@rolznz
Copy link
Contributor

rolznz commented Mar 7, 2024

See getAlby/nostr-wallet-connect-next#92 (comment)

@rolznz rolznz self-assigned this Mar 7, 2024
@bumi bumi assigned rdmitr and unassigned rolznz Mar 19, 2024
@rdmitr
Copy link
Collaborator

rdmitr commented Mar 20, 2024

Looking closer at this issue, I realized the current implementation should probably be left as is.

First of all, the rule of thumb in Go is to pass context as a parameter to methods instead of storing it in structs.

However, in NWC, the context is stored within Service. The intention, apparently, is to make it possible to abort currently running requests when the app is stopped and the parent context is cancelled. Since we have potentially long-running requests (i.e. requests going to the Lightning network), it does make sense. On the other hand, it only seems to be used in LND, because other clients call Rust code through FFI and Go contexts do not work there.

@rdmitr rdmitr linked a pull request Mar 20, 2024 that will close this issue
@rolznz
Copy link
Contributor Author

rolznz commented Mar 21, 2024

I think there are still some places where we can reduce the usage of the context stored in the service - by moving the signal.NotifyContext to the entrypoints (main_http and main_wails) and pass it as a parameter further down, rather than creating it in the service. There are some places in the api where we use svc.ctx instead of passing a context in (e.g. the HTTP service could pass the echo request context to the api functions such as ListChannels)

Would that sound like a good idea to you?

@rdmitr
Copy link
Collaborator

rdmitr commented Mar 21, 2024

I think there are still some places where we can reduce the usage of the context stored in the service - by moving the signal.NotifyContext to the entrypoints (main_http and main_wails) and pass it as a parameter further down, rather than creating it in the service

Right, that makes sense. I moved it to main_http. However, I am not so sure about Wails — do we need to handle signals there? Doesn't Wails handle shutdown on its own?

There are some places in the api where we use svc.ctx instead of passing a context in (e.g. the HTTP service could pass the echo request context to the api functions such as ListChannels)

That could work, actually. First I thought it was impossible, since echo.Context is totally not the same as golang's Context. Then I realised that we still can get the request through echo.Context's Request() method, and then get the request's context, which should propagate cancellation when either a client drops connection or the server is shutdown.

I will implement that!

@rdmitr
Copy link
Collaborator

rdmitr commented Mar 21, 2024

I have implemented the proposed changes.

I tried to remove the context from the service entirely, but that turned out to be much trickier. The service's context is used as the parent context when starting the API in the /api/start endpoint. We cannot use request context instead, because it is cancelled when the client connection is closed.

@im-adithya im-adithya transferred this issue from getAlby/nostr-wallet-connect-next Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants