Alanxtl commented on code in PR #3746: URL: https://github.com/apache/dubbo-go/pull/3746#discussion_r4059065898
########## protocol/triple/triple_protocol/buffered_writer.go: ########## @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package triple_protocol + +import ( + "bytes" + "io" + "sync" +) + +// defaultStreamWriteBufSize is the aggregation threshold for the streaming +// write buffer. It matches the gRPC-Go default write buffer size: large +// enough to batch small, high-rate messages together, small enough to bound +// how much a burst of tiny messages spends buffered (and therefore delays) +// before hitting the wire. +const defaultStreamWriteBufSize = 32 << 10 // 32 KiB + +// streamBufferWriter is an opt-in aggregation layer over a duplexHTTPCall for +// streaming requests. Without it, every streamed message is flushed down the +// io.Pipe alone, paying a synchronous cross-goroutine handshake per message. +// streamBufferWriter amortizes those handshakes by coalescing small messages +// in a capacity-bounded buffer and flushing to the underlying writer only when +// the buffer fills (or when Flush or Close is called). It is safe for +// concurrent use; StreamingClientConn requires Send and CloseRequest to be +// concurrency-safe. +type streamBufferWriter struct { + mu sync.Mutex + + next io.Writer // wrapped duplexHTTPCall + limit int + buf *bytes.Buffer + + err error + closed bool +} + +// newStreamBufferWriter wraps next with an aggregation buffer of default +// capacity. +func newStreamBufferWriter(next io.Writer) *streamBufferWriter { + return &streamBufferWriter{ + next: next, + limit: defaultStreamWriteBufSize, + buf: makeStreamWriteBuf(), + } +} + +// makeStreamWriteBuf preallocates the aggregation capacity. +func makeStreamWriteBuf() *bytes.Buffer { + return bytes.NewBuffer(make([]byte, 0, defaultStreamWriteBufSize)) +} + +// Write coalesces a small message into the buffer, flushing the batch once the +// buffer reaches its limit. Payloads at or over the limit bypass the buffer +// and go directly to the underlying writer so a single big message is not +// held in memory twice. +func (w *streamBufferWriter) Write(p []byte) (int, error) { + w.mu.Lock() + defer w.mu.Unlock() + + if w.err != nil { + return 0, w.err + } + if w.closed { + // Mirror duplexHTTPCall.Write: a late Send after the write side closed + // reports io.EOF. + return 0, io.EOF + } + if len(p) >= w.limit { + if err := w.flushLocked(); err != nil { + return 0, err + } + n, err := w.next.Write(p) + if err == nil && n < len(p) { + // Same short-write guard as flushLocked: freeze the buffer. + err = io.ErrShortWrite + } + if err != nil { + w.err = err + w.closed = true + } + return n, err + } + w.buf.Write(p) + if w.buf.Len() >= w.limit && w.flushLocked() != nil { + return len(p), w.err + } + return len(p), nil +} Review Comment: 小消息只写入内存 buffer,只有达到 32 KiB 或 `CloseRequest` 才 flush;但 `Receive` 没有先 flush,而是直接等待 response。于是合法的: ```go stream.Send(msg) stream.Receive(resp) ``` 在消息不足 32 KiB 时,服务端根本收不到请求,`Receive` 会一直等待。当前文档只描述了“增加首包延迟”,没有说明可能无限等待。 建议在 `Receive`(以及必要时 `ResponseHeader`)前 flush,或者限制该选项只用于非 bidi 场景,并增加 echo bidi regression test。 ########## protocol/triple/triple_protocol/option.go: ########## Review Comment: 这几行有必要再定义一个....Option 吗 直接 config.UnaryFastPath = true 不行吗 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
