mirror of
https://github.com/Gouryella/drip.git
synced 2026-02-23 21:00:44 +00:00
A new FlowControl frame type has been added for backpressure control between the client and server, and SafeFrame has been introduced. Frame resource management has been encapsulated to ensure that frame data is correctly returned to the memory pool after use. The frame processing logic has also been optimized. The way frame.Release() is called improves code security and maintainability. feat(client): Implements queue pressure monitoring and flow control signal sending functions. Add the `monitorQueuePressure` method to periodically check the length of the `dataFrameQueue`, and trigger an alarm when the queue usage exceeds a threshold. Automatically send flow control commands to the server to pause or resume writing, preventing data backlog and connection interruptions caused by slow consumption speed. feat(server): Supports receiving and responding to flow control requests from clients. The `handleFlowControl` and `sendWithBackpressure` methods have been added to enable backpressure control of received data frames on the server side. By blocking the sending process using a condition variable until the client releases the pause state, connection stability is ensured under high load. refactor(client): Reduces redundant resource release operations during frame processing. Use SafeFrame to manage frame lifecycles uniformly, replacing manual frame.Release() with defer sf.Close() in multiple frame handlers. This avoids the risk of memory leaks caused by unreleased abnormal paths. perf(client): Shorten the shutdown timeout to speed up resource reclamation. The forced shutdown wait time in the tunnel runner and connector has been adjusted from 5 seconds and 3 seconds to 2 seconds to improve the program exit response speed.
35 lines
763 B
Go
35 lines
763 B
Go
package protocol
|
|
|
|
import (
|
|
json "github.com/goccy/go-json"
|
|
)
|
|
|
|
type FlowControlAction string
|
|
|
|
const (
|
|
FlowControlPause FlowControlAction = "pause"
|
|
FlowControlResume FlowControlAction = "resume"
|
|
)
|
|
|
|
type FlowControlMessage struct {
|
|
StreamID string `json:"stream_id"`
|
|
Action FlowControlAction `json:"action"`
|
|
}
|
|
|
|
func NewFlowControlFrame(streamID string, action FlowControlAction) *Frame {
|
|
msg := FlowControlMessage{
|
|
StreamID: streamID,
|
|
Action: action,
|
|
}
|
|
payload, _ := json.Marshal(&msg)
|
|
return NewFrame(FrameTypeFlowControl, payload)
|
|
}
|
|
|
|
func DecodeFlowControlMessage(payload []byte) (*FlowControlMessage, error) {
|
|
var msg FlowControlMessage
|
|
if err := json.Unmarshal(payload, &msg); err != nil {
|
|
return nil, err
|
|
}
|
|
return &msg, nil
|
|
}
|