Documentation ¶
Index ¶
- Constants
- func EncodeResult(result ResponseCode, w io.Writer) error
- func StreamChunk(result ResponseCode, size uint64, contextBytes []byte, r io.WriterTo, ...) error
- func StreamHeaderAndPayload(size uint64, r io.WriterTo, w io.Writer, comp Compression) error
- type BufLimitReader
- type ChunkedRequestHandler
- type ChunkedResponseHandler
- type Compression
- type Method
- type MinMaxSize
- type NewStreamFn
- type OnRequestListener
- type OnResponseListener
- type ReadContextFn
- type ReadRequestFn
- type Request
- type RequestPayloadHandler
- type RequestReader
- type RequestResponder
- type ResponseChunkHandler
- type ResponseCode
- type ResponseHandler
- type SnappyCompression
- type StreamCtxFn
- type WriteMsgFn
- type WriteSuccessChunkFn
Constants ¶
const MAX_ERR_SIZE = 256
256 bytes max error size
Variables ¶
This section is empty.
Functions ¶
func EncodeResult ¶
func EncodeResult(result ResponseCode, w io.Writer) error
EncodeResult writes the result code to the output writer.
func StreamChunk ¶
func StreamChunk(result ResponseCode, size uint64, contextBytes []byte, r io.WriterTo, w io.Writer, comp Compression) error
StreamChunk takes the (decompressed) response message from the msg io.WriterTo, and writes it as a chunk with given result code to the output writer. The compression is optional and may be nil.
func StreamHeaderAndPayload ¶
StreamHeaderAndPayload reads a payload and streams (and optionally compresses) it to the writer. To do so, it requires the (uncompressed) payload length to be known in advance.
Types ¶
type BufLimitReader ¶
type BufLimitReader struct { N int // max bytes remaining PerRead bool // Limit applies per read, i.e. it is not affected at the end of the read. // contains filtered or unexported fields }
Reader implements buffering for an io.Reader object.
func NewBufLimitReader ¶
func NewBufLimitReader(rd io.Reader, size int, limit int) *BufLimitReader
NewBufLimitReader returns a new Reader whose buffer has the specified size. The reader will return an error if Read crosses the limit.
func (*BufLimitReader) Read ¶
func (b *BufLimitReader) Read(p []byte) (n int, err error)
Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying Reader, hence N may be less than len(p). At EOF, the count will be zero and err will be io.EOF.
func (*BufLimitReader) ReadByte ¶
func (b *BufLimitReader) ReadByte() (byte, error)
type ChunkedRequestHandler ¶
type ChunkedRequestHandler interface { RequestReader RequestResponder }
type ChunkedResponseHandler ¶
type Compression ¶
type Compression interface { // Wraps a reader to decompress data as reads happen. Decompress(r io.Reader) io.Reader // Wraps a writer to compress data as writes happen. Compress(w io.WriteCloser) io.WriteCloser // Returns an error when the input size is too large to encode. MaxEncodedLen(msgLen uint64) (uint64, error) // The name of the compression that is suffixed to the actual encoding. E.g. "snappy", w.r.t. "ssz_snappy". Name() string }
type Method ¶
type Method struct { // Protocol ID, e.g. /eth2/beacon_chain/req/beacon_blocks_by_range/1/ssz_snappy Protocol protocol.ID RequestMinMax MinMaxSize ReadContextBytes ReadContextFn // Compression to apply to requests and response chunks. Nil if no compression. Compression Compression }
func (*Method) MakeStreamHandler ¶
func (m *Method) MakeStreamHandler(newCtx StreamCtxFn, listener OnRequestListener) network.StreamHandler
func (*Method) RunRequest ¶
func (m *Method) RunRequest(ctx context.Context, newStreamFn NewStreamFn, peerId peer.ID, req codec.Serializable, maxRespChunks uint64, onResponse OnResponseListener) error
type MinMaxSize ¶
func (MinMaxSize) Check ¶
func (mms MinMaxSize) Check(size uint64) error
type NewStreamFn ¶
type NewStreamFn func(ctx context.Context, peerId peer.ID, protocolId ...protocol.ID) (network.Stream, error)
func (NewStreamFn) Request ¶
func (newStreamFn NewStreamFn) Request(ctx context.Context, peerId peer.ID, protocolId protocol.ID, size uint64, r io.WriterTo, comp Compression, handle ResponseHandler) error
type OnRequestListener ¶
type OnRequestListener func(ctx context.Context, peerId peer.ID, handler ChunkedRequestHandler)
type OnResponseListener ¶
type OnResponseListener func(chunk ChunkedResponseHandler) error
type ReadContextFn ¶
type ReadContextFn func(blr *BufLimitReader) (contextBytes []byte, minMax MinMaxSize, err error)
ReadContextFn reads <context-bytes> from the response stream, and determines the max chunk size for decoding. The blr.N and blr.PerRead fields should be customized to read the input.
type ReadRequestFn ¶
type ReadRequestFn func(dest interface{}) error
type RequestPayloadHandler ¶
type RequestPayloadHandler func(ctx context.Context, peerId peer.ID, requestLen uint64, r io.ReadCloser, w io.Writer, comp Compression, invalidInputErr error)
RequestPayloadHandler processes a request (decompressed if previously compressed), read from r. The handler can respond by writing to w. After returning the writer will automatically be closed. If the input is already known to be invalid, e.g. the request size is invalid, then `invalidInputErr != nil`, and r will not read anything more.
func (RequestPayloadHandler) MakeStreamHandler ¶
func (handle RequestPayloadHandler) MakeStreamHandler(newCtx StreamCtxFn, comp Compression, minRequestContentSize, maxRequestContentSize uint64) network.StreamHandler
startReqRPC registers a request handler for the given protocol. Compression is optional and may be nil.
type RequestReader ¶
type RequestResponder ¶
type RequestResponder interface { StreamSSZ(code ResponseCode, contextBytes []byte, data codec.Serializable) error WriteRawResponseChunk(code ResponseCode, contextBytes []byte, chunk []byte) error StreamResponseChunk(code ResponseCode, contextBytes []byte, size uint64, r io.WriterTo) error WriteErrorChunk(code ResponseCode, msg string) error }
type ResponseChunkHandler ¶
type ResponseChunkHandler func(ctx context.Context, chunkIndex uint64, chunkSize uint64, result ResponseCode, contextBytes []byte, r io.Reader) error
ResponseChunkHandler is a function that processes a response chunk. The index, size and result-code are already parsed. The response context-bytes are nil if the result is not SuccessCode. The contents (decompressed if previously compressed) can be read from r. Optionally an answer can be written back to w. If the response chunk could not be processed, an error may be returned.
func (ResponseChunkHandler) MakeResponseHandler ¶
func (handleChunk ResponseChunkHandler) MakeResponseHandler( maxChunkCount uint64, readContext ReadContextFn, comp Compression) ResponseHandler
MakeResponseHandler builds a ResponseHandler, which won't take more than maxChunkCount chunks, or chunk contents larger than maxChunkContentSize. Compression is optional and may be nil. Chunks are processed by the given ResponseChunkHandler.
type ResponseCode ¶
type ResponseCode uint8
const ( SuccessCode ResponseCode = 0 InvalidReqCode ResponseCode = 1 ServerErrCode ResponseCode = 2 )
type ResponseHandler ¶
type ResponseHandler func(ctx context.Context, r io.ReadCloser) error
ResponseHandler processes a response by internally processing chunks, any error is propagated up.
type SnappyCompression ¶
type SnappyCompression struct{}
func (SnappyCompression) Compress ¶
func (c SnappyCompression) Compress(w io.WriteCloser) io.WriteCloser
func (SnappyCompression) Decompress ¶
func (c SnappyCompression) Decompress(reader io.Reader) io.Reader
func (SnappyCompression) MaxEncodedLen ¶
func (c SnappyCompression) MaxEncodedLen(msgLen uint64) (uint64, error)
func (SnappyCompression) Name ¶
func (c SnappyCompression) Name() string
type StreamCtxFn ¶
type WriteMsgFn ¶
type WriteSuccessChunkFn ¶
type WriteSuccessChunkFn func(data interface{}) error