diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2020-06-16 10:40:34 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2020-06-16 10:40:34 +0200 |
commit | 280b1c6c83b676ef4e592f4ecf60cb5b54b6a753 (patch) | |
tree | bf9a35f020de061ba66a432411ee44866405fe76 /codec/msg_codec.go | |
parent | f049390060630c0085fe4ad683c83a4a14a47ffb (diff) |
Optimize socketclient adapter and add various code improvements
This commit includes:
Features
- optimized [socketclient](adapter/socketclient) adapter and add method to set client name
- added list of compatible messages to `CompatibilityError`
Fixes
- `MsgCodec` will recover panic occurring during a message decoding
- calling `Unsubscibe` will close the notification channel
Other
- improved log messages to provide more relevant info
Examples
- added more code samples of working with unions in [union example](examples/union-example)
- added profiling mode to [perf bench](examples/perf-bench) example
- improved [simple client](examples/simple-client) example to work properly even with multiple runs
Dependencies
- updated `github.com/sirupsen/logrus` dep to `v1.6.0`
- updated `github.com/lunixbochs/struc` dep to `v0.0.0-20200521075829-a4cb8d33dbbe`
Change-Id: I136a3968ccf9e93760d7ee2b9902fc7e6390a09d
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'codec/msg_codec.go')
-rw-r--r-- | codec/msg_codec.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/codec/msg_codec.go b/codec/msg_codec.go index 67628a4..3f60cae 100644 --- a/codec/msg_codec.go +++ b/codec/msg_codec.go @@ -20,8 +20,9 @@ import ( "fmt" "reflect" - "git.fd.io/govpp.git/api" "github.com/lunixbochs/struc" + + "git.fd.io/govpp.git/api" ) // MsgCodec provides encoding and decoding functionality of `api.Message` structs into/from @@ -65,7 +66,7 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) (data []byte, err erro if err, ok = r.(error); !ok { err = fmt.Errorf("%v", r) } - err = fmt.Errorf("panic occurred: %v", err) + err = fmt.Errorf("panic occurred during encoding message %s: %v", msg.GetMessageName(), err) } }() @@ -101,11 +102,22 @@ func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) (data []byte, err erro } // DecodeMsg decodes binary-encoded data of a message into provided `Message` structure. -func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error { +func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) (err error) { if msg == nil { return errors.New("nil message passed in") } + // try to recover panic which might possibly occur + defer func() { + if r := recover(); r != nil { + var ok bool + if err, ok = r.(error); !ok { + err = fmt.Errorf("%v", r) + } + err = fmt.Errorf("panic occurred during decoding message %s: %v", msg.GetMessageName(), err) + } + }() + var header interface{} // check which header is expected @@ -123,7 +135,7 @@ func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error { buf := bytes.NewReader(data) // decode message header - if err := struc.Unpack(buf, header); err != nil { + if err = struc.Unpack(buf, header); err != nil { return fmt.Errorf("failed to decode message header: %+v, error: %v", header, err) } |