Skip to content

Conversation

@hawkw
Copy link
Member

@hawkw hawkw commented Nov 1, 2019

This commit adds set_default drop guard style APIs for setting the
default reactor, executor, and timer. These are similar to the APIs used
in tokio 0.2

In addition to having potentially better ergonomics than the
with_default closure APIs, the drop-guard based APIs will be helpful
in rewriting the tokio-compat crate to wrap the existing tokio 0.2
runtime, rather than constructing its own runtime.

Because the runtime does not expose an around_worker API, it cannot
currently be used with the 0.1 with_default method of setting the
reactor, timer, and executor. This means that tokio-compat must
duplicate a lot of existing code from tokio to construct the runtime,
which is unfortunate (and has the potential to introduce errors). On the
other hand, we can use the drop guard APIs with before_start and
after_stop, by storing the drop guards in a thread-local. This will
allow tokio-compat to wrap the 0.2 runtime, reducing code duplication.
Also, this will allow the blocking pool to be used on the compat
runtime, which is currently impossible (as the blocking APIs are private
to tokio).

Signed-off-by: Eliza Weisman eliza@buoyant.io

@hawkw hawkw self-assigned this Nov 1, 2019
@hawkw
Copy link
Member Author

hawkw commented Nov 1, 2019

The "Test async-await" CI failure seems unrelated?

@GeorgeHahn
Copy link

Could set_default be implemented for Clock as well?

I'm excited to see this PR! I have been thinking about building something like this to improve our libevent-tokio integration. This new API will allow us to have tokio defaults set when we are executing outside of a tokio context.

(For context, we have a very large amount of C that runs outside of a tokio runtime context. This C code may call into Rust via FFI or callbacks. We must avoid calling tokio functions in these cases because the missing defaults will cause a panic.)

@hawkw
Copy link
Member Author

hawkw commented Nov 5, 2019

@GeorgeHahn Yup, added clock::set_default as well, thanks for pointing that out!

@hawkw
Copy link
Member Author

hawkw commented Nov 6, 2019

PR #1742 will fix the broken CI builds for this branch, which are unrelated to the rest of this change. When that branch merges, I'll rebase this one and merge it, unless there are any objections?

hawkw added 7 commits November 6, 2019 14:48
This commit adds `set_default` drop guard style APIs for setting the
default reactor, executor, and timer. These are similar to the APIs used
in `tokio` 0.2

In addition to having potentially better ergonomics than the
`with_default` closure APIs, the drop-guard based APIs will be helpful
in rewriting the `tokio-compat` crate to wrap the existing tokio 0.2
runtime, rather than constructing its own runtime.

Because the runtime does not expose an `around_worker` API, it cannot
currently be used with the 0.1 `with_default` method of setting the
reactor, timer, and executor. This means that tokio-compat must
duplicate a lot of existing code from `tokio` to construct the runtime,
which is unfortunate (and has the potential to introduce errors). On the
other hand, we can use the drop guard APIs with `before_start` and
`after_stop`, by storing the drop guards in a thread-local. This will
allow `tokio-compat` to wrap the 0.2 runtime, reducing code duplication.
Also, this will allow the blocking pool to be used on the compat
runtime, which is currently impossible (as the blocking APIs are private
to `tokio`).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Using `try_with` prevents double panics from occurring when thread
locals have already been destroyed

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@hawkw hawkw force-pushed the eliza/set-default branch from 993cf0d to 4906d99 Compare November 6, 2019 22:48
@hawkw hawkw merged commit 22b7bd2 into v0.1.x Nov 6, 2019
hawkw added a commit that referenced this pull request Nov 7, 2019
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 7, 2019
# 0.1.9 (November 7, 2019)

### Added
- Add `executor::set_default` which behaves like `with_default` but returns a
  drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 7, 2019
# 0.2.12 (November 7, 2019)

### Added
- `timer::set_default`, which functions like `timer::with_default`, but
  returns a drop guard (#1725).
- `clock::set_default`, which functions like `clock::with_default`, but
  returns a drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 7, 2019
# 0.1.11 (November 7, 2019)

### Added
- `set_default`, which functions like `with_default` but returns a drop
  guard (#1725)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 7, 2019
# 0.2.12 (November 7, 2019)

### Added
- `timer::set_default`, which functions like `timer::with_default`, but
  returns a drop guard (#1725).
- `clock::set_default`, which functions like `clock::with_default`, but
  returns a drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 7, 2019
# 0.1.9 (November 7, 2019)

### Added
- Add `executor::set_default` which behaves like `with_default` but returns a
  drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
@taiki-e taiki-e deleted the eliza/set-default branch November 16, 2019 07:08
hawkw added a commit that referenced this pull request Nov 27, 2019
# 0.1.11 (November 7, 2019)

### Added
- `set_default`, which functions like `with_default` but returns a drop
  guard (#1725)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 27, 2019
# 0.2.12 (November 7, 2019)

### Added
- `timer::set_default`, which functions like `timer::with_default`, but
  returns a drop guard (#1725).
- `clock::set_default`, which functions like `clock::with_default`, but
  returns a drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw added a commit that referenced this pull request Nov 27, 2019
# 0.1.9 (November 7, 2019)

### Added
- Add `executor::set_default` which behaves like `with_default` but returns a
  drop guard (#1725).

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
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.

4 participants