Rust Async Programming

오늘은 Rust에서 제공하는 Asynchronous Programming 관련 feature들에 대해 정리하면서, async 관련 포스트를 쓸 때 마다 사용되는 단어들, async, future, runtime, executor에 대해 정리해 보겠습니다. 오늘은 유독 내용이 추상적인 느낌에 부족한 부분이 많은 것 같은데, 틀린 부분이나 부족한 부분이 있다면 코멘트 남겨 주세요 :) Asynchronous Programming Async book에는 Asynchronous programming이 다음과 같이 정의되어 있습니다. Asynchronous programming, or async for short, is a concurrent programming model supported by an increasing number of programming languages. It lets you run a large number of concurrent tasks on a small number of OS threads, while preserving much of the look and feel of ordinary synchronous programming, through the async/await syntax. ...

2022-09-11 · 7 min · 1415 words · Huijeong Kim

Rust로 Parameterised Test 작성하기

Unit test를 작성 하다 보면 간단한 함수에 대해서도 꽤나 많은 수의 테스트 케이스를 작성하게 됩니다. Input 값의 조합, 특히나 Invalid Input 값의 조합은 무지 많아질 수 있기 때문 입니다. 그 모든 케이스를 하나 씩 테스트로 작성 하는 것은 꽤 귀찮기도 하고, 테스트 함수 작명 지옥에 빠지면서 테스트 가독성도 떨어지게 됩니다. 이럴 때 사용할 수 있는 것이 table-driven test, 혹은 parameterised test 입니다. Input-Output 조합을 table로 표현하는 방식입니다. Rust로 간단한 함수의 parameterised test를 작성해 보고, rust의 test-case, rtest crate를 활용해 이를 더 간편하게 작성하는 방법을 알아보겠습니다. ...

2022-08-28 · 6 min · 1114 words · Huijeong Kim

Async Rust에서 mpsc queue 사용하기

mpsc(multi produce single consumer) queue는 thread 간 message를 주고받는 channel로 많이 쓰입니다. std mpsc와 crossbeam channel가 많이 쓰이는 mpsc channel이고, async rust에서는 tokio mpsc를 사용할 수 있습니다. async rust에서 mpsc queue를 사용하는 방법을 알아보겠습니다. 1. std::sync::mpsc 사용하기 가장 먼저 std 라이브러리의 mpsc를 사용해 볼 수 있겠습니다. Async rust에서 mpsc를 사용하려면 여러 future들이 message sender(tx)를 갖고 있고 하나의 future가 message receiver(rx)를 갖고 있어야 합니다. mpsc의 Sender는 clone 가능하므로 다음과 같이 tx를 clone하여 여러 Future가 message channel을 공유할 수 있습니다. ...

2022-08-27 · 8 min · 1596 words · Huijeong Kim

Actix actor의 AsyncContext 활용하기 (2)

지난 글에 이어 Actix actor의 AsyncContext 함수를 사용하는 방법을 알아봅니다. add_stream, add_message_stream 1 2 3 4 5 6 7 8 9 10 fn add_stream<S>(&mut self, fut: S) -> SpawnHandle where S: Stream + 'static, A: StreamHandler<S::Item>; fn add_message_stream<S>(&mut self, fut: S) where S: Stream + 'static, S::Item: Message, A: Handler<S::Item>; Context에 stream을 등록하는 함수입니다. 두 함수는 거의 비슷하나 차이점은 add_message_stream은 stream error를 무시한다는 점입니다. ...

2022-08-27 · 3 min · 468 words · Huijeong Kim

Actix actor의 AsyncContext 활용하기 (1)

Actix에서 actor를 생성하면 해당 actor를 실행시키는 문맥(context)에 해당하는 Context가 생성 되고, Context는 다양한 actor의 동작을 정의합니다. 그 중 Context를 활용해서 actor가 future를 실행, 취소, 기다리는 방법을 알아봅니다. 아래 예제 코드들은 actix 0.13.0 버전을 사용했습니다. Actor Context 먼저, Actor의 Context의 정의, 생성 시기를 알아보겠습니다. 사용자가 정의한 struct를 actor로 만들기 위해서 최소로 필요한 것은 Actor trait을 구현(implement) 하고 Context를 정의하는 것 입니다. 1 2 3 4 5 struct MyActor; impl Actor for MyActor { type Context = Context<Self>; } 위 actor는 다음과 같이 생성되고 시작될 수 있습니다. ...

2022-08-21 · 8 min · 1541 words · Huijeong Kim

Actix actor를 사용하는 두 가지 방법

Actix에서 actor를 사용하는 방법은 두 가지로 나눌 수 있을 것 같습니다. 첫 번째는 특정 task를 실행시키는 용도로, client가 message를 보내면 이를 받아서 처리하는 actor를 만드는 것 이고, 두 번째는 주기적으로 task를 실행시키는 actor를 만드는 것 입니다. 두 경우 각각 actor를 사용하는 방법에 대해 알아봅니다. 1. Message를 처리하는 actor Actor를 시작하면 Addr가 리턴됩니다. Addr는 해당 actor에게 message를 전달할 수 있는 channel 입니다. Client module은 이 addr를 통해서 message를 전달할 수 있습니다. ...

2022-08-17 · 8 min · 1494 words · Huijeong Kim

Expand Rust Macros

Rust에서 사용 된 macro의 구현이 궁금할 때 cargo expand를 사용할 수 있습니다. 설치 $ cargo install cargo-expand 사용법 이전 글에서 사용한 코드를 expand해 봅시다. cargo expand 시 expand 된 코드가 stdout으로 출력됩니다. $ cargo expand --bin actix-and-tonic 이 예제는 하나의 파일로 이루어져 있어 binary를 지정했지만, 특정 module 코드를 expand하기 위해서 module을 지정할 수도 있습니다. $ cargo expand path::to:module Message 구현에 사용했던 Message macro는 1 2 3 #[derive(Default, Message)] #[rtype(result = "String")] struct GetName; 이렇게 풀어쓸 수 있습니다. ...

2022-08-14 · 1 min · 132 words · Huijeong Kim

Actix에서 gRPC Server 시작하기

Rust로 gRPC로 외부와 통신하는 Actix 기반 application을 작성하는 방법을 알아봅니다. gRPC Server는 Tonic을 사용해 구현합니다. Tonic과 Actix는 둘 다 tokio 기반 application으로, tokio Runtime 위에서 동작합니다. 같은 tokio Runtime에 gRPC Server와 Actor System을 실행시켜 보겠습니다. Tonic을 사용해 gRPC Server 시작하기 Tonic을 사용하면 다음과 같은 간단한 Server 실행 코드를 포함한 Async Block(Future)을 Tokio Runtime에서 실행하여 gRPC Server를 시작할 수 있습니다. 아래 코드는 Tonic repo 내 helloworld exmaple에서 tonic::main 매크로를 풀어 쓴 것입니다. ...

2022-08-14 · 4 min · 699 words · Huijeong Kim