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

Writing to group table from asynchronous function; using ctx.SetValue causes a WaitGroup panic #443

Open
dschreij opened this issue Nov 23, 2023 · 0 comments

Comments

@dschreij
Copy link

In the past few days, I have been struggling with this problem. I have a processor function that uses a persist edge. At some point in this function, I must offload some work to a goroutine, and the result of the work should be written to the group table from within this goroutine. However, if I use ctx.SetValue for this, I get this panic: panic: sync: negative WaitGroup counter. I found this issue that explains the cause of this and it suggests the following (among some other solutions below that):

If you want to use a goroutine, then it's better to create a producer and pass the producer and message to the goroutine (but not the goka.Context). It may be also safer cloning the message.

but are there any examples on how to do this? Is a producer basically a standalone emitter that is not bound to the context (that is the solution I have in mind currently for this problem). The other suggested solutions also do not really solve my problem as they involve blocking the process until the goroutine finishes, and that is not an option for me. The purpose of my function is to debounce calls to another API until after a certain time, e.g.:

func (h *messageHandler) updateUser(ctx goka.Context, user, prevUser *mdl.User) {
	if h.timer != nil {
		h.timer.Stop()
	}

	h.timer = time.AfterFunc(h.debounceTime, func() {
		if _, err := h.hs.UpdateContact(user, prevUser); err != nil {
			log.Error().Err(err).Interface("user", user).Msg("failed to update user")
		} else {
			log.Info().Msgf("Updated user %d (%s)", user.Id, user.HubspotContactId)
                        ctx.SetValue(user)
		}
        }
}

Consecutive calls to this updateUser function cancel eachother out, until there hasn't been a call for a certain time (h.debounceTime). If I block the updateUser function until this time has passed this construct is not possible.

I only want to write the user to the group table once the call to the remote API has succeeded, so the current context value always contains the user version that has last been successfully sent to the API.
Does anybody have any words of wisdoms or advice on this? :)

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

No branches or pull requests

1 participant