good morning!!!!

Skip to content
Snippets Groups Projects
Verified Commit 8be57947 authored by a's avatar a
Browse files

ok

parent 636a614d
No related branches found
No related tags found
No related merge requests found
Pipeline #33009 passed with stage
in 6 minutes and 19 seconds
package server
import (
"errors"
"fmt"
"io"
)
var _ io.Writer = (*Writer)(nil)
var ErrThresholdExceeded = errors.New("stream size exceeds threshold")
// Writer wraps w with writing length limit.
//
// To create Writer, use NewWriter().
type Writer struct {
w io.Writer
written int
limit int
regardOverSizeNormal bool
}
// NewWriter create a writer that writes at most n bytes.
//
// regardOverSizeNormal controls whether Writer.Write() returns error
// when writing totally more bytes than n, or do no-op to inner w,
// pretending writing is processed normally.
func newWriter(w io.Writer, n int, regardOverSizeNormal bool) *Writer {
return &Writer{
w: w,
written: 0,
limit: n,
regardOverSizeNormal: regardOverSizeNormal,
}
}
// Writer implements io.Writer
func (lw *Writer) Write(p []byte) (n int, err error) {
if lw.written >= lw.limit {
if lw.regardOverSizeNormal {
n = len(p)
lw.written += n
return
}
err = fmt.Errorf("threshold is %d bytes: %w", lw.limit, ErrThresholdExceeded)
return
}
var (
overSized bool
originalLen int
)
left := lw.limit - lw.written
if originalLen = len(p); originalLen > left {
overSized = true
p = p[0:left]
}
n, err = lw.w.Write(p)
lw.written += n
if overSized && err == nil {
// Write must return a non-nil error if it returns n < len(p).
if lw.regardOverSizeNormal {
return originalLen, nil
}
err = fmt.Errorf("threshold is %d bytes: %w", lw.limit, ErrThresholdExceeded)
return
}
return
}
......@@ -6,11 +6,6 @@ import (
"gfx.cafe/open/jrpc/pkg/jsonrpc"
)
// 128mb... should be more than enough for any batch.
// you shouldn't be batching more than this. really, you shouldn't be using batching at all.
// TODO: make this configurable
const maxBatchSizeBytes = 1024 * 1024 * 1024 * 128
var _ jsonrpc.ResponseWriter = (*streamingRespWriter)(nil)
// streamingRespWriter is NOT thread safe
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment