mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-29 05:22:37 +00:00
Framer does not need to allocate a slice for each execution
This commit is contained in:
parent
159797bac4
commit
470a181846
@ -25,6 +25,7 @@ import (
|
|||||||
|
|
||||||
type lengthDelimitedFrameWriter struct {
|
type lengthDelimitedFrameWriter struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
|
h [4]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLengthDelimitedFrameWriter(w io.Writer) io.Writer {
|
func NewLengthDelimitedFrameWriter(w io.Writer) io.Writer {
|
||||||
@ -34,13 +35,12 @@ func NewLengthDelimitedFrameWriter(w io.Writer) io.Writer {
|
|||||||
// Write writes a single frame to the nested writer, prepending it with the length in
|
// Write writes a single frame to the nested writer, prepending it with the length in
|
||||||
// in bytes of data (as a 4 byte, bigendian uint32).
|
// in bytes of data (as a 4 byte, bigendian uint32).
|
||||||
func (w *lengthDelimitedFrameWriter) Write(data []byte) (int, error) {
|
func (w *lengthDelimitedFrameWriter) Write(data []byte) (int, error) {
|
||||||
header := [4]byte{}
|
binary.BigEndian.PutUint32(w.h[:], uint32(len(data)))
|
||||||
binary.BigEndian.PutUint32(header[:], uint32(len(data)))
|
n, err := w.w.Write(w.h[:])
|
||||||
n, err := w.w.Write(header[:])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
if n != len(header) {
|
if n != len(w.h) {
|
||||||
return 0, io.ErrShortWrite
|
return 0, io.ErrShortWrite
|
||||||
}
|
}
|
||||||
return w.w.Write(data)
|
return w.w.Write(data)
|
||||||
|
Loading…
Reference in New Issue
Block a user