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

refactor(libwaku): async #3180

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

refactor(libwaku): async #3180

wants to merge 2 commits into from

Conversation

richard-ramos
Copy link
Member

Description

Allows the execution of libwaku functions asynchronisly instead of executing each request linearly in the event loop of runWaku.

Copy link

github-actions bot commented Nov 21, 2024

You can find the image built from this PR at

quay.io/wakuorg/nwaku-pr:3180

Built from 6d81f18

Copy link
Collaborator

@Ivansete-status Ivansete-status left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the magic! 🥳
I just added a few comments that I hope you find useful :)

setupForeignThreadGc()

body
template checkLibwakuParams*(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also move this template to the ffi_types module

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this template depends on WakuContext we would end up with a import cycle.

let msg = $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return nil
discard waku_thread
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think is interesting to handle any possible error, and in case of error, return nil :)

Comment on lines 153 to 158
let res = waku_thread.destroyWakuThread(ctx)
if res.isErr():
foreignThreadGc:
let msg = "libwaku error: " & $res.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use the isOkOr and also return RET_OK if all goes well ;P

Suggested change
let res = waku_thread.destroyWakuThread(ctx)
if res.isErr():
foreignThreadGc:
let msg = "libwaku error: " & $res.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
waku_thread.destroyWakuThread(ctx).isOkOr:
foreignThreadGc:
let msg = "libwaku error: " & $error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
return RET_OK

@@ -28,22 +29,79 @@ type RequestType* {.pure.} = enum
type InterThreadRequest* = object
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from this PR but I think it would make more sense to rename this type to WakuThreadRequest, if you agree :)

deallocShared(request)
let fireSyncRes = request[].doneSignal.fireSync()
if fireSyncRes.isErr():
let msg = "libwaku error: " & $fireSyncRes.error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give a little bit more context

Suggested change
let msg = "libwaku error: " & $fireSyncRes.error
let msg = "libwaku error: handleRes fireSyncRes error: " & $fireSyncRes.error


proc deallocOnDone*(T: type InterThreadRequest, request: ptr InterThreadRequest) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could make this private and invoke it within a defer block inside the handleRes proc.

Suggested change
proc deallocOnDone*(T: type InterThreadRequest, request: ptr InterThreadRequest) =
proc deallocOnDone(T: type InterThreadRequest, request: ptr InterThreadRequest) =

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm not sure how at the moment. I'm using this deallocOnDone() function as a way to get the functions in libwaku.nim to 'wait' until there's a result and dispose of the waku request afterwards 🤔

)
return

discard request[].doneSignal.close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could handle any possible error

Suggested change
discard request[].doneSignal.close()
request[].doneSignal.close().isOkOr:
foreignThreadGc:
let msg = "libwaku error: " & $res.error
request[].callback(
RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), request[].userData
)

return

discard request[].doneSignal.close()
deallocShared(request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move that line to the top of the proc within a defer block


proc process*(
T: type InterThreadRequest, request: ptr InterThreadRequest, waku: ptr Waku
) {.async.} =
echo "Request received: " & $request[].reqType
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR but I think we can remove this line:)


proc handleRes[T: string | void](
res: Result[T, string], callback: WakuCallBack, userData: pointer
proc handleSentToChannelRes(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems as if this proc is no longer needed because we are handling the responses within the waku_thread_request module. I see some room for simplification by completely removing that one :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a small refactor. Should simplify a bit the code

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 this pull request may close these issues.

2 participants