v3 refactor (#1868)

* Move to v3

Co-authored-by: Ben Toogood <bentoogood@gmail.com>
This commit is contained in:
Asim Aslam
2020-07-27 13:22:00 +01:00
committed by GitHub
parent 9dfeb98111
commit 563768b58a
424 changed files with 6383 additions and 22490 deletions

View File

@@ -2,22 +2,10 @@ package server
import (
"context"
"sync"
)
type serverKey struct{}
func wait(ctx context.Context) *sync.WaitGroup {
if ctx == nil {
return nil
}
wg, ok := ctx.Value("wait").(*sync.WaitGroup)
if !ok {
return nil
}
return wg
}
func FromContext(ctx context.Context) (Server, bool) {
c, ok := ctx.Value(serverKey{}).(Server)
return c, ok

View File

@@ -8,8 +8,8 @@ import (
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/codec/bytes"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
"google.golang.org/grpc/metadata"

View File

@@ -3,7 +3,7 @@ package grpc
import (
"context"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/server"
)
func setServerOption(k, v interface{}) server.Option {

View File

@@ -3,7 +3,7 @@ package grpc
import (
"net/http"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v3/errors"
"google.golang.org/grpc/codes"
)

View File

@@ -5,7 +5,7 @@ import (
"reflect"
"strings"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
)
func extractValue(v reflect.Type, d int) *registry.Value {

View File

@@ -5,7 +5,7 @@ import (
"reflect"
"testing"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
)
type testHandler struct{}

View File

@@ -15,17 +15,17 @@ import (
"time"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/errors"
pberr "github.com/micro/go-micro/v2/errors/proto"
"github.com/micro/go-micro/v2/logger"
meta "github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/util/addr"
"github.com/micro/go-micro/v2/util/backoff"
mgrpc "github.com/micro/go-micro/v2/util/grpc"
mnet "github.com/micro/go-micro/v2/util/net"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/errors"
pberr "github.com/micro/go-micro/v3/errors/proto"
"github.com/micro/go-micro/v3/logger"
meta "github.com/micro/go-micro/v3/metadata"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v3/util/addr"
"github.com/micro/go-micro/v3/util/backoff"
mgrpc "github.com/micro/go-micro/v3/util/grpc"
mnet "github.com/micro/go-micro/v3/util/net"
"golang.org/x/net/netutil"
"google.golang.org/grpc"

View File

@@ -5,21 +5,20 @@ import (
"fmt"
"testing"
"github.com/micro/go-micro/v2"
bmemory "github.com/micro/go-micro/v2/broker/memory"
"github.com/micro/go-micro/v2/client"
gcli "github.com/micro/go-micro/v2/client/grpc"
"github.com/micro/go-micro/v2/errors"
pberr "github.com/micro/go-micro/v2/errors/proto"
rmemory "github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/router"
"github.com/micro/go-micro/v2/server"
gsrv "github.com/micro/go-micro/v2/server/grpc"
tgrpc "github.com/micro/go-micro/v2/transport/grpc"
bmemory "github.com/micro/go-micro/v3/broker/memory"
"github.com/micro/go-micro/v3/client"
gcli "github.com/micro/go-micro/v3/client/grpc"
"github.com/micro/go-micro/v3/errors"
pberr "github.com/micro/go-micro/v3/errors/proto"
rmemory "github.com/micro/go-micro/v3/registry/memory"
"github.com/micro/go-micro/v3/router"
rtreg "github.com/micro/go-micro/v3/router/registry"
"github.com/micro/go-micro/v3/server"
gsrv "github.com/micro/go-micro/v3/server/grpc"
pb "github.com/micro/go-micro/v3/server/grpc/proto"
tgrpc "github.com/micro/go-micro/v3/transport/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
pb "github.com/micro/go-micro/v2/server/grpc/proto"
)
// server is used to implement helloworld.GreeterServer.
@@ -117,7 +116,7 @@ func TestGRPCServer(t *testing.T) {
r := rmemory.NewRegistry()
b := bmemory.NewBroker()
tr := tgrpc.NewTransport()
rtr := router.NewRouter(router.Registry(r))
rtr := rtreg.NewRouter(router.Registry(r))
s := gsrv.NewServer(
server.Broker(b),
@@ -136,10 +135,7 @@ func TestGRPCServer(t *testing.T) {
h := &testServer{}
pb.RegisterTestHandler(s, h)
if err := micro.RegisterSubscriber("test_topic", s, h.Handle); err != nil {
t.Fatal(err)
}
if err := micro.RegisterSubscriber("error_topic", s, h.HandleError); err != nil {
if err := s.Subscribe(s.NewSubscriber("test_topic", h.Handle)); err != nil {
t.Fatal(err)
}
@@ -159,11 +155,10 @@ func TestGRPCServer(t *testing.T) {
}
}()
pub := micro.NewEvent("test_topic", c)
pubErr := micro.NewEvent("error_topic", c)
cnt := 4
for i := 0; i < cnt; i++ {
if err = pub.Publish(ctx, &pb.Request{Name: fmt.Sprintf("msg %d", i)}); err != nil {
msg := c.NewMessage("test_topic", &pb.Request{Name: fmt.Sprintf("msg %d", i)})
if err = c.Publish(ctx, msg); err != nil {
t.Fatal(err)
}
}
@@ -171,9 +166,6 @@ func TestGRPCServer(t *testing.T) {
if h.msgCount != cnt {
t.Fatalf("pub/sub not work, or invalid message count %d", h.msgCount)
}
if err = pubErr.Publish(ctx, &pb.Request{}); err == nil {
t.Fatal("this must return error, as we return error from handler")
}
cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
if err != nil {

View File

@@ -3,8 +3,8 @@ package grpc
import (
"reflect"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
type rpcHandler struct {

View File

@@ -5,12 +5,11 @@ import (
"crypto/tls"
"net"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/broker/http"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/registry/mdns"
"github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v3/transport"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
)
@@ -67,11 +66,10 @@ func MaxMsgSize(s int) server.Option {
func newOptions(opt ...server.Option) server.Options {
opts := server.Options{
Auth: auth.DefaultAuth,
Codecs: make(map[string]codec.NewCodec),
Metadata: map[string]string{},
Broker: broker.DefaultBroker,
Registry: registry.DefaultRegistry,
Broker: http.NewBroker(),
Registry: mdns.NewRegistry(),
Transport: transport.DefaultTransport,
Address: server.DefaultAddress,
Name: server.DefaultName,

View File

@@ -12,9 +12,9 @@ import (
import (
context "context"
api "github.com/micro/go-micro/v2/api"
client "github.com/micro/go-micro/v2/client"
server "github.com/micro/go-micro/v2/server"
api "github.com/micro/go-micro/v3/api"
client "github.com/micro/go-micro/v3/client"
server "github.com/micro/go-micro/v3/server"
)
// Reference imports to suppress errors if they are not otherwise used.

View File

@@ -1,8 +1,8 @@
package grpc
import (
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/codec/bytes"
)
type rpcRequest struct {

View File

@@ -1,7 +1,7 @@
package grpc
import (
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v3/codec"
)
type rpcResponse struct {

View File

@@ -14,8 +14,8 @@ import (
"unicode"
"unicode/utf8"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/server"
)
var (

View File

@@ -3,7 +3,7 @@ package grpc
import (
"context"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/server"
"google.golang.org/grpc"
)

View File

@@ -7,12 +7,12 @@ import (
"runtime/debug"
"strings"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/errors"
"github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/metadata"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
const (

View File

@@ -5,7 +5,7 @@ import (
"sync"
"github.com/google/uuid"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/server"
)
type MockServer struct {

View File

@@ -1,8 +1,8 @@
package mock
import (
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
type MockHandler struct {

View File

@@ -1,8 +1,8 @@
package mock
import (
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
type MockSubscriber struct {

View File

@@ -3,7 +3,7 @@ package mock
import (
"testing"
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/server"
)
func TestMockServer(t *testing.T) {

View File

@@ -1,11 +1,11 @@
package server
package mucp
import (
"fmt"
"reflect"
"strings"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
)
func extractValue(v reflect.Type, d int) *registry.Value {

View File

@@ -1,11 +1,11 @@
package server
package mucp
import (
"context"
"reflect"
"testing"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
)
type testHandler struct{}

View File

@@ -2,10 +2,14 @@
package mucp
import (
"github.com/micro/go-micro/v2/server"
"github.com/micro/go-micro/v3/server"
)
var (
DefaultRouter = newRpcRouter()
)
// NewServer returns a micro server interface
func NewServer(opts ...server.Option) server.Server {
return server.NewServer(opts...)
return newServer(opts...)
}

56
server/mucp/options.go Normal file
View File

@@ -0,0 +1,56 @@
package mucp
import (
"github.com/micro/go-micro/v3/broker/http"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/registry/mdns"
"github.com/micro/go-micro/v3/server"
thttp "github.com/micro/go-micro/v3/transport/http"
)
func newOptions(opt ...server.Option) server.Options {
opts := server.Options{
Codecs: make(map[string]codec.NewCodec),
Metadata: map[string]string{},
RegisterInterval: server.DefaultRegisterInterval,
RegisterTTL: server.DefaultRegisterTTL,
}
for _, o := range opt {
o(&opts)
}
if opts.Broker == nil {
opts.Broker = http.NewBroker()
}
if opts.Registry == nil {
opts.Registry = mdns.NewRegistry()
}
if opts.Transport == nil {
opts.Transport = thttp.NewTransport()
}
if opts.RegisterCheck == nil {
opts.RegisterCheck = server.DefaultRegisterCheck
}
if len(opts.Address) == 0 {
opts.Address = server.DefaultAddress
}
if len(opts.Name) == 0 {
opts.Name = server.DefaultName
}
if len(opts.Id) == 0 {
opts.Id = server.DefaultId
}
if len(opts.Version) == 0 {
opts.Version = server.DefaultVersion
}
return opts
}

View File

@@ -1,17 +1,17 @@
package server
package mucp
import (
"bytes"
"sync"
"github.com/micro/go-micro/v2/codec"
raw "github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/codec/grpc"
"github.com/micro/go-micro/v2/codec/json"
"github.com/micro/go-micro/v2/codec/jsonrpc"
"github.com/micro/go-micro/v2/codec/proto"
"github.com/micro/go-micro/v2/codec/protorpc"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/codec"
raw "github.com/micro/go-micro/v3/codec/bytes"
"github.com/micro/go-micro/v3/codec/grpc"
"github.com/micro/go-micro/v3/codec/json"
"github.com/micro/go-micro/v3/codec/jsonrpc"
"github.com/micro/go-micro/v3/codec/proto"
"github.com/micro/go-micro/v3/codec/protorpc"
"github.com/micro/go-micro/v3/transport"
"github.com/oxtoacart/bpool"
"github.com/pkg/errors"
)

View File

@@ -1,12 +1,12 @@
package server
package mucp
import (
"bytes"
"errors"
"testing"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/transport"
)
// testCodec is a dummy codec that only knows how to encode nil bodies

View File

@@ -1,8 +1,8 @@
package server
package mucp
import (
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/transport"
)
// event is a broker event we handle on the server transport

View File

@@ -1,20 +1,21 @@
package server
package mucp
import (
"reflect"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
type rpcHandler struct {
name string
handler interface{}
endpoints []*registry.Endpoint
opts HandlerOptions
opts server.HandlerOptions
}
func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
options := HandlerOptions{
func newRpcHandler(handler interface{}, opts ...server.HandlerOption) server.Handler {
options := server.HandlerOptions{
Metadata: make(map[string]map[string]string),
}
@@ -60,6 +61,6 @@ func (r *rpcHandler) Endpoints() []*registry.Endpoint {
return r.endpoints
}
func (r *rpcHandler) Options() HandlerOptions {
func (r *rpcHandler) Options() server.HandlerOptions {
return r.opts
}

View File

@@ -1,11 +1,11 @@
package server
package mucp
import (
"bytes"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v2/util/buf"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/transport"
"github.com/micro/go-micro/v3/util/buf"
)
type rpcRequest struct {

View File

@@ -1,10 +1,10 @@
package server
package mucp
import (
"net/http"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/transport"
)
type rpcResponse struct {

View File

@@ -1,4 +1,4 @@
package server
package mucp
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
@@ -18,8 +18,9 @@ import (
"unicode"
"unicode/utf8"
"github.com/micro/go-micro/v2/codec"
merrors "github.com/micro/go-micro/v2/errors"
"github.com/micro/go-micro/v3/codec"
merrors "github.com/micro/go-micro/v3/errors"
"github.com/micro/go-micro/v3/server"
)
var (
@@ -70,9 +71,9 @@ type router struct {
freeResp *response
// handler wrappers
hdlrWrappers []HandlerWrapper
hdlrWrappers []server.HandlerWrapper
// subscriber wrappers
subWrappers []SubscriberWrapper
subWrappers []server.SubscriberWrapper
su sync.RWMutex
subscribers map[string][]*subscriber
@@ -80,15 +81,15 @@ type router struct {
// rpcRouter encapsulates functions that become a server.Router
type rpcRouter struct {
h func(context.Context, Request, interface{}) error
m func(context.Context, Message) error
h func(context.Context, server.Request, interface{}) error
m func(context.Context, server.Message) error
}
func (r rpcRouter) ProcessMessage(ctx context.Context, msg Message) error {
func (r rpcRouter) ProcessMessage(ctx context.Context, msg server.Message) error {
return r.m(ctx, msg)
}
func (r rpcRouter) ServeRequest(ctx context.Context, req Request, rsp Response) error {
func (r rpcRouter) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
return r.h(ctx, req, rsp)
}
@@ -146,7 +147,7 @@ func prepareMethod(method reflect.Method) *methodType {
if stream {
// check stream type
streamType := reflect.TypeOf((*Stream)(nil)).Elem()
streamType := reflect.TypeOf((*server.Stream)(nil)).Elem()
if !argType.Implements(streamType) {
log.Errorf("%v argument does not implement Stream interface: %v", mname, argType)
return nil
@@ -220,7 +221,7 @@ func (s *service) call(ctx context.Context, router *router, sending *sync.Mutex,
}
if !mtype.stream {
fn := func(ctx context.Context, req Request, rsp interface{}) error {
fn := func(ctx context.Context, req server.Request, rsp interface{}) error {
returnValues = function.Call([]reflect.Value{s.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(argv.Interface()), reflect.ValueOf(rsp)})
// The return value for the method is an error.
@@ -256,7 +257,7 @@ func (s *service) call(ctx context.Context, router *router, sending *sync.Mutex,
}
// Invoke the method, providing a new value for the reply.
fn := func(ctx context.Context, req Request, stream interface{}) error {
fn := func(ctx context.Context, req server.Request, stream interface{}) error {
returnValues = function.Call([]reflect.Value{s.rcvr, mtype.prepareContext(ctx), reflect.ValueOf(stream)})
if err := returnValues[0].Interface(); err != nil {
// the function returned an error, we use that
@@ -328,7 +329,7 @@ func (router *router) freeResponse(resp *response) {
router.respLock.Unlock()
}
func (router *router) readRequest(r Request) (service *service, mtype *methodType, req *request, argv, replyv reflect.Value, keepReading bool, err error) {
func (router *router) readRequest(r server.Request) (service *service, mtype *methodType, req *request, argv, replyv reflect.Value, keepReading bool, err error) {
cc := r.Codec()
service, mtype, req, keepReading, err = router.readHeader(cc)
@@ -411,11 +412,11 @@ func (router *router) readHeader(cc codec.Reader) (service *service, mtype *meth
return
}
func (router *router) NewHandler(h interface{}, opts ...HandlerOption) Handler {
func (router *router) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
return newRpcHandler(h, opts...)
}
func (router *router) Handle(h Handler) error {
func (router *router) Handle(h server.Handler) error {
router.mu.Lock()
defer router.mu.Unlock()
if router.serviceMap == nil {
@@ -460,7 +461,7 @@ func (router *router) Handle(h Handler) error {
return nil
}
func (router *router) ServeRequest(ctx context.Context, r Request, rsp Response) error {
func (router *router) ServeRequest(ctx context.Context, r server.Request, rsp server.Response) error {
sending := new(sync.Mutex)
service, mtype, req, argv, replyv, keepReading, err := router.readRequest(r)
if err != nil {
@@ -476,11 +477,11 @@ func (router *router) ServeRequest(ctx context.Context, r Request, rsp Response)
return service.call(ctx, router, sending, mtype, req, argv, replyv, rsp.Codec())
}
func (router *router) NewSubscriber(topic string, handler interface{}, opts ...SubscriberOption) Subscriber {
func (router *router) NewSubscriber(topic string, handler interface{}, opts ...server.SubscriberOption) server.Subscriber {
return newSubscriber(topic, handler, opts...)
}
func (router *router) Subscribe(s Subscriber) error {
func (router *router) Subscribe(s server.Subscriber) error {
sub, ok := s.(*subscriber)
if !ok {
return fmt.Errorf("invalid subscriber: expected *subscriber")
@@ -504,7 +505,11 @@ func (router *router) Subscribe(s Subscriber) error {
return nil
}
func (router *router) ProcessMessage(ctx context.Context, msg Message) (err error) {
func (router *router) String() string {
return "mucp"
}
func (router *router) ProcessMessage(ctx context.Context, msg server.Message) (err error) {
defer func() {
// recover any panics
if r := recover(); r != nil {
@@ -561,7 +566,7 @@ func (router *router) ProcessMessage(ctx context.Context, msg Message) (err erro
}
// create the handler which will honour the SubscriberFunc type
fn := func(ctx context.Context, msg Message) error {
fn := func(ctx context.Context, msg server.Message) error {
var vals []reflect.Value
if sub.typ.Kind() != reflect.Func {
vals = append(vals, sub.rcvr)

View File

@@ -1,4 +1,4 @@
package server
package mucp
import (
"context"
@@ -12,17 +12,18 @@ import (
"sync"
"time"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/codec"
raw "github.com/micro/go-micro/v2/codec/bytes"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/metadata"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v2/util/addr"
"github.com/micro/go-micro/v2/util/backoff"
mnet "github.com/micro/go-micro/v2/util/net"
"github.com/micro/go-micro/v2/util/socket"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/codec"
raw "github.com/micro/go-micro/v3/codec/bytes"
"github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/metadata"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v3/transport"
"github.com/micro/go-micro/v3/util/addr"
"github.com/micro/go-micro/v3/util/backoff"
mnet "github.com/micro/go-micro/v3/util/net"
"github.com/micro/go-micro/v3/util/socket"
)
type rpcServer struct {
@@ -30,9 +31,9 @@ type rpcServer struct {
exit chan chan error
sync.RWMutex
opts Options
handlers map[string]Handler
subscribers map[Subscriber][]broker.Subscriber
opts server.Options
handlers map[string]server.Handler
subscribers map[server.Subscriber][]broker.Subscriber
// marks the serve as started
started bool
// used for first registration
@@ -45,7 +46,22 @@ type rpcServer struct {
rsvc *registry.Service
}
func newRpcServer(opts ...Option) Server {
var (
log = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
)
func wait(ctx context.Context) *sync.WaitGroup {
if ctx == nil {
return nil
}
wg, ok := ctx.Value("wait").(*sync.WaitGroup)
if !ok {
return nil
}
return wg
}
func newServer(opts ...server.Option) server.Server {
options := newOptions(opts...)
router := newRpcRouter()
router.hdlrWrappers = options.HdlrWrappers
@@ -54,8 +70,8 @@ func newRpcServer(opts ...Option) Server {
return &rpcServer{
opts: options,
router: router,
handlers: make(map[string]Handler),
subscribers: make(map[Subscriber][]broker.Subscriber),
handlers: make(map[string]server.Handler),
subscribers: make(map[server.Subscriber][]broker.Subscriber),
exit: make(chan chan error),
wg: wait(options.Context),
}
@@ -110,7 +126,7 @@ func (s *rpcServer) HandleEvent(e broker.Event) error {
}
// existing router
r := Router(s.router)
r := server.Router(s.router)
// if the router is present then execute it
if s.opts.Router != nil {
@@ -333,13 +349,13 @@ func (s *rpcServer) ServeConn(sock transport.Socket) {
}
// set router
r := Router(s.router)
r := server.Router(s.router)
// if not nil use the router specified
if s.opts.Router != nil {
// create a wrapped function
handler := func(ctx context.Context, req Request, rsp interface{}) error {
return s.opts.Router.ServeRequest(ctx, req, rsp.(Response))
handler := func(ctx context.Context, req server.Request, rsp interface{}) error {
return s.opts.Router.ServeRequest(ctx, req, rsp.(server.Response))
}
// execute the wrapper for it
@@ -439,14 +455,14 @@ func (s *rpcServer) newCodec(contentType string) (codec.NewCodec, error) {
return nil, fmt.Errorf("Unsupported Content-Type: %s", contentType)
}
func (s *rpcServer) Options() Options {
func (s *rpcServer) Options() server.Options {
s.RLock()
opts := s.opts
s.RUnlock()
return opts
}
func (s *rpcServer) Init(opts ...Option) error {
func (s *rpcServer) Init(opts ...server.Option) error {
s.Lock()
defer s.Unlock()
@@ -467,11 +483,11 @@ func (s *rpcServer) Init(opts ...Option) error {
return nil
}
func (s *rpcServer) NewHandler(h interface{}, opts ...HandlerOption) Handler {
func (s *rpcServer) NewHandler(h interface{}, opts ...server.HandlerOption) server.Handler {
return s.router.NewHandler(h, opts...)
}
func (s *rpcServer) Handle(h Handler) error {
func (s *rpcServer) Handle(h server.Handler) error {
s.Lock()
defer s.Unlock()
@@ -484,11 +500,11 @@ func (s *rpcServer) Handle(h Handler) error {
return nil
}
func (s *rpcServer) NewSubscriber(topic string, sb interface{}, opts ...SubscriberOption) Subscriber {
func (s *rpcServer) NewSubscriber(topic string, sb interface{}, opts ...server.SubscriberOption) server.Subscriber {
return s.router.NewSubscriber(topic, sb, opts...)
}
func (s *rpcServer) Subscribe(sb Subscriber) error {
func (s *rpcServer) Subscribe(sb server.Subscriber) error {
s.Lock()
defer s.Unlock()
@@ -606,7 +622,7 @@ func (s *rpcServer) Register() error {
sort.Strings(handlerList)
var subscriberList []Subscriber
var subscriberList []server.Subscriber
for e := range s.subscribers {
// Only advertise non internal subscribers
if !e.Options().Internal {

View File

@@ -1,4 +1,4 @@
package server
package mucp
import (
"context"
@@ -6,7 +6,8 @@ import (
"io"
"sync"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/server"
)
// Implements the Streamer interface
@@ -15,7 +16,7 @@ type rpcStream struct {
id string
closed bool
err error
request Request
request server.Request
codec codec.Codec
context context.Context
}
@@ -24,7 +25,7 @@ func (r *rpcStream) Context() context.Context {
return r.context
}
func (r *rpcStream) Request() Request {
func (r *rpcStream) Request() server.Request {
return r.request
}

View File

@@ -1,4 +1,4 @@
package server
package mucp
import (
"bytes"
@@ -10,8 +10,8 @@ import (
"time"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/v2/codec/json"
protoCodec "github.com/micro/go-micro/v2/codec/proto"
"github.com/micro/go-micro/v3/codec/json"
protoCodec "github.com/micro/go-micro/v3/codec/proto"
)
// protoStruct implements proto.Message

View File

@@ -1,4 +1,4 @@
package server
package mucp
import (
"sync"

View File

@@ -1,10 +1,11 @@
package server
package mucp
import (
"fmt"
"reflect"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/server"
)
const (
@@ -24,11 +25,11 @@ type subscriber struct {
subscriber interface{}
handlers []*handler
endpoints []*registry.Endpoint
opts SubscriberOptions
opts server.SubscriberOptions
}
func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subscriber {
options := SubscriberOptions{
func newSubscriber(topic string, sub interface{}, opts ...server.SubscriberOption) server.Subscriber {
options := server.SubscriberOptions{
AutoAck: true,
}
@@ -104,7 +105,7 @@ func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subs
}
}
func validateSubscriber(sub Subscriber) error {
func validateSubscriber(sub server.Subscriber) error {
typ := reflect.TypeOf(sub.Subscriber())
var argType reflect.Type
@@ -170,6 +171,6 @@ func (s *subscriber) Endpoints() []*registry.Endpoint {
return s.endpoints
}
func (s *subscriber) Options() SubscriberOptions {
func (s *subscriber) Options() server.SubscriberOptions {
return s.opts
}

View File

@@ -6,12 +6,14 @@ import (
"sync"
"time"
"github.com/micro/go-micro/v2/auth"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/debug/trace"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-micro/v2/transport"
"github.com/micro/go-micro/v3/auth"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/broker/http"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/debug/trace"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/registry/mdns"
"github.com/micro/go-micro/v3/transport"
)
type Options struct {
@@ -61,16 +63,12 @@ func newOptions(opt ...Option) Options {
o(&opts)
}
if opts.Auth == nil {
opts.Auth = auth.DefaultAuth
}
if opts.Broker == nil {
opts.Broker = broker.DefaultBroker
opts.Broker = http.NewBroker()
}
if opts.Registry == nil {
opts.Registry = registry.DefaultRegistry
opts.Registry = mdns.NewRegistry()
}
if opts.Transport == nil {

View File

@@ -11,9 +11,9 @@ import (
import (
context "context"
api "github.com/micro/go-micro/v2/api"
client "github.com/micro/go-micro/v2/client"
server "github.com/micro/go-micro/v2/server"
api "github.com/micro/go-micro/v3/api"
client "github.com/micro/go-micro/v3/client"
server "github.com/micro/go-micro/v3/server"
)
// Reference imports to suppress errors if they are not otherwise used.

View File

@@ -3,15 +3,11 @@ package server
import (
"context"
"os"
"os/signal"
"time"
"github.com/google/uuid"
"github.com/micro/go-micro/v2/codec"
"github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
signalutil "github.com/micro/go-micro/v2/util/signal"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/registry"
)
// Server is a simple micro server abstraction
@@ -137,105 +133,11 @@ type Subscriber interface {
type Option func(*Options)
var (
DefaultAddress = ":0"
DefaultName = "go.micro.server"
DefaultVersion = "latest"
DefaultId = uuid.New().String()
DefaultServer Server = newRpcServer()
DefaultRouter = newRpcRouter()
DefaultRegisterCheck = func(context.Context) error { return nil }
DefaultRegisterInterval = time.Second * 30
DefaultRegisterTTL = time.Second * 90
// NewServer creates a new server
NewServer func(...Option) Server = newRpcServer
log = logger.NewHelper(logger.DefaultLogger).WithFields(map[string]interface{}{"service": "server"})
DefaultAddress = ":0"
DefaultName = "go.micro.server"
DefaultVersion = "latest"
DefaultId = uuid.New().String()
DefaultRegisterCheck = func(context.Context) error { return nil }
DefaultRegisterInterval = time.Second * 30
DefaultRegisterTTL = time.Second * 90
)
// DefaultOptions returns config options for the default service
func DefaultOptions() Options {
return DefaultServer.Options()
}
// Init initialises the default server with options passed in
func Init(opt ...Option) {
if DefaultServer == nil {
DefaultServer = newRpcServer(opt...)
}
DefaultServer.Init(opt...)
}
// NewRouter returns a new router
func NewRouter() *router {
return newRpcRouter()
}
// NewSubscriber creates a new subscriber interface with the given topic
// and handler using the default server
func NewSubscriber(topic string, h interface{}, opts ...SubscriberOption) Subscriber {
return DefaultServer.NewSubscriber(topic, h, opts...)
}
// NewHandler creates a new handler interface using the default server
// Handlers are required to be a public object with public
// endpoints. Call to a service endpoint such as Foo.Bar expects
// the type:
//
// type Foo struct {}
// func (f *Foo) Bar(ctx, req, rsp) error {
// return nil
// }
//
func NewHandler(h interface{}, opts ...HandlerOption) Handler {
return DefaultServer.NewHandler(h, opts...)
}
// Handle registers a handler interface with the default server to
// handle inbound requests
func Handle(h Handler) error {
return DefaultServer.Handle(h)
}
// Subscribe registers a subscriber interface with the default server
// which subscribes to specified topic with the broker
func Subscribe(s Subscriber) error {
return DefaultServer.Subscribe(s)
}
// Run starts the default server and waits for a kill
// signal before exiting. Also registers/deregisters the server
func Run() error {
if err := Start(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
signal.Notify(ch, signalutil.Shutdown()...)
if logger.V(logger.InfoLevel, log) {
log.Infof("Received signal %s", <-ch)
}
return Stop()
}
// Start starts the default server
func Start() error {
config := DefaultServer.Options()
if logger.V(logger.InfoLevel, log) {
log.Infof("Starting server %s id %s", config.Name, config.Id)
}
return DefaultServer.Start()
}
// Stop stops the default server
func Stop() error {
if logger.V(logger.InfoLevel, log) {
log.Infof("Stopping server")
}
return DefaultServer.Stop()
}
// String returns name of Server implementation
func String() string {
return DefaultServer.String()
}