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

Submit should return error if the pool is stopped #98

Open
cemremengu opened this issue Jan 29, 2025 · 2 comments
Open

Submit should return error if the pool is stopped #98

cemremengu opened this issue Jan 29, 2025 · 2 comments

Comments

@cemremengu
Copy link

cemremengu commented Jan 29, 2025

After stopping the pool, submit calls still return a task which contains an error but to view the error you need to wait for the task (which we don't want to do every time).

Submit should return error as separate value independent of the task instead so that we can let the user know whether the submission failed or not.

Is there already a way to do this? I can work on a pull request if this is ok for you.

@alitto
Copy link
Owner

alitto commented Jan 30, 2025

Hey @cemremengu,

Yes, that is the current semantics of the different variations of the Submit methods. Could you provide a code example of the proposed semantics?
At the moment, errors are consolidated via the task's Wait method to simplify the program flow on the caller side. If we were to return the "pool stopped" error in the call to Submit, then the caller would need to handle 2 different error flows.

@cemremengu
Copy link
Author

cemremengu commented Jan 31, 2025

I see your point. The idea here is that one error will belong to the task (whether the task executed successfully or not) the other one will belong to submission (whether the task was successfully accepted; the pool can be stopped, full etc.).

So two different errors are actually communicate different things.

// Create a pool that accepts tasks that return a string and an error
pool := pond.NewResultPool[string](10)

// Submit a task that returns a string or a submission error
task, err := pool.Submit(func() (string) {
	return "Hello, World!"
})

if err != nil {
  // Submission failed for some reason, we don't need to wait for the result and can immediately notify the caller 
  // (for example a rest api call that requested some async processing)
  
  // without this error we always need to wait for the task which may return an error immediately 
  // or block our call until done
  return err 
}

// At this point we are sure that the task was submitted so we can wait for the task to complete and get the result
// Or we can just let the caller know that the task was submitted successfully and it is being processed. 
result, err := task.Wait()
// result = "Hello, World!" and err = nil

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

2 participants