many fixes for lint and context.Context usage (#5)

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2020-11-03 02:02:32 +03:00
committed by GitHub
parent 40b0870cf8
commit 8a2b122015
44 changed files with 152 additions and 1175 deletions

View File

@@ -10,13 +10,13 @@ import (
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/oxtoacart/bpool"
jsonrpc "github.com/unistack-org/micro-codec-jsonrpc"
protorpc "github.com/unistack-org/micro-codec-protorpc"
"github.com/unistack-org/micro/v3/api"
"github.com/unistack-org/micro/v3/api/handler"
"github.com/unistack-org/micro/v3/api/internal/proto"
"github.com/unistack-org/micro/v3/client"
"github.com/unistack-org/micro/v3/codec"
"github.com/unistack-org/micro/v3/codec/jsonrpc"
"github.com/unistack-org/micro/v3/codec/protorpc"
"github.com/unistack-org/micro/v3/errors"
"github.com/unistack-org/micro/v3/logger"
"github.com/unistack-org/micro/v3/metadata"

View File

@@ -1,6 +1,8 @@
package router
import (
"context"
"github.com/unistack-org/micro/v3/api/resolver"
"github.com/unistack-org/micro/v3/api/resolver/vpath"
"github.com/unistack-org/micro/v3/registry"
@@ -10,12 +12,14 @@ type Options struct {
Handler string
Registry registry.Registry
Resolver resolver.Resolver
Context context.Context
}
type Option func(o *Options)
func NewOptions(opts ...Option) Options {
options := Options{
Context: context.Background(),
Handler: "meta",
}
@@ -32,18 +36,28 @@ func NewOptions(opts ...Option) Options {
return options
}
// WithContext sets the context
func WithContext(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx
}
}
// WithHandler sets the handler
func WithHandler(h string) Option {
return func(o *Options) {
o.Handler = h
}
}
// WithRegistry sets the registry
func WithRegistry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
}
}
// WithResolver sets the resolver
func WithResolver(r resolver.Resolver) Option {
return func(o *Options) {
o.Resolver = r

View File

@@ -50,7 +50,7 @@ func (r *registryRouter) refresh() {
var attempts int
for {
services, err := r.opts.Registry.ListServices()
services, err := r.opts.Registry.ListServices(r.opts.Context)
if err != nil {
attempts++
if logger.V(logger.ErrorLevel) {
@@ -64,7 +64,7 @@ func (r *registryRouter) refresh() {
// for each service, get service and store endpoints
for _, s := range services {
service, err := r.opts.Registry.GetService(s.Name)
service, err := r.opts.Registry.GetService(r.opts.Context, s.Name)
if err != nil {
if logger.V(logger.ErrorLevel) {
logger.Errorf("unable to get service: %v", err)
@@ -92,7 +92,7 @@ func (r *registryRouter) process(res *registry.Result) {
}
// get entry from cache
service, err := r.opts.Registry.GetService(res.Service.Name)
service, err := r.opts.Registry.GetService(r.opts.Context, res.Service.Name)
if err != nil {
if logger.V(logger.ErrorLevel) {
logger.Errorf("unable to get %v service: %v", res.Service.Name, err)
@@ -230,7 +230,7 @@ func (r *registryRouter) watch() {
}
// watch for changes
w, err := r.opts.Registry.Watch()
w, err := r.opts.Registry.Watch(r.opts.Context)
if err != nil {
attempts++
if logger.V(logger.ErrorLevel) {
@@ -432,7 +432,7 @@ func (r *registryRouter) Route(req *http.Request) (*api.Service, error) {
name := rp.Name
// get service
services, err := r.opts.Registry.GetService(name, registry.GetDomain(rp.Domain))
services, err := r.opts.Registry.GetService(r.opts.Context, name, registry.GetDomain(rp.Domain))
if err != nil {
return nil, err
}

View File

@@ -177,7 +177,7 @@ func (r *staticRouter) Endpoint(req *http.Request) (*api.Service, error) {
}
epf := strings.Split(ep.apiep.Name, ".")
services, err := r.opts.Registry.GetService(epf[0])
services, err := r.opts.Registry.GetService(r.opts.Context, epf[0])
if err != nil {
return nil, err
}