Context上下文
Request handlers often start additional goroutines to access backends such as databases and RPC services.
The set of goroutines working on a request typically needs access to request-specific values such as the identity of the end user, authorization tokens, and the request’s deadline.
When a request is canceled or times out, all the goroutines working on that request should exit quickly so the system can reclaim any resources they are using.
跨goroutine控制,并发、超时控制、路由ID、鉴权校验等
Context的详细介绍可以参考:https://www.51cto.com/article/642806.html和https://go.dev/blog/context
流水线并发模式
Definition
流水线并发模式参考: https://go.dev/blog/pipelines
Fan-out, fan-in
- Multiple functions can read from the same channel until that channel is closed; this is called fan-out. This provides a way to distribute work amongst a group of workers to parallelize CPU use and I/O.
- A function can read from multiple inputs and proceed until all are closed by multiplexing the input channels onto a single channel that’s closed when all the inputs are closed. This is called fan-in.
Guidelines
The guidelines for pipeline construction:
1.stages close their outbound channels when all the send operations are done.
2.stages keep receiving values from inbound channels until those channels are closed or the senders are unblocked.
In real pipelines, stages don’t always receive all the inbound values. Sometimes this is by design: the receiver may only need a subset of values to make progress. More often, a stage exits early because an inbound value represents an error in an earlier stage. In either case the receiver should not have to wait for the remaining values to arrive, and we want earlier stages to stop producing values that later stages don’t need.
A
selectstatement that proceeds either when the send onouthappens or when they receive a value fromdone. The value type of done is the empty struct because the value doesn’t matter: it is the receive event that indicates the send onoutshould be abandoned. Theoutputgoroutines continue looping on their inbound channel,c, so the upstream stages are not blocked.
1.A way to tell an unknown and unbounded number of goroutines to stop sending their values downstream is by closing a channel becase a receive operation on a closed cahnnel can always proceed immediately, yielding the element type’s zero value.
2.This means that can unblock all the senders simply by closing thedonechannel. This close is effectively a broadcast signal to the senders.
3. 多个协程监听done需要close操作立即广播停止,不需要预先知道协程数!
Goroutines consume memory and runtime resources, and heap references in goroutine stacks keep data from being garbage collected. Goroutines are not garbage collected; they must exit on their own.
- A receive operation on a closed channel can always proceed immediately, yielding the element type’s zero value
- 通道用完后,不是一定要close操作,只有在通知接收方goroutine所有数据都发送完毕时才需要显示关闭
- 垃圾回收器根据通道是否可以被访问来确定通道是否被回收,跟是否关闭无关!
Kernel Example Code
1 | package main |
MD5Sum Pipeline
1 | package main |
批跑框架
底层数据流图

核心处理链路
1 | Pipeline.Process() |
源码
C++实现版:https://github.com/TechCodingLearning/pipeline-pattern
