การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม
เรียนรู้วิธีที่สตรีม gRPC จัดการการควบคุมการไหลและแรงกดดันย้อนกลับ เพื่อไม่ให้ผู้ผลิตที่รวดเร็วส่งข้อมูลจนผู้บริโภคที่ช้ารับไม่ไหวบนการเชื่อมต่อสองทิศทางที่ทำงานต่อเนื่องเป็นเวลานาน
การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม เป็นบทเรียน gRPC & High Performance APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน gRPC & High Performance APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Flow Control Matters
In a stream, a producer can generate messages far faster than a consumer reads them. Without limits, buffers grow unbounded and memory explodes.
Flow control is the mechanism that keeps producer and consumer in balance.
HTTP/2 Flow Control Windows
gRPC rides on HTTP/2, which has built-in flow control. Each stream and the whole connection has a window — a credit of bytes the sender may transmit.
As the receiver consumes data, it sends WINDOW_UPDATE frames to replenish credit.
What is Backpressure?
Backpressure is the feedback signal that tells a producer to slow down. When the receiver's window is full, the sender simply cannot write more bytes until credit returns.
This naturally throttles a fast sender to the consumer's pace.
Blocking vs Non-Blocking Sends
Different language stubs expose backpressure differently:
- Blocking stubs: a write blocks until the window allows it
- Async stubs: a callback or
isReadyflag tells you when to resume
The isReady Signal (Java)
In Java's async API, CallStreamObserver.isReady() reports whether the transport can accept more messages without buffering.
if (responseObserver.isReady()) {
responseObserver.onNext(buildChunk());
} else {
// pause until onReadyHandler fires
}Reacting to onReady
Register an onReadyHandler so the runtime calls you back when the window reopens, letting you resume sending without busy-waiting.
observer.setOnReadyHandler(() -> {
while (observer.isReady() && hasMore()) {
observer.onNext(next());
}
});Go Streaming and Backpressure
In Go, stream.Send blocks when the HTTP/2 window is exhausted, giving you implicit backpressure for free. Just loop and send; the call returns when there is room.
for _, item := range items {
if err := stream.Send(item); err != nil {
return err
}
}Tuning Window Sizes
You can tune flow-control behavior at startup:
InitialWindowSizeper streamInitialConnWindowSizeper connection
Larger windows raise throughput on high-latency links but use more memory.
grpc.WithInitialWindowSize(1 << 20)Avoiding Unbounded Buffers
A common bug is reading from a database or file faster than the stream drains, buffering everything in memory. Always gate production on the readiness signal so the source is paused too.
Chunking Large Payloads
For big transfers, split data into bounded chunks (e.g. 64 KB) and stream them. Each chunk respects flow control, keeping memory flat regardless of total size.
for offset := 0; offset < len(data); offset += 65536 {
end := min(offset+65536, len(data))
stream.Send(&Chunk{Data: data[offset:end]})
}Operational Tips
Healthy streaming requires monitoring:
- Watch memory growth on senders
- Track stalled streams (windows stuck at zero)
- Combine deadlines with flow control to cap stuck calls
Quick Check
Test your flow control understanding.
Recap
You learned streaming flow control and backpressure:
- HTTP/2 windows credit how many bytes can flow
- Backpressure signals a producer to slow down
- Blocking stubs block; async stubs expose
isReadyandonReadyHandler - Tune window sizes for throughput vs memory
- Chunk large payloads and gate sources to keep memory flat
คำถามที่พบบ่อย
บทเรียน “การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส gRPC & High Performance APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส gRPC & High Performance APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม”
เรียนรู้วิธีที่สตรีม gRPC จัดการการควบคุมการไหลและแรงกดดันย้อนกลับ เพื่อไม่ให้ผู้ผลิตที่รวดเร็วส่งข้อมูลจนผู้บริโภคที่ช้ารับไม่ไหวบนการเชื่อมต่อสองทิศทางที่ทำงานต่อเนื่องเป็นเวลานาน คุณปฏิบัติ gRPC & High Performance APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน gRPC & High Performance APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน gRPC & High Performance APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน gRPC & High Performance APIs นี้ได้ไหม
ได้ บทเรียน gRPC & High Performance APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- อธิบายการสตรีมจากเซิร์ฟเวอร์
- อธิบายการสตรีมจากไคลเอ็นต์
- การสตรีมสองทิศทาง
- การควบคุมการไหลและแรงกดดันย้อนกลับของสตรีม