Jembatan Supervisor & Hierarki Proses Campuran
Integrasikan proses non-OTP dan subsistem eksternal ke dalam pohon supervisi menggunakan supervisor_bridge, sambil mempertahankan toleransi kesalahan.
Jembatan Supervisor & Hierarki Proses Campuran adalah pelajaran Erlang OTP: Distributed & Fault-Tolerant Systems Programming gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Erlang OTP: Distributed & Fault-Tolerant Systems Programming, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Erlang OTP: Distributed & Fault-Tolerant Systems Programming mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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/1starts the real subsystem and returns its pidterminate/2cleanly 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_bridgewraps non-OTP subsystemsinit/1starts and links the subsystem;terminate/2stops 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
Belajar Erlang dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Jembatan Supervisor & Hierarki Proses Campuran” gratis?
Ya — teks lengkap “Jembatan Supervisor & Hierarki Proses Campuran” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Erlang OTP: Distributed & Fault-Tolerant Systems Programming, upgrade ke CoddyKit PRO. Kursus Erlang OTP: Distributed & Fault-Tolerant Systems Programming mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Jembatan Supervisor & Hierarki Proses Campuran”?
Integrasikan proses non-OTP dan subsistem eksternal ke dalam pohon supervisi menggunakan supervisor_bridge, sambil mempertahankan toleransi kesalahan. Kamu berlatih Erlang OTP: Distributed & Fault-Tolerant Systems Programming dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Tidak diperlukan pengalaman sebelumnya. Erlang OTP: Distributed & Fault-Tolerant Systems Programming di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.
Berapa lama pelajaran “Jembatan Supervisor & Hierarki Proses Campuran” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Erlang OTP: Distributed & Fault-Tolerant Systems Programming ini?
Ya. Setiap pelajaran Erlang OTP: Distributed & Fault-Tolerant Systems Programming menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pohon Supervisi Kompleks
- Pengelolaan Proses Dinamis
- Strategi Memulai Ulang Tingkat Lanjut
- Jembatan Supervisor & Hierarki Proses Campuran