Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม

ผสานกระบวนการที่ไม่ใช่ OTP และระบบย่อยภายนอกเข้ากับต้นไม้การควบคุมด้วย supervisor_bridge พร้อมคงความทนทานต่อข้อผิดพลาดไว้

บทเรียน 4 จาก 413 ขั้นตอน

สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why a Supervisor Bridge?

Not every process you depend on is a clean OTP gen_server. Sometimes you must supervise a plain Erlang process, a port, or a third-party subsystem. The supervisor_bridge behaviour lets such a process appear as a normal child in a supervision tree.

The Problem It Solves

A standard supervisor only knows how to start, stop and restart OTP-compliant children. A raw process started with spawn_link does not respond to OTP shutdown protocols. The bridge translates between the two worlds.

Behaviour Callbacks

A supervisor_bridge module implements two callbacks:

  • init/1 starts the real subsystem and returns its pid
  • terminate/2 cleanly shuts the subsystem down

Skeleton Module

The bridge is started like a supervisor but behaves like a worker to its parent.

-module(legacy_bridge).
-behaviour(supervisor_bridge).
-export([start_link/0, init/1, terminate/2]).

start_link() ->
    supervisor_bridge:start_link({local, ?MODULE}, ?MODULE, []).

Implementing init/1

init/1 must start and link the external process, then return {ok, Pid, State}. If start fails, return {error, Reason}.

init([]) ->
    case legacy_server:start() of
        {ok, Pid} -> {ok, Pid, Pid};
        Error -> Error
    end.

Implementing terminate/2

When the bridge is asked to shut down, it calls terminate/2 so you can stop the subsystem gracefully.

terminate(_Reason, Pid) ->
    legacy_server:stop(Pid),
    ok.

Placing the Bridge in a Tree

To its parent supervisor, the bridge is just another child spec with type => worker.

ChildSpec = #{id => legacy,
              start => {legacy_bridge, start_link, []},
              restart => permanent,
              type => worker}.

Failure Propagation

Because the bridge links to the external process, a crash there exits the bridge, which the parent supervisor sees as a normal worker crash and restarts according to its strategy.

Mixed Hierarchies

A real system mixes gen_server workers, sub-supervisors, and bridges. The bridge lets legacy or foreign code participate without weakening the OTP guarantees of the rest of the tree.

When NOT to Use a Bridge

If you can wrap the subsystem in a proper gen_server, prefer that — it gives you full OTP introspection. Reserve the bridge for code you cannot rewrite or that has its own process lifecycle.

Debugging Bridged Subsystems

Because the inner process is not OTP, tools like sys:get_state/1 will not reach it. Add logging inside the subsystem and rely on the bridge state to track the inner pid.

Quick Check

Test your understanding of supervisor bridges.

Recap

You learned to integrate foreign processes into supervision trees:

  • supervisor_bridge wraps non-OTP subsystems
  • init/1 starts and links the subsystem; terminate/2 stops it
  • It appears as a worker child to its parent supervisor
  • Crashes propagate so normal restart strategies apply
  • Prefer a real gen_server when you control the code
เริ่มต้นได้ฟรี

เรียนรู้ Erlang ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม”

ผสานกระบวนการที่ไม่ใช่ OTP และระบบย่อยภายนอกเข้ากับต้นไม้การควบคุมด้วย supervisor_bridge พร้อมคงความทนทานต่อข้อผิดพลาดไว้ คุณปฏิบัติ Erlang OTP: Distributed & Fault-Tolerant Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming นี้ได้ไหม

ได้ บทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โครงสร้างต้นไม้การควบคุมที่ซับซ้อน
  2. การจัดการโพรเซสแบบไดนามิก
  3. กลยุทธ์การเริ่มต้นใหม่ขั้นสูง
  4. สะพานตัวควบคุมและลำดับชั้นกระบวนการแบบผสม
← กลับไปที่ Erlang OTP: Distributed & Fault-Tolerant Systems Programming