From f892b41299b8e6d40f24ffb221f809b39e47e01f Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Mon, 11 May 2020 17:09:28 +0100 Subject: [PATCH] Add runtime to service.Options() --- api/resolver/vpath/vpath.go | 3 --- config/cmd/options.go | 6 ++++++ options.go | 10 ++++++++++ runtime/options.go | 11 +++++++++++ runtime/service/service.go | 15 ++++++++------- service.go | 17 +++++++---------- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/api/resolver/vpath/vpath.go b/api/resolver/vpath/vpath.go index cf407ded..f653a86f 100644 --- a/api/resolver/vpath/vpath.go +++ b/api/resolver/vpath/vpath.go @@ -3,7 +3,6 @@ package vpath import ( "errors" - "fmt" "net/http" "regexp" "strings" @@ -28,8 +27,6 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) { return nil, errors.New("unknown name") } - fmt.Println(req.URL.Path) - parts := strings.Split(req.URL.Path[1:], "/") if len(parts) == 1 { return &resolver.Endpoint{ diff --git a/config/cmd/options.go b/config/cmd/options.go index 7ec9e2b4..1dfc8076 100644 --- a/config/cmd/options.go +++ b/config/cmd/options.go @@ -100,6 +100,12 @@ func Registry(r *registry.Registry) Option { } } +func Runtime(r *runtime.Runtime) Option { + return func(o *Options) { + o.Runtime = r + } +} + func Transport(t *transport.Transport) Option { return func(o *Options) { o.Transport = t diff --git a/options.go b/options.go index d216b53e..b5e6372f 100644 --- a/options.go +++ b/options.go @@ -14,6 +14,7 @@ import ( "github.com/micro/go-micro/v2/debug/profile" "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/registry" + "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/transport" @@ -29,6 +30,7 @@ type Options struct { Server server.Server Store store.Store Registry registry.Registry + Runtime runtime.Runtime Transport transport.Transport Profile profile.Profile @@ -55,6 +57,7 @@ func newOptions(opts ...Option) Options { Server: server.DefaultServer, Store: store.DefaultStore, Registry: registry.DefaultRegistry, + Runtime: runtime.DefaultRuntime, Transport: transport.DefaultTransport, Context: context.Background(), Signal: true, @@ -182,6 +185,13 @@ func Transport(t transport.Transport) Option { } } +// Runtime sets the runtime +func Runtime(r runtime.Runtime) Option { + return func(o *Options) { + o.Runtime = r + } +} + // Convenience options // Address sets the address of the server diff --git a/runtime/options.go b/runtime/options.go index d7d9fe90..dea936b9 100644 --- a/runtime/options.go +++ b/runtime/options.go @@ -3,6 +3,8 @@ package runtime import ( "context" "io" + + "github.com/micro/go-micro/v2/client" ) type Option func(o *Options) @@ -17,6 +19,8 @@ type Options struct { Source string // Base image to use Image string + // Client to use when making requests + Client client.Client } // WithSource sets the base image / repository @@ -47,6 +51,13 @@ func WithImage(t string) Option { } } +// WithClient sets the client to use +func WithClient(c client.Client) Option { + return func(o *Options) { + o.Client = c + } +} + type CreateOption func(o *CreateOptions) type ReadOption func(o *ReadOptions) diff --git a/runtime/service/service.go b/runtime/service/service.go index e6bc03fc..90451028 100644 --- a/runtime/service/service.go +++ b/runtime/service/service.go @@ -24,6 +24,9 @@ func (s *svc) Init(opts ...runtime.Option) error { o(&s.options) } + // reset the runtime as the client could have changed + s.runtime = pb.NewRuntimeService(runtime.DefaultName, s.options.Client) + return nil } @@ -278,19 +281,17 @@ func (s *svc) String() string { // NewRuntime creates new service runtime and returns it func NewRuntime(opts ...runtime.Option) runtime.Runtime { - // get default options - options := runtime.Options{} + var options runtime.Options - // apply requested options for _, o := range opts { o(&options) } - - // create default client - cli := client.DefaultClient + if options.Client == nil { + options.Client = client.DefaultClient + } return &svc{ options: options, - runtime: pb.NewRuntimeService(runtime.DefaultName, cli), + runtime: pb.NewRuntimeService(runtime.DefaultName, options.Client), } } diff --git a/service.go b/service.go index c81127d2..02c8ef6b 100644 --- a/service.go +++ b/service.go @@ -3,7 +3,7 @@ package micro import ( "os" "os/signal" - "runtime" + rtime "runtime" "strings" "sync" @@ -15,6 +15,7 @@ import ( "github.com/micro/go-micro/v2/debug/trace" "github.com/micro/go-micro/v2/logger" "github.com/micro/go-micro/v2/plugin" + "github.com/micro/go-micro/v2/runtime" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" signalutil "github.com/micro/go-micro/v2/util/signal" @@ -98,6 +99,7 @@ func (s *service) Init(opts ...Option) { cmd.Auth(&s.opts.Auth), cmd.Broker(&s.opts.Broker), cmd.Registry(&s.opts.Registry), + cmd.Runtime(&s.opts.Runtime), cmd.Transport(&s.opts.Transport), cmd.Client(&s.opts.Client), cmd.Config(&s.opts.Config), @@ -112,13 +114,8 @@ func (s *service) Init(opts ...Option) { name := s.opts.Cmd.App().Name s.opts.Store.Init(store.Table(name)) - // TODO: replace Cmd.Init with config.Load - // Right now we're just going to load a token - // May need to re-read value on change - // TODO: should be scoped to micro/auth/token - // if tk, _ := config.Get("token"); len(tk) > 0 { - // s.opts.Auth.Init(auth.ServiceToken(tk)) - // } + // Set the client for the micro clients + s.opts.Runtime.Init(runtime.WithClient(s.Client())) }) } @@ -192,9 +189,9 @@ func (s *service) Run() error { // start the profiler if s.opts.Profile != nil { // to view mutex contention - runtime.SetMutexProfileFraction(5) + rtime.SetMutexProfileFraction(5) // to view blocking profile - runtime.SetBlockProfileRate(1) + rtime.SetBlockProfileRate(1) if err := s.opts.Profile.Start(); err != nil { return err