Elixir & Phoenix: Scalable Backend Development · درس

مسارات GenStage والتحكم في التدفق

ابنِ مسارات بيانات مدفوعة بالطلب باستخدام GenStage، حيث يتحكم المستهلكون في التدفق عبر التحكم في الضغط لمنع التحميل الزائد في الأنظمة الموزعة.

الدرس 4 من 413 خطوة

مسارات GenStage والتحكم في التدفق درس مجاني في Elixir & Phoenix: Scalable Backend Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Elixir & Phoenix: Scalable Backend Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Elixir & Phoenix: Scalable Backend Development 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Overload Problem

When a fast producer feeds a slow consumer, messages pile up and memory explodes. Backpressure solves this: consumers signal how much work they can handle, and producers send only that much.

What is GenStage?

GenStage is an Elixir behaviour for building staged, demand-driven pipelines. Data flows from producers to consumers, with optional producer-consumer stages in between.

The Three Stage Types

Every GenStage process plays one role:

  • Producer: emits events on demand
  • ProducerConsumer: receives, transforms, and re-emits
  • Consumer: receives and processes events

Defining a Producer

A producer implements init/1 and handle_demand/2. It returns events only when consumers ask for them.

defmodule Counter do
  use GenStage
  def init(start), do: {:producer, start}
  def handle_demand(demand, state) do
    events = Enum.to_list(state..(state + demand - 1))
    {:noreply, events, state + demand}
  end
end

Defining a Consumer

A consumer implements handle_events/3. It returns no events itself, only acknowledging the demand it processed.

defmodule Printer do
  use GenStage
  def init(_), do: {:consumer, :ok}
  def handle_events(events, _from, state) do
    for e <- events, do: IO.inspect(e)
    {:noreply, [], state}
  end
end

Subscribing Stages

Connect a consumer to a producer with GenStage.sync_subscribe/2. The max_demand option caps how many events flow at once.

{:ok, prod} = GenStage.start_link(Counter, 0)
{:ok, cons} = GenStage.start_link(Printer, :ok)
GenStage.sync_subscribe(cons, to: prod, max_demand: 10)

How Backpressure Works

The consumer requests up to max_demand events. The producer can never send more than was requested, so a slow consumer naturally throttles a fast producer. No buffering blowup.

Producer-Consumer Stages

A middle stage transforms data. It implements handle_events/3 but returns transformed events for the next stage.

def handle_events(events, _from, state) do
  doubled = Enum.map(events, &(&1 * 2))
  {:noreply, doubled, state}
end

ConsumerSupervisor

For concurrent processing, ConsumerSupervisor spawns a short-lived child process per event, bounded by demand. This parallelizes work while keeping backpressure intact.

Flow for Parallel Pipelines

The Flow library builds on GenStage to offer map/reduce-style parallel data processing with partitioning — ideal for crunching large collections across cores.

File.stream!("big.csv")
|> Flow.from_enumerable()
|> Flow.map(&parse_line/1)
|> Flow.partition()
|> Enum.to_list()

When to Reach for GenStage

Use GenStage when you have:

  • A rate mismatch between data source and processing
  • Streaming data that must not overwhelm memory
  • Multi-stage transformation pipelines

Quick Check

Test your GenStage knowledge.

Recap

You learned demand-driven pipelines:

  • GenStage has producers, producer-consumers, and consumers
  • Consumers request bounded demand for backpressure
  • Subscribe stages with sync_subscribe and max_demand
  • ConsumerSupervisor parallelizes per-event work
  • Flow builds parallel map/reduce pipelines on top

Backpressure keeps distributed systems stable under load.

البدء مجانًا

تعلم Elixir مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «مسارات GenStage والتحكم في التدفق» مجاني؟

نعم — نص درس «مسارات GenStage والتحكم في التدفق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Elixir & Phoenix: Scalable Backend Development، انتقل إلى CoddyKit PRO. تتضمن دورة Elixir & Phoenix: Scalable Backend Development 4 دروس في المجموع.

ماذا ستتعلم في «مسارات GenStage والتحكم في التدفق»؟

ابنِ مسارات بيانات مدفوعة بالطلب باستخدام GenStage، حيث يتحكم المستهلكون في التدفق عبر التحكم في الضغط لمنع التحميل الزائد في الأنظمة الموزعة. تتمرن على Elixir & Phoenix: Scalable Backend Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Elixir & Phoenix: Scalable Backend Development؟

لا تُشترط خبرة سابقة. Elixir & Phoenix: Scalable Backend Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «مسارات GenStage والتحكم في التدفق»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Elixir & Phoenix: Scalable Backend Development هذا؟

نعم. كل درس في Elixir & Phoenix: Scalable Backend Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. Elixir الموزّع والعنقدة
  2. استراتيجيات الإشراف المتقدمة
  3. المشرفون الديناميكيون وRegistry
  4. مسارات GenStage والتحكم في التدفق
← العودة إلى Elixir & Phoenix: Scalable Backend Development