Skip Navigation
Thai Rust Programming Language Community @lemmy.ml

ข้อสังเกตเกี่ยวกับการใช้ actix actor และ supervisor

Actix มี supervisor ที่ช่วย restart actor ใต้บังคับ แต่บางทีมันก็ไม่ restart 😹

เท่าที่ทราบบางอย่างต้องส่ง Die (message) ไปให้ actor เวลามีอะไรพัง actor ต้อง implement ตัว handle Die message handler ใน handler ต้องเรียก ctx.stop (context stop) เมื่อ "Actor::stopping(...) == Running::Stop" poll function จะคืนค่า Poll::Ready จากนั้น supervisor ก็จะไปสั่ง restart ได้

ในกรณีของผมพวกนี้ทำงานไม่ได้เพราะสองอย่างคือ

  1. Actor หยุดแบบไม่ได้เรียก stopped function ดังนั้นทำให้ actor ไม่มีโอกาสได้ handle Die message เลย
  2. Actor ใช้ CPU เพราะเกิดกรณีที่ผิดพลาด ดังนั้น poll function ก็โดน block ไปเลย ทำให้ actor ไม่สามารถ handle Die message.

ผมแก้กรณีแรกโดยเรียก tokio::time::sleep และกรณีที่สองโดยเรียก break ออกจาก loop

ปล. ผมไม่มั่นใจเรื่องที่เขียนเลย ถ้าผมเข้าใจอะไรผิดก็ให้ชี้แนะด้วยครับ

0 comments

No comments