Commit 783893f
committed
*: add opt-in stream multiplexing support
DRPC currently allows only one active stream per transport at a time.
This commit adds multiplexing as an opt-in mode (Options.Mux) that
enables concurrent streams over a single transport, while preserving
the original sequential behavior as the default.
Mux and non-mux paths have fundamentally different concurrency models,
so a single Manager with conditionals would add branching in hot paths.
Instead, two separate types are used: Manager (non-mux, unchanged) and
MuxManager (new, in manager_mux.go). MuxManager runs two goroutines:
manageReader routes packets to streams via a streamRegistry, and
manageWriter batches frames from a sharedWriteBuf into transport writes.
The Stream now accepts a StreamWriter interface instead of *Writer
directly, so both paths share the same Stream code. Non-mux uses *Writer
(direct transport writes), mux uses muxWriter (serializes each packet
atomically into the shared buffer to prevent frame interleaving).
Packet buffering is abstracted behind a packetStore interface with two
implementations: syncPacketBuffer (blocking single-slot, non-mux) and
queuePacketBuffer (non-blocking queue with sync.Pool recycling, mux).
RawRecv and MsgRecv branch on the mux flag for buffer lifecycle: non-mux
copies data then calls Done() to unblock the reader, mux takes ownership
and recycles after consumption.
HandlePacket now processes KindMessage before checking the term signal.
This is needed for mux mode where Put must always run to return pool
buffers. In non-mux mode, Put on a closed syncPacketBuffer returns
immediately, so there is no behavioral change. Though there is a brief
blocking window if terminate() has set the term signal but hasn't called
pbuf.Close() yet.
The reader's monotonicity check is relaxed from global to per-stream so
interleaved frames from different streams are accepted. Non-mux never
produces interleaved stream IDs, so this has no behavioral impact there.
Conn uses a streamManager interface satisfied by both Manager types,
branching once in the constructor. In mux mode, Invoke allocates a
per-call marshal buffer instead of reusing a shared one to support
concurrent calls. On the server side, ServeOne branches once to either
serveOneNonMux (sequential handleRPC) or serveOneMux (concurrent
handleRPC with WaitGroup).1 parent 774206f commit 783893f
File tree
20 files changed
+1890
-72
lines changed- drpcconn
- drpcmanager
- drpcserver
- drpcstream
- drpcwire
- internal
- drpcopts
- integration
20 files changed
+1890
-72
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
32 | 41 | | |
33 | 42 | | |
34 | 43 | | |
35 | | - | |
| 44 | + | |
| 45 | + | |
36 | 46 | | |
37 | 47 | | |
38 | 48 | | |
| |||
56 | 66 | | |
57 | 67 | | |
58 | 68 | | |
59 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
60 | 75 | | |
61 | 76 | | |
62 | 77 | | |
| |||
100 | 115 | | |
101 | 116 | | |
102 | 117 | | |
103 | | - | |
104 | | - | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
105 | 121 | | |
106 | 122 | | |
107 | 123 | | |
| |||
117 | 133 | | |
118 | 134 | | |
119 | 135 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
129 | 155 | | |
130 | 156 | | |
131 | | - | |
| 157 | + | |
132 | 158 | | |
133 | 159 | | |
134 | 160 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
184 | 187 | | |
185 | 188 | | |
186 | 189 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
63 | 68 | | |
64 | 69 | | |
65 | 70 | | |
| |||
306 | 311 | | |
307 | 312 | | |
308 | 313 | | |
| 314 | + | |
309 | 315 | | |
310 | 316 | | |
311 | 317 | | |
| |||
0 commit comments