fix repocard issues (#21)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-08-25 15:41:48 +03:00 committed by GitHub
parent 8076e410a9
commit 67ab44593b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 68 additions and 38 deletions

View File

@ -4,10 +4,11 @@ package json
import ( import (
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
jsonpb "google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
) )
type Codec struct { type Codec struct {
@ -25,7 +26,11 @@ func (c *Codec) ReadBody(b interface{}) error {
return nil return nil
} }
if pb, ok := b.(proto.Message); ok { 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) return c.Decoder.Decode(b)
} }

View File

@ -1,15 +1,14 @@
package json package json
import ( import (
"bytes"
"encoding/json" "encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/oxtoacart/bpool" "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 // create buffer pool with 16 instances each preallocated with 256 bytes
var bufferPool = bpool.NewSizedBufferPool(16, 256) var bufferPool = bpool.NewSizedBufferPool(16, 256)
@ -18,19 +17,18 @@ type Marshaler struct{}
func (j Marshaler) Marshal(v interface{}) ([]byte, error) { func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
if pb, ok := v.(proto.Message); ok { if pb, ok := v.(proto.Message); ok {
buf := bufferPool.Get() buf, err := jsonpbMarshaler.Marshal(pb)
defer bufferPool.Put(buf) if err != nil {
if err := jsonpbMarshaler.Marshal(buf, pb); err != nil {
return nil, err return nil, err
} }
return buf.Bytes(), nil return buf, nil
} }
return json.Marshal(v) return json.Marshal(v)
} }
func (j Marshaler) Unmarshal(d []byte, v interface{}) error { func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok { if pb, ok := v.(proto.Message); ok {
return jsonpb.Unmarshal(bytes.NewReader(d), pb) return jsonpb.Unmarshal(d, pb)
} }
return json.Unmarshal(d, v) return json.Unmarshal(d, v)
} }

View File

@ -5,8 +5,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"github.com/golang/protobuf/proto"
"github.com/unistack-org/micro/v3/codec" "github.com/unistack-org/micro/v3/codec"
"google.golang.org/protobuf/proto"
) )
type Codec struct { type Codec struct {

View File

@ -2,6 +2,7 @@ package config
import ( import (
"bytes" "bytes"
"fmt"
"sync" "sync"
"time" "time"
@ -176,7 +177,7 @@ func (c *config) Close() error {
return nil return nil
} }
func (c *config) Get(path ...string) reader.Value { func (c *config) Get(path ...string) (reader.Value, error) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
@ -186,21 +187,20 @@ func (c *config) Get(path ...string) reader.Value {
} }
// no 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() c.Lock()
defer c.Unlock() defer c.Unlock()
if c.vals != nil { if c.vals != nil {
c.vals.Set(val, path...) c.vals.Set(val, path...)
} }
return nil
return
} }
func (c *config) Del(path ...string) { func (c *config) Del(path ...string) error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
@ -208,7 +208,7 @@ func (c *config) Del(path ...string) {
c.vals.Del(path...) c.vals.Del(path...)
} }
return return nil
} }
func (c *config) Bytes() []byte { 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) { 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...) w, err := c.opts.Loader.Watch(path...)
if err != nil { if err != nil {
@ -282,8 +285,7 @@ func (w *watcher) Next() (reader.Value, error) {
return nil, err return nil, err
} }
w.value = v.Get() return v.Get()
return w.value, nil
} }
} }

View File

@ -17,9 +17,9 @@ type Reader interface {
// Values is returned by the reader // Values is returned by the reader
type Values interface { type Values interface {
Bytes() []byte Bytes() []byte
Get(path ...string) Value Get(path ...string) (Value, error)
Set(val interface{}, path ...string) Set(val interface{}, path ...string) error
Del(path ...string) Del(path ...string) error
Map() map[string]interface{} Map() map[string]interface{}
Scan(v interface{}) error Scan(v interface{}) error
} }

1
go.sum
View File

@ -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/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/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/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/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA= github.com/vultr/govultr v0.1.4/go.mod h1:9H008Uxr/C4vFNGLqKx232C206GL0PBHzOP0809bGNA=
github.com/xanzy/go-gitlab v0.35.1 h1:jJSgT0NxjCvrSZf7Gvn2NxxV9xAYkTjYrKW8XwWhrfY= github.com/xanzy/go-gitlab v0.35.1 h1:jJSgT0NxjCvrSZf7Gvn2NxxV9xAYkTjYrKW8XwWhrfY=

View File

@ -16,6 +16,7 @@ import (
"strings" "strings"
"github.com/teris-io/shortid" "github.com/teris-io/shortid"
"github.com/unistack-org/micro/v3/logger"
"github.com/xanzy/go-gitlab" "github.com/xanzy/go-gitlab"
) )
@ -549,10 +550,14 @@ func unzip(src, dest string, skipTopFolder bool) error {
return err return err
} }
defer func() { 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 // Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error { extractAndWriteFile := func(f *zip.File) error {
@ -568,15 +573,21 @@ func unzip(src, dest string, skipTopFolder bool) error {
} }
path := filepath.Join(dest, f.Name) path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() { if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode()) if err = os.MkdirAll(path, f.Mode()); err != nil {
return err
}
} else { } 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()) f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil { if err != nil {
return err return err
} }
defer func() { defer func() {
f.Close() if err = f.Close(); err != nil {
logger.Errorf("failed to close file: %v", err)
}
}() }()
_, err = io.Copy(f, rc) _, err = io.Copy(f, rc)

View File

@ -4,13 +4,14 @@ import (
"github.com/unistack-org/micro/v3/selector" "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 { func NewSelector(opts ...selector.Option) selector.Selector {
return new(roundrobin) return new(roundrobin)
} }
type roundrobin struct{} type roundrobin struct{}
// Select return routes based on algo
func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) { func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) {
if len(routes) == 0 { if len(routes) == 0 {
return nil, selector.ErrNoneAvailable return nil, selector.ErrNoneAvailable

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/unistack-org/micro/v3/broker" "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/transport"
"github.com/unistack-org/micro/v3/tunnel" "github.com/unistack-org/micro/v3/tunnel"
) )
@ -111,7 +112,10 @@ func (t *tunSubscriber) run() {
// receive message // receive message
m := new(transport.Message) m := new(transport.Message)
if err := c.Recv(m); err != nil { if err := c.Recv(m); err != nil {
c.Close() logger.Error(err)
if err = c.Close(); err != nil {
logger.Error(err)
}
continue continue
} }

View File

@ -122,12 +122,13 @@ func Listen(entries chan<- *ServiceEntry, exit chan struct{}) error {
ip := make(map[string]*ServiceEntry) ip := make(map[string]*ServiceEntry)
loop:
for { for {
select { select {
case <-exit: case <-exit:
return nil break loop
case <-client.closedCh: case <-client.closedCh:
return nil break loop
case m := <-msgCh: case m := <-msgCh:
e := messageToEntry(m, ip) e := messageToEntry(m, ip)
if e == nil { if e == nil {

View File

@ -163,7 +163,9 @@ func (s *Server) Shutdown() error {
s.shutdown = true s.shutdown = true
close(s.shutdownCh) close(s.shutdownCh)
s.unregister() if err := s.unregister(); err != nil {
return err
}
if s.ipv4List != nil { if s.ipv4List != nil {
s.ipv4List.Close() s.ipv4List.Close()

View File

@ -147,6 +147,7 @@ func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) {
return out.Bytes(), nil return out.Bytes(), nil
} }
//nolint:gocritic
func decodePEM(PEM []byte) ([]*pem.Block, error) { func decodePEM(PEM []byte) ([]*pem.Block, error) {
var blocks []*pem.Block var blocks []*pem.Block
var asn1 *pem.Block var asn1 *pem.Block

View File

@ -60,7 +60,9 @@ func Certificate(host ...string) (tls.Certificate, error) {
// create public key // create public key
certOut := bytes.NewBuffer(nil) 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 // create private key
keyOut := bytes.NewBuffer(nil) keyOut := bytes.NewBuffer(nil)
@ -68,7 +70,9 @@ func Certificate(host ...string) (tls.Certificate, error) {
if err != nil { if err != nil {
return tls.Certificate{}, err 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()) return tls.X509KeyPair(certOut.Bytes(), keyOut.Bytes())
} }