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

Expose after_start and before_stop in runtime::Builder #756

Merged
merged 1 commit into from
Nov 19, 2018

Conversation

bkchr
Copy link
Contributor

@bkchr bkchr commented Nov 15, 2018

This new function is called by tokio around each thread that is started.
This functionality is useful, when the user needs to setup certain
conditions for each new thread.

Motivation

Hey, I require to setup each thread that is started by tokio. In my case I want to call to Java and that requires that a thread is attached to the JVM. By using the new around_thread function, I'm able to setup each thread easily.

Solution

Added a function to runtime::Builder. The function accepts a closure that will be given another closure that represents the thread. The outer closure is required to call the inner closure, otherwise the thread would not do any work.

@bkchr
Copy link
Contributor Author

bkchr commented Nov 15, 2018

Hmm the test failure looks like another error, maybe restarting the job could solve it?

@tobz
Copy link
Member

tobz commented Nov 15, 2018

So I can't restart AppVeyor runs myself, unfortunately, but had a question: any reason you opted to create a new mechanism for this instead of threading through to tokio_threadpool::Builder::around_worker?

@bkchr
Copy link
Contributor Author

bkchr commented Nov 15, 2018

Yeah, if you look here you see that tokio already itself uses the functionality.
And there was already an issue, saying that it does not work: #321
Which makes clearly sense, if you look into the code :)

@carllerche
Copy link
Member

@bkchr do before and after hooks work for you? Those are more conservative and would be easier to add.

@bkchr
Copy link
Contributor Author

bkchr commented Nov 16, 2018

@carllerche I tested another solution and that should work with before and after hooks. I would update this pull request to forward before and after to the threadpool used by the builder. Would that be okay?

@carllerche
Copy link
Member

@bkchr That would be a good solution as long as no tokio-threadpool API (types) leak out. The goal is to be able to bump the version of tokio-threadpool internally w/o a breaking change.

@bkchr bkchr changed the title Adds around_thread to the runtime::Builder Expose after_start and before_stop in runtime::Builder Nov 19, 2018
@bkchr
Copy link
Contributor Author

bkchr commented Nov 19, 2018

@carllerche I updated the pr to expose after_start and before_stop in runtime::Builder and removed the around_thread stuff.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Docs and a test 👍 Looks great to me.

@carllerche carllerche merged commit d3dca45 into tokio-rs:master Nov 19, 2018
@bkchr bkchr deleted the around-thread branch November 19, 2018 19:55
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.

3 participants