Skip to content

Commit

Permalink
[Blog] Publish
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed May 19, 2024
1 parent eff3019 commit 927e94e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Blog/blog/2024-05-05-redis-kafka-data-sink.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ image: https://www.sea-ql.org/SeaStreamer/img/SeaStreamer%20banner.png
tags: [news]
---

<a href="https://www.sea-ql.org/SeaStreamer/"><img src="https://www.sea-ql.org/SeaStreamer/img/SeaStreamer%20banner.png" /></a>
<img src="https://www.sea-ql.org/SeaStreamer/img/SeaStreamer%20banner.png" />

This tutorial shows you how to use Rust to build a system that:

Expand Down
14 changes: 11 additions & 3 deletions Blog/blog/2024-05-20-async-rainbow-bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ image: https://www.sea-ql.org/blog/img/async-rainbow-bridge.png
tags: [news]
---

<img src="/blog/img/async-rainbow-bridge.png" />

This story stems from the saying "What Color is Your Function?" as a criticism to the async implementation of common programming languages. Well, Rust also falls into the category of "colored functions". So in this blog post, let's see how we can design systems to effectively combine sync and async code.

Rainbow bridge is a reference to the bridge in Thor that teleports you between different realms - a perfect analogy!
Expand Down Expand Up @@ -107,7 +109,12 @@ Full source code can be found [here](https://github.com/SeaQL/FireDBG.Rust.Testb

Next, we'll migrate to async land. Using [tokio::sync::mpsc](https://docs.rs/tokio/latest/tokio/sync/mpsc/index.html), it's very similar to the above example, except every operation is `async` and thus imposes additional restrictions to lifetimes. (The trick is, just move / clone. Don't borrow)

Strangely `tokio`'s `unbounded_channel` is the equivalent to `std`'s `channel`. Otherwise it's very similar. The `spawn` method takes in a `Future`; since the worker needs to take in the channels, we construct an async closure with `async move {}`.
`tokio`'s `unbounded_channel` is the equivalent to `std`'s `channel`. Otherwise it's very similar. The `spawn` method takes in a `Future`; since the worker needs to take in the channels, we construct an async closure with `async move {}`.

| std | tokio |
|-------|-------|
| (unbounded) [`channel`](https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html) | [`unbounded_channel`](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.unbounded_channel.html)
| [`sync_channel`](https://doc.rust-lang.org/std/sync/mpsc/fn.sync_channel.html) | (bounded) [`channel`](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.channel.html) |

```rust
let (result, mut collector) = unbounded_channel();
Expand Down Expand Up @@ -162,11 +169,12 @@ Here is the ideal setup:
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

Let's rewrite our example using [Flume](https://docs.rs/flume/latest/flume/). But first, note the differences between `tokio` and `flume`:
Let's rewrite our example using [Flume](https://docs.rs/flume/latest/flume/). But first, know the mapping between `tokio` and `flume`:

| Tokio | Flume |
|-------|-------|
| [`unbounded_channel`](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.unbounded_channel.html) | [`unbounded`](https://docs.rs/flume/latest/flume/fn.unbounded.html) |
| [`unbounded_channel`](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.unbounded_channel.html) | [`unbounded`](https://docs.rs/flume/latest/flume/fn.unbounded.html) (channel) |
| (bounded) [`channel`](https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.channel.html) | [bounded](https://docs.rs/flume/latest/flume/fn.bounded.html) (channel) |
| `send` | `send` |
| `recv` | `recv_async` |

Expand Down

0 comments on commit 927e94e

Please sign in to comment.