many fixes for lint and context.Context usage (#5)
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -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"
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user