lint fixes (#14)
* lint fixes Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -75,7 +75,6 @@ func (g *grpcClient) secure(addr string) grpc.DialOption {
|
||||
func (g *grpcClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
||||
var header map[string]string
|
||||
|
||||
header = make(map[string]string)
|
||||
if md, ok := metadata.FromContext(ctx); ok {
|
||||
header = make(map[string]string, len(md))
|
||||
for k, v := range md {
|
||||
@@ -103,8 +102,10 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
||||
|
||||
var grr error
|
||||
|
||||
gctx, cancel := context.WithTimeout(ctx, opts.DialTimeout)
|
||||
defer cancel()
|
||||
|
||||
grpcDialOptions := []grpc.DialOption{
|
||||
grpc.WithTimeout(opts.DialTimeout),
|
||||
g.secure(addr),
|
||||
grpc.WithDefaultCallOptions(
|
||||
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
|
||||
@@ -116,7 +117,7 @@ func (g *grpcClient) call(ctx context.Context, addr string, req client.Request,
|
||||
grpcDialOptions = append(grpcDialOptions, opts...)
|
||||
}
|
||||
|
||||
cc, err := g.pool.getConn(addr, grpcDialOptions...)
|
||||
cc, err := g.pool.getConn(gctx, addr, grpcDialOptions...)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", fmt.Sprintf("Error sending request: %v", err))
|
||||
}
|
||||
@@ -187,7 +188,6 @@ func (g *grpcClient) stream(ctx context.Context, addr string, req client.Request
|
||||
wc := wrapCodec{cf}
|
||||
|
||||
grpcDialOptions := []grpc.DialOption{
|
||||
grpc.WithTimeout(opts.DialTimeout),
|
||||
g.secure(addr),
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -66,7 +67,7 @@ func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error) {
|
||||
func (p *pool) getConn(ctx context.Context, addr string, opts ...grpc.DialOption) (*poolConn, error) {
|
||||
now := time.Now().Unix()
|
||||
p.Lock()
|
||||
sp, ok := p.conns[addr]
|
||||
@@ -135,7 +136,7 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
|
||||
p.Unlock()
|
||||
|
||||
// create new conn
|
||||
cc, err := grpc.Dial(addr, opts...)
|
||||
cc, err := grpc.DialContext(ctx, addr, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -184,7 +185,6 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
|
||||
sp.idle++
|
||||
}
|
||||
p.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (conn *poolConn) Close() {
|
||||
@@ -202,7 +202,6 @@ func removeConn(conn *poolConn) {
|
||||
conn.next = nil
|
||||
conn.in = false
|
||||
conn.sp.count--
|
||||
return
|
||||
}
|
||||
|
||||
func addConnAfter(conn *poolConn, after *poolConn) {
|
||||
@@ -214,5 +213,4 @@ func addConnAfter(conn *poolConn, after *poolConn) {
|
||||
after.next = conn
|
||||
conn.in = true
|
||||
conn.sp.count++
|
||||
return
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func testPool(t *testing.T, size int, ttl time.Duration, idle int, ms int) {
|
||||
// setup server
|
||||
l, err := net.Listen("tcp", ":0")
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
@@ -24,13 +24,14 @@ func testPool(t *testing.T, size int, ttl time.Duration, idle int, ms int) {
|
||||
|
||||
go s.Serve(l)
|
||||
defer s.Stop()
|
||||
ctx := context.Background()
|
||||
|
||||
// zero pool
|
||||
p := newPool(size, ttl, idle, ms)
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
// get a conn
|
||||
cc, err := p.getConn(l.Addr().String(), grpc.WithInsecure())
|
||||
cc, err := p.getConn(ctx, l.Addr().String(), grpc.WithInsecure())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ func (g *greeterServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.
|
||||
}
|
||||
|
||||
func TestGRPCClient(t *testing.T) {
|
||||
l, err := net.Listen("tcp", ":0")
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
@@ -40,8 +40,7 @@ func LookupRoute(ctx context.Context, req Request, opts CallOptions) ([]string,
|
||||
return routes[i].Metric < routes[j].Metric
|
||||
})
|
||||
|
||||
var addrs []string
|
||||
|
||||
addrs := make([]string, 0, len(routes))
|
||||
for _, route := range routes {
|
||||
addrs = append(addrs, route.Address)
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/unistack-org/micro/v3/codec/proto"
|
||||
"github.com/unistack-org/micro/v3/codec/protorpc"
|
||||
"github.com/unistack-org/micro/v3/errors"
|
||||
"github.com/unistack-org/micro/v3/registry"
|
||||
"github.com/unistack-org/micro/v3/transport"
|
||||
)
|
||||
|
||||
@@ -62,15 +61,6 @@ var (
|
||||
"application/proto-rpc": protorpc.NewCodec,
|
||||
"application/octet-stream": raw.NewCodec,
|
||||
}
|
||||
|
||||
// TODO: remove legacy codec list
|
||||
defaultCodecs = map[string]codec.NewCodec{
|
||||
"application/json": jsonrpc.NewCodec,
|
||||
"application/json-rpc": jsonrpc.NewCodec,
|
||||
"application/protobuf": protorpc.NewCodec,
|
||||
"application/proto-rpc": protorpc.NewCodec,
|
||||
"application/octet-stream": protorpc.NewCodec,
|
||||
}
|
||||
)
|
||||
|
||||
func (rwc *readWriteCloser) Read(p []byte) (n int, err error) {
|
||||
@@ -127,30 +117,6 @@ func setHeaders(m *codec.Message, stream string) {
|
||||
}
|
||||
}
|
||||
|
||||
// setupProtocol sets up the old protocol
|
||||
func setupProtocol(msg *transport.Message, node *registry.Node) codec.NewCodec {
|
||||
// get the protocol from node metadata
|
||||
if protocol := node.Metadata["protocol"]; len(protocol) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// processing topic publishing
|
||||
if len(msg.Header["Micro-Topic"]) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// no protocol use old codecs
|
||||
switch msg.Header["Content-Type"] {
|
||||
case "application/json":
|
||||
msg.Header["Content-Type"] = "application/json-rpc"
|
||||
case "application/protobuf":
|
||||
msg.Header["Content-Type"] = "application/proto-rpc"
|
||||
}
|
||||
|
||||
// now return codec
|
||||
return defaultCodecs[msg.Header["Content-Type"]]
|
||||
}
|
||||
|
||||
func newRpcCodec(req *transport.Message, client transport.Client, c codec.NewCodec, stream string) codec.Codec {
|
||||
rwc := &readWriteCloser{
|
||||
wbuf: bytes.NewBuffer(nil),
|
||||
|
Reference in New Issue
Block a user