diff --git a/codec/json/json.go b/codec/json/json.go index fffdc6ab..74ffe267 100644 --- a/codec/json/json.go +++ b/codec/json/json.go @@ -4,10 +4,11 @@ package json import ( "encoding/json" "io" + "io/ioutil" - "github.com/golang/protobuf/jsonpb" - "github.com/golang/protobuf/proto" "github.com/unistack-org/micro/v3/codec" + jsonpb "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" ) type Codec struct { @@ -25,7 +26,11 @@ func (c *Codec) ReadBody(b interface{}) error { return nil } if pb, ok := b.(proto.Message); ok { - return jsonpb.UnmarshalNext(c.Decoder, pb) + buf, err := ioutil.ReadAll(c.Conn) + if err != nil { + return err + } + return jsonpb.Unmarshal(buf, pb) } return c.Decoder.Decode(b) } diff --git a/codec/json/marshaler.go b/codec/json/marshaler.go index 2f6bcd54..9ea03070 100644 --- a/codec/json/marshaler.go +++ b/codec/json/marshaler.go @@ -1,15 +1,14 @@ package json import ( - "bytes" "encoding/json" - "github.com/golang/protobuf/jsonpb" - "github.com/golang/protobuf/proto" "github.com/oxtoacart/bpool" + jsonpb "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" ) -var jsonpbMarshaler = &jsonpb.Marshaler{} +var jsonpbMarshaler = &jsonpb.MarshalOptions{} // create buffer pool with 16 instances each preallocated with 256 bytes var bufferPool = bpool.NewSizedBufferPool(16, 256) @@ -18,19 +17,18 @@ type Marshaler struct{} func (j Marshaler) Marshal(v interface{}) ([]byte, error) { if pb, ok := v.(proto.Message); ok { - buf := bufferPool.Get() - defer bufferPool.Put(buf) - if err := jsonpbMarshaler.Marshal(buf, pb); err != nil { + buf, err := jsonpbMarshaler.Marshal(pb) + if err != nil { return nil, err } - return buf.Bytes(), nil + return buf, nil } return json.Marshal(v) } func (j Marshaler) Unmarshal(d []byte, v interface{}) error { if pb, ok := v.(proto.Message); ok { - return jsonpb.Unmarshal(bytes.NewReader(d), pb) + return jsonpb.Unmarshal(d, pb) } return json.Unmarshal(d, v) } diff --git a/codec/proto/proto.go b/codec/proto/proto.go index 86ec0615..cbfc2496 100644 --- a/codec/proto/proto.go +++ b/codec/proto/proto.go @@ -5,8 +5,8 @@ import ( "io" "io/ioutil" - "github.com/golang/protobuf/proto" "github.com/unistack-org/micro/v3/codec" + "google.golang.org/protobuf/proto" ) type Codec struct { diff --git a/config/default.go b/config/default.go index 6971c76e..1ed08e47 100644 --- a/config/default.go +++ b/config/default.go @@ -2,6 +2,7 @@ package config import ( "bytes" + "fmt" "sync" "time" @@ -176,7 +177,7 @@ func (c *config) Close() error { return nil } -func (c *config) Get(path ...string) reader.Value { +func (c *config) Get(path ...string) (reader.Value, error) { c.RLock() defer c.RUnlock() @@ -186,21 +187,20 @@ func (c *config) Get(path ...string) reader.Value { } // no value - return newValue() + return nil, fmt.Errorf("no value") } -func (c *config) Set(val interface{}, path ...string) { +func (c *config) Set(val interface{}, path ...string) error { c.Lock() defer c.Unlock() if c.vals != nil { c.vals.Set(val, path...) } - - return + return nil } -func (c *config) Del(path ...string) { +func (c *config) Del(path ...string) error { c.Lock() defer c.Unlock() @@ -208,7 +208,7 @@ func (c *config) Del(path ...string) { c.vals.Del(path...) } - return + return nil } func (c *config) Bytes() []byte { @@ -246,7 +246,10 @@ func (c *config) Load(sources ...source.Source) error { } func (c *config) Watch(path ...string) (Watcher, error) { - value := c.Get(path...) + value, err := c.Get(path...) + if err != nil { + return nil, err + } w, err := c.opts.Loader.Watch(path...) if err != nil { @@ -282,8 +285,7 @@ func (w *watcher) Next() (reader.Value, error) { return nil, err } - w.value = v.Get() - return w.value, nil + return v.Get() } } diff --git a/config/reader/reader.go b/config/reader/reader.go index c2f25213..a44732cb 100644 --- a/config/reader/reader.go +++ b/config/reader/reader.go @@ -17,9 +17,9 @@ type Reader interface { // Values is returned by the reader type Values interface { Bytes() []byte - Get(path ...string) Value - Set(val interface{}, path ...string) - Del(path ...string) + Get(path ...string) (Value, error) + Set(val interface{}, path ...string) error + Del(path ...string) error Map() map[string]interface{} Scan(v interface{}) error } diff --git a/go.sum b/go.sum index 1f589874..4e562439 100644 --- a/go.sum +++ b/go.sum @@ -290,6 +290,7 @@ github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf/go.mod h1:M8agBzg github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7/go.mod h1:imsgLplxEC/etjIhdr3dNzV3JeT27LbVu5pYWm0JCBY= github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4o2HM0m3DZYQWsj6/MEowD57VzoH0v3d7igeFY= github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g= +github.com/unistack-org/micro v1.18.0 h1:EbFiII0bKV0Xcua7o6J30MFmm4/g0Hv3ECOKzsUBihU= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/xanzy/go-gitlab v0.35.1 h1:jJSgT0NxjCvrSZf7Gvn2NxxV9xAYkTjYrKW8XwWhrfY= diff --git a/runtime/local/git/git.go b/runtime/local/git/git.go index c7c55874..9c07a56e 100644 --- a/runtime/local/git/git.go +++ b/runtime/local/git/git.go @@ -16,6 +16,7 @@ import ( "strings" "github.com/teris-io/shortid" + "github.com/unistack-org/micro/v3/logger" "github.com/xanzy/go-gitlab" ) @@ -549,10 +550,14 @@ func unzip(src, dest string, skipTopFolder bool) error { return err } defer func() { - r.Close() + if err = r.Close(); err != nil { + logger.Errorf("failed to close reader: %v", err) + } }() - os.MkdirAll(dest, 0755) + if err = os.MkdirAll(dest, 0755); err != nil { + return err + } // Closure to address file descriptors issue with all the deferred .Close() methods extractAndWriteFile := func(f *zip.File) error { @@ -568,15 +573,21 @@ func unzip(src, dest string, skipTopFolder bool) error { } path := filepath.Join(dest, f.Name) if f.FileInfo().IsDir() { - os.MkdirAll(path, f.Mode()) + if err = os.MkdirAll(path, f.Mode()); err != nil { + return err + } } else { - os.MkdirAll(filepath.Dir(path), f.Mode()) + if err = os.MkdirAll(filepath.Dir(path), f.Mode()); err != nil { + return err + } f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { return err } defer func() { - f.Close() + if err = f.Close(); err != nil { + logger.Errorf("failed to close file: %v", err) + } }() _, err = io.Copy(f, rc) diff --git a/selector/roundrobin/roundrobin.go b/selector/roundrobin/roundrobin.go index e64927b3..bb83b642 100644 --- a/selector/roundrobin/roundrobin.go +++ b/selector/roundrobin/roundrobin.go @@ -4,13 +4,14 @@ import ( "github.com/unistack-org/micro/v3/selector" ) -// NewSelector returns an initalised round robin selector +// NewSelector returns an initialised round robin selector func NewSelector(opts ...selector.Option) selector.Selector { return new(roundrobin) } type roundrobin struct{} +// Select return routes based on algo func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) { if len(routes) == 0 { return nil, selector.ErrNoneAvailable diff --git a/tunnel/broker/broker.go b/tunnel/broker/broker.go index bda676bf..a52dffdb 100644 --- a/tunnel/broker/broker.go +++ b/tunnel/broker/broker.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/unistack-org/micro/v3/broker" + "github.com/unistack-org/micro/v3/logger" "github.com/unistack-org/micro/v3/transport" "github.com/unistack-org/micro/v3/tunnel" ) @@ -111,7 +112,10 @@ func (t *tunSubscriber) run() { // receive message m := new(transport.Message) if err := c.Recv(m); err != nil { - c.Close() + logger.Error(err) + if err = c.Close(); err != nil { + logger.Error(err) + } continue } diff --git a/util/mdns/client.go b/util/mdns/client.go index 08772ef6..78c2cb70 100644 --- a/util/mdns/client.go +++ b/util/mdns/client.go @@ -122,12 +122,13 @@ func Listen(entries chan<- *ServiceEntry, exit chan struct{}) error { ip := make(map[string]*ServiceEntry) +loop: for { select { case <-exit: - return nil + break loop case <-client.closedCh: - return nil + break loop case m := <-msgCh: e := messageToEntry(m, ip) if e == nil { diff --git a/util/mdns/server.go b/util/mdns/server.go index 332058a4..44ae8d6b 100644 --- a/util/mdns/server.go +++ b/util/mdns/server.go @@ -163,7 +163,9 @@ func (s *Server) Shutdown() error { s.shutdown = true close(s.shutdownCh) - s.unregister() + if err := s.unregister(); err != nil { + return err + } if s.ipv4List != nil { s.ipv4List.Close() diff --git a/util/pki/pki.go b/util/pki/pki.go index fd5f5d4d..1d2f3a85 100644 --- a/util/pki/pki.go +++ b/util/pki/pki.go @@ -147,6 +147,7 @@ func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) { return out.Bytes(), nil } +//nolint:gocritic func decodePEM(PEM []byte) ([]*pem.Block, error) { var blocks []*pem.Block var asn1 *pem.Block diff --git a/util/tls/tls.go b/util/tls/tls.go index 6df2c6e4..a2745c07 100644 --- a/util/tls/tls.go +++ b/util/tls/tls.go @@ -60,7 +60,9 @@ func Certificate(host ...string) (tls.Certificate, error) { // create public key certOut := bytes.NewBuffer(nil) - pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) + if err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { + return tls.Certificate{}, err + } // create private key keyOut := bytes.NewBuffer(nil) @@ -68,7 +70,9 @@ func Certificate(host ...string) (tls.Certificate, error) { if err != nil { return tls.Certificate{}, err } - pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}) + if err = pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}); err != nil { + return tls.Certificate{}, err + } return tls.X509KeyPair(certOut.Bytes(), keyOut.Bytes()) }