Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-12-08 00:38:37 +03:00
parent f63ff80d46
commit b7b28f6b9a
29 changed files with 138 additions and 85 deletions

View File

@@ -9,6 +9,7 @@ import (
)
const (
// Message types
Error MessageType = iota
Request
Response
@@ -23,6 +24,7 @@ var (
)
var (
// DefaultMaxMsgSize specifies how much data codec can handle
DefaultMaxMsgSize = 1024 * 1024 * 4 // 4Mb
)
@@ -59,18 +61,22 @@ type Message struct {
Body []byte
}
// Option func
type Option func(*Options)
// Options contains codec options
type Options struct {
MaxMsgSize int64
}
// MaxMsgSize sets the max message size
func MaxMsgSize(n int64) Option {
return func(o *Options) {
o.MaxMsgSize = n
}
}
// NewMessage creates new codec message
func NewMessage(t MessageType) *Message {
return &Message{Type: t, Header: metadata.New(0)}
}

View File

@@ -74,11 +74,12 @@ func (c *noopCodec) String() string {
return "noop"
}
// NewCodec returns new noop codec
func NewCodec() Codec {
return &noopCodec{}
}
func (n *noopCodec) Marshal(v interface{}) ([]byte, error) {
func (c *noopCodec) Marshal(v interface{}) ([]byte, error) {
if v == nil {
return nil, nil
}
@@ -100,7 +101,7 @@ func (n *noopCodec) Marshal(v interface{}) ([]byte, error) {
return nil, ErrInvalidMessage
}
func (n *noopCodec) Unmarshal(d []byte, v interface{}) error {
func (c *noopCodec) Unmarshal(d []byte, v interface{}) error {
if v == nil {
return nil
}