From 280b1c6c83b676ef4e592f4ecf60cb5b54b6a753 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Tue, 16 Jun 2020 10:40:34 +0200 Subject: 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 --- codec/msg_codec.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'codec') 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) } -- cgit 1.2.3-korg