代码拉取完成,页面将自动刷新
package stream
// Sender encapsulates a gRPC response stream and the current chunk
// that's being built.
//
// Reset, Append, [Append...], Send, Reset, Append, [Append...], Send, ...
type Sender interface {
// Reset should create a fresh response message.
Reset()
// Append should append the given item to the slice in the current response message
Append(ChunkData)
// Send should send the current response message
Send() error
}
type ChunkData interface {
Size() int
}
// New returns a new Chunker.
func NewChunker(s Sender) *Chunker { return &Chunker{s: s} }
// Chunker lets you spread items you want to send over multiple chunks.
// This type is not thread-safe.
type Chunker struct {
s Sender
size int
}
// maxMessageSize is maximum size per protobuf message
const maxMessageSize = 1 * 1024 * 1024
// Send will append an item to the current chunk and send the chunk if it is full.
func (c *Chunker) Send(it ChunkData) error {
if it == nil {
return nil
}
if c.size == 0 {
c.s.Reset()
}
itSize := it.Size()
if itSize+c.size >= maxMessageSize {
if err := c.sendResponseMsg(); err != nil {
return err
}
c.s.Reset()
}
c.s.Append(it)
c.size += itSize
return nil
}
func (c *Chunker) sendResponseMsg() error {
c.size = 0
return c.s.Send()
}
// Flush sends remaining items in the current chunk, if any.
func (c *Chunker) Flush() error {
if c.size == 0 {
return nil
}
return c.sendResponseMsg()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。