Skip to content

Commit

Permalink
Moving to gRPC by default (micro#1069)
Browse files Browse the repository at this point in the history
* Step 1

* Fix the test panics
  • Loading branch information
asim authored Dec 29, 2019
1 parent 9434452 commit c145f35
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 255 deletions.
36 changes: 4 additions & 32 deletions client/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import (
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/client/selector"
"github.com/micro/go-micro/codec"
raw "github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/transport"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -623,45 +621,19 @@ func (g *grpcClient) getGrpcCallOptions() []grpc.CallOption {
}

func newClient(opts ...client.Option) client.Client {
options := client.Options{
Codecs: make(map[string]codec.NewCodec),
CallOptions: client.CallOptions{
Backoff: client.DefaultBackoff,
Retry: client.DefaultRetry,
Retries: client.DefaultRetries,
RequestTimeout: client.DefaultRequestTimeout,
DialTimeout: transport.DefaultDialTimeout,
},
PoolSize: client.DefaultPoolSize,
PoolTTL: client.DefaultPoolTTL,
}
options := client.NewOptions()
// default content type for grpc
options.ContentType = "application/grpc+proto"

for _, o := range opts {
o(&options)
}

if len(options.ContentType) == 0 {
options.ContentType = "application/grpc+proto"
}

if options.Broker == nil {
options.Broker = broker.DefaultBroker
}

if options.Registry == nil {
options.Registry = registry.DefaultRegistry
}

if options.Selector == nil {
options.Selector = selector.NewSelector(
selector.Registry(options.Registry),
)
}

rc := &grpcClient{
once: sync.Once{},
opts: options,
}

rc.pool = newPool(options.PoolSize, options.PoolTTL, rc.poolMaxIdle(), rc.poolMaxStreams())

c := client.Client(rc)
Expand Down
52 changes: 26 additions & 26 deletions client/grpc/grpc_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ import (
type pool struct {
size int
ttl int64

// max streams on a *poolConn
maxStreams int
// max idle conns
maxIdle int
maxIdle int

sync.Mutex
conns map[string]*streamsPool
}

type streamsPool struct {
// head of list
head *poolConn
head *poolConn
// busy conns list
busy *poolConn
busy *poolConn
// the siza of list
count int
count int
// idle conn
idle int
idle int
}

type poolConn struct {
// grpc conn
*grpc.ClientConn
err error
addr string
err error
addr string

// pool and streams pool
pool *pool
Expand All @@ -44,9 +44,9 @@ type poolConn struct {
created int64

// list
pre *poolConn
next *poolConn
in bool
pre *poolConn
next *poolConn
in bool
}

func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
Expand All @@ -57,11 +57,11 @@ func newPool(size int, ttl time.Duration, idle int, ms int) *pool {
idle = 0
}
return &pool{
size: size,
ttl: int64(ttl.Seconds()),
size: size,
ttl: int64(ttl.Seconds()),
maxStreams: ms,
maxIdle: idle,
conns: make(map[string]*streamsPool),
maxIdle: idle,
conns: make(map[string]*streamsPool),
}
}

Expand All @@ -70,7 +70,7 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
p.Lock()
sp, ok := p.conns[addr]
if !ok {
sp = &streamsPool{head:&poolConn{}, busy:&poolConn{}, count:0, idle:0}
sp = &streamsPool{head: &poolConn{}, busy: &poolConn{}, count: 0, idle: 0}
p.conns[addr] = sp
}
// while we have conns check streams and then return one
Expand All @@ -90,11 +90,11 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
}
// a busy conn
if conn.streams >= p.maxStreams {
next := conn.next
removeConn(conn)
addConnAfter(conn, sp.busy)
conn = next
continue
next := conn.next
removeConn(conn)
addConnAfter(conn, sp.busy)
conn = next
continue
}
// a idle conn
if conn.streams == 0 {
Expand All @@ -112,7 +112,7 @@ func (p *pool) getConn(addr string, opts ...grpc.DialOption) (*poolConn, error)
if err != nil {
return nil, err
}
conn = &poolConn{cc,nil,addr,p,sp,1,time.Now().Unix(), nil, nil, false}
conn = &poolConn{cc, nil, addr, p, sp, 1, time.Now().Unix(), nil, nil, false}

// add conn to streams pool
p.Lock()
Expand Down Expand Up @@ -148,7 +148,7 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
// 2. too many idle conn or
// 3. conn is too old
now := time.Now().Unix()
if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl {
if err != nil || sp.idle >= p.maxIdle || now-created > p.ttl {
removeConn(conn)
p.Unlock()
conn.ClientConn.Close()
Expand All @@ -160,11 +160,11 @@ func (p *pool) release(addr string, conn *poolConn, err error) {
return
}

func (conn *poolConn)Close() {
func (conn *poolConn) Close() {
conn.pool.release(conn.addr, conn, conn.err)
}

func removeConn(conn *poolConn) {
func removeConn(conn *poolConn) {
if conn.pre != nil {
conn.pre.next = conn.next
}
Expand All @@ -178,7 +178,7 @@ func removeConn(conn *poolConn) {
return
}

func addConnAfter(conn *poolConn, after *poolConn) {
func addConnAfter(conn *poolConn, after *poolConn) {
conn.next = after.next
conn.pre = after
if after.next != nil {
Expand Down
5 changes: 2 additions & 3 deletions client/grpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ var (
DefaultMaxSendMsgSize = 1024 * 1024 * 4
)

type poolMaxStreams struct {}
type poolMaxIdle struct {}
type poolMaxStreams struct{}
type poolMaxIdle struct{}
type codecsKey struct{}
type tlsAuth struct{}
type maxRecvMsgSizeKey struct{}
Expand Down Expand Up @@ -129,4 +129,3 @@ func CallOptions(opts ...grpc.CallOption) client.CallOption {
o.Context = context.WithValue(o.Context, grpcCallOptions{}, opts)
}
}

42 changes: 12 additions & 30 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,50 +85,30 @@ type RequestOptions struct {
Context context.Context
}

func newOptions(options ...Option) Options {
func NewOptions(options ...Option) Options {
opts := Options{
Codecs: make(map[string]codec.NewCodec),
Context: context.Background(),
ContentType: DefaultContentType,
Codecs: make(map[string]codec.NewCodec),
CallOptions: CallOptions{
Backoff: DefaultBackoff,
Retry: DefaultRetry,
Retries: DefaultRetries,
RequestTimeout: DefaultRequestTimeout,
DialTimeout: transport.DefaultDialTimeout,
},
PoolSize: DefaultPoolSize,
PoolTTL: DefaultPoolTTL,
PoolSize: DefaultPoolSize,
PoolTTL: DefaultPoolTTL,
Broker: broker.DefaultBroker,
Selector: selector.DefaultSelector,
Registry: registry.DefaultRegistry,
Transport: transport.DefaultTransport,
}

for _, o := range options {
o(&opts)
}

if len(opts.ContentType) == 0 {
opts.ContentType = DefaultContentType
}

if opts.Broker == nil {
opts.Broker = broker.DefaultBroker
}

if opts.Registry == nil {
opts.Registry = registry.DefaultRegistry
}

if opts.Selector == nil {
opts.Selector = selector.NewSelector(
selector.Registry(opts.Registry),
)
}

if opts.Transport == nil {
opts.Transport = transport.DefaultTransport
}

if opts.Context == nil {
opts.Context = context.Background()
}

return opts
}

Expand Down Expand Up @@ -171,6 +151,8 @@ func PoolTTL(d time.Duration) Option {
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
// set in the selector
o.Selector.Init(selector.Registry(r))
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCallOptions(t *testing.T) {
var cl Client

if d.set {
opts = newOptions(
opts = NewOptions(
Retries(d.retries),
RequestTimeout(d.rtimeout),
DialTimeout(d.dtimeout),
Expand All @@ -35,7 +35,7 @@ func TestCallOptions(t *testing.T) {
DialTimeout(d.dtimeout),
)
} else {
opts = newOptions()
opts = NewOptions()
cl = NewClient()
}

Expand Down
2 changes: 1 addition & 1 deletion client/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type rpcClient struct {
}

func newRpcClient(opt ...Option) Client {
opts := newOptions(opt...)
opts := NewOptions(opt...)

p := pool.NewPool(
pool.Size(opts.PoolSize),
Expand Down
6 changes: 2 additions & 4 deletions config/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ var (
}

DefaultClients = map[string]func(...client.Option) client.Client{
"rpc": client.NewClient,
"mucp": cmucp.NewClient,
"grpc": cgrpc.NewClient,
}
Expand All @@ -224,7 +223,6 @@ var (
}

DefaultServers = map[string]func(...server.Option) server.Server{
"rpc": server.NewServer,
"mucp": smucp.NewServer,
"grpc": sgrpc.NewServer,
}
Expand All @@ -242,8 +240,8 @@ var (
}

// used for default selection as the fall back
defaultClient = "rpc"
defaultServer = "rpc"
defaultClient = "grpc"
defaultServer = "grpc"
defaultBroker = "http"
defaultRegistry = "mdns"
defaultSelector = "registry"
Expand Down
2 changes: 0 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ func Registry(r registry.Registry) Option {
// Update Client and Server
o.Client.Init(client.Registry(r))
o.Server.Init(server.Registry(r))
// Update Selector
o.Client.Options().Selector.Init(selector.Registry(r))
// Update Broker
o.Broker.Init(broker.Registry(r))
}
Expand Down
2 changes: 1 addition & 1 deletion server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func (g *grpcServer) Register() error {
g.Unlock()

if !registered {
log.Logf("Registering node: %s", node.Id)
log.Logf("Registry [%s] Registering node: %s", config.Registry.String(), node.Id)
}

// create registry options
Expand Down
28 changes: 0 additions & 28 deletions service/grpc/README.md

This file was deleted.

Loading

0 comments on commit c145f35

Please sign in to comment.