fieldalignment of all structs to save memory
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
ee11f39a2f
commit
86626c5922
.github/workflows
.golangci.ymlapi
auth
broker
client
codec
config
errors
flow
logger
metadata
meter
network
options.goprofiler
proxy
register
router
runtime
selector
server
service.gosync
tracer
util
addr
buf
net
pki
qson
rand
reflect
register
router
sync
token
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@ -50,10 +50,9 @@ jobs:
|
||||
uses: actions/checkout@v2
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
version: v1.39
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
|
3
.github/workflows/pr.yml
vendored
3
.github/workflows/pr.yml
vendored
@ -50,10 +50,9 @@ jobs:
|
||||
uses: actions/checkout@v2
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v2
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
version: v1.30
|
||||
version: v1.39
|
||||
# Optional: working directory, useful for monorepos
|
||||
# working-directory: somedir
|
||||
# Optional: golangci-lint command line arguments.
|
||||
|
@ -1,30 +1,44 @@
|
||||
run:
|
||||
concurrency: 4
|
||||
deadline: 5m
|
||||
modules-download-mode: readonly
|
||||
skip-files:
|
||||
- ".*\\.pb\\.go$"
|
||||
- ".*\\.pb\\.micro\\.go$"
|
||||
issues-exit-code: 1
|
||||
tests: true
|
||||
|
||||
linters-settings:
|
||||
govet:
|
||||
check-shadowing: true
|
||||
enable:
|
||||
- fieldalignment
|
||||
|
||||
linters:
|
||||
disable-all: false
|
||||
enable-all: false
|
||||
enable:
|
||||
- megacheck
|
||||
- staticcheck
|
||||
- deadcode
|
||||
- varcheck
|
||||
- gosimple
|
||||
- unused
|
||||
- prealloc
|
||||
- scopelint
|
||||
- gocritic
|
||||
- goimports
|
||||
- unconvert
|
||||
- govet
|
||||
- nakedret
|
||||
- deadcode
|
||||
- errcheck
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- structcheck
|
||||
- gosec
|
||||
disable:
|
||||
- maligned
|
||||
- interfacer
|
||||
- typecheck
|
||||
- dupl
|
||||
- unused
|
||||
- varcheck
|
||||
- bodyclose
|
||||
- gci
|
||||
- goconst
|
||||
- gocritic
|
||||
- gosimple
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goimports
|
||||
- golint
|
||||
- gosec
|
||||
- makezero
|
||||
- misspell
|
||||
- nakedret
|
||||
- nestif
|
||||
- nilerr
|
||||
- noctx
|
||||
- prealloc
|
||||
- unconvert
|
||||
- unparam
|
||||
disable-all: false
|
||||
|
@ -149,5 +149,4 @@ func TestValidate(t *testing.T) {
|
||||
if err := Validate(epPcreInvalid); err == nil {
|
||||
t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0])
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,10 +6,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultMaxRecvSize specifies max recv size for handler
|
||||
DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
|
||||
)
|
||||
// DefaultMaxRecvSize specifies max recv size for handler
|
||||
var DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
|
||||
|
||||
// Options struct holds handler options
|
||||
type Options struct {
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
// NewResolver creates new subdomain api resolver
|
||||
func NewResolver(parent resolver.Resolver, opts ...resolver.Option) resolver.Resolver {
|
||||
options := resolver.NewOptions(opts...)
|
||||
return &subdomainResolver{options, parent}
|
||||
return &subdomainResolver{opts: options, Resolver: parent}
|
||||
}
|
||||
|
||||
type subdomainResolver struct {
|
||||
|
@ -19,9 +19,7 @@ type vpathResolver struct {
|
||||
opts resolver.Options
|
||||
}
|
||||
|
||||
var (
|
||||
re = regexp.MustCompile("^v[0-9]+$")
|
||||
)
|
||||
var re = regexp.MustCompile("^v[0-9]+$")
|
||||
|
||||
// Resolve endpoint
|
||||
func (r *vpathResolver) Resolve(req *http.Request, opts ...resolver.ResolveOption) (*resolver.Endpoint, error) {
|
||||
|
@ -7,10 +7,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/api"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultRouter contains default router implementation
|
||||
DefaultRouter Router
|
||||
)
|
||||
// DefaultRouter contains default router implementation
|
||||
var DefaultRouter Router
|
||||
|
||||
// Router is used to determine an endpoint for a request
|
||||
type Router interface {
|
||||
|
@ -24,11 +24,11 @@ func TestVerify(t *testing.T) {
|
||||
}
|
||||
|
||||
tt := []struct {
|
||||
Name string
|
||||
Rules []*Rule
|
||||
Error error
|
||||
Account *Account
|
||||
Resource *Resource
|
||||
Error error
|
||||
Name string
|
||||
Rules []*Rule
|
||||
}{
|
||||
{
|
||||
Name: "NoRules",
|
||||
|
@ -7,10 +7,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultBroker default broker
|
||||
DefaultBroker Broker = NewBroker()
|
||||
)
|
||||
// DefaultBroker default broker
|
||||
var DefaultBroker Broker = NewBroker()
|
||||
|
||||
// Broker is an interface used for asynchronous messaging.
|
||||
type Broker interface {
|
||||
|
@ -9,16 +9,10 @@ import (
|
||||
"github.com/unistack-org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultCodecs will be used to encode/decode data
|
||||
DefaultCodecs = map[string]codec.Codec{
|
||||
//"application/json": cjson.NewCodec,
|
||||
//"application/json-rpc": cjsonrpc.NewCodec,
|
||||
//"application/protobuf": cproto.NewCodec,
|
||||
//"application/proto-rpc": cprotorpc.NewCodec,
|
||||
"application/octet-stream": codec.NewCodec(),
|
||||
}
|
||||
)
|
||||
// DefaultCodecs will be used to encode/decode data
|
||||
var DefaultCodecs = map[string]codec.Codec{
|
||||
"application/octet-stream": codec.NewCodec(),
|
||||
}
|
||||
|
||||
type noopClient struct {
|
||||
opts Options
|
||||
|
@ -5,8 +5,7 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type noopCodec struct {
|
||||
}
|
||||
type noopCodec struct{}
|
||||
|
||||
func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error {
|
||||
return nil
|
||||
|
@ -6,10 +6,8 @@ import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultConfig default config
|
||||
DefaultConfig Config = NewConfig()
|
||||
)
|
||||
// DefaultConfig default config
|
||||
var DefaultConfig Config = NewConfig()
|
||||
|
||||
var (
|
||||
// ErrCodecMissing is returned when codec needed and not specified
|
||||
@ -37,10 +35,10 @@ type Config interface {
|
||||
}
|
||||
|
||||
// Watcher is the config watcher
|
||||
//type Watcher interface {
|
||||
// type Watcher interface {
|
||||
// Next() (, error)
|
||||
// Stop() error
|
||||
//}
|
||||
// }
|
||||
|
||||
// Load loads config from config sources
|
||||
func Load(ctx context.Context, cs ...Config) error {
|
||||
|
@ -35,7 +35,7 @@ func (c *defaultConfig) Load(ctx context.Context) error {
|
||||
src, err := rutil.Zero(c.opts.Struct)
|
||||
if err == nil {
|
||||
valueOf := reflect.ValueOf(src)
|
||||
if err = c.fillValues(ctx, valueOf); err == nil {
|
||||
if err = c.fillValues(valueOf); err == nil {
|
||||
err = mergo.Merge(c.opts.Struct, src, mergo.WithOverride, mergo.WithTypeCheck, mergo.WithAppendSlice)
|
||||
}
|
||||
}
|
||||
@ -54,7 +54,7 @@ func (c *defaultConfig) Load(ctx context.Context) error {
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val string) error {
|
||||
func (c *defaultConfig) fillValue(value reflect.Value, val string) error {
|
||||
if !rutil.IsEmpty(value) {
|
||||
return nil
|
||||
}
|
||||
@ -71,10 +71,10 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
|
||||
kv := strings.FieldsFunc(nval, func(c rune) bool { return c == '=' })
|
||||
mkey := reflect.Indirect(reflect.New(kt))
|
||||
mval := reflect.Indirect(reflect.New(et))
|
||||
if err := c.fillValue(ctx, mkey, kv[0]); err != nil {
|
||||
if err := c.fillValue(mkey, kv[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.fillValue(ctx, mval, kv[1]); err != nil {
|
||||
if err := c.fillValue(mval, kv[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
value.SetMapIndex(mkey, mval)
|
||||
@ -84,7 +84,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
|
||||
value.Set(reflect.MakeSlice(reflect.SliceOf(value.Type().Elem()), len(nvals), len(nvals)))
|
||||
for idx, nval := range nvals {
|
||||
nvalue := reflect.Indirect(reflect.New(value.Type().Elem()))
|
||||
if err := c.fillValue(ctx, nvalue, nval); err != nil {
|
||||
if err := c.fillValue(nvalue, nval); err != nil {
|
||||
return err
|
||||
}
|
||||
value.Index(idx).Set(nvalue)
|
||||
@ -173,7 +173,7 @@ func (c *defaultConfig) fillValue(ctx context.Context, value reflect.Value, val
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) error {
|
||||
func (c *defaultConfig) fillValues(valueOf reflect.Value) error {
|
||||
var values reflect.Value
|
||||
|
||||
if valueOf.Kind() == reflect.Ptr {
|
||||
@ -200,7 +200,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
|
||||
switch value.Kind() {
|
||||
case reflect.Struct:
|
||||
value.Set(reflect.Indirect(reflect.New(value.Type())))
|
||||
if err := c.fillValues(ctx, value); err != nil {
|
||||
if err := c.fillValues(value); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
@ -214,7 +214,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
|
||||
value.Set(reflect.New(value.Type().Elem()))
|
||||
}
|
||||
value = value.Elem()
|
||||
if err := c.fillValues(ctx, value); err != nil {
|
||||
if err := c.fillValues(value); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
@ -224,7 +224,7 @@ func (c *defaultConfig) fillValues(ctx context.Context, valueOf reflect.Value) e
|
||||
continue
|
||||
}
|
||||
|
||||
if err := c.fillValue(ctx, value, tag); err != nil {
|
||||
if err := c.fillValue(value, tag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -10,30 +10,30 @@ import (
|
||||
|
||||
type Cfg struct {
|
||||
StringValue string `default:"string_value"`
|
||||
IntValue int `default:"99"`
|
||||
IgnoreValue string `json:"-"`
|
||||
StructValue struct {
|
||||
StringValue string `default:"string_value"`
|
||||
}
|
||||
IntValue int `default:"99"`
|
||||
}
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conf := &Cfg{IntValue: 10}
|
||||
blfn := func(ctx context.Context, cfg config.Config) error {
|
||||
conf, ok := cfg.Options().Struct.(*Cfg)
|
||||
nconf, ok := cfg.Options().Struct.(*Cfg)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
|
||||
}
|
||||
conf.StringValue = "before_load"
|
||||
nconf.StringValue = "before_load"
|
||||
return nil
|
||||
}
|
||||
alfn := func(ctx context.Context, cfg config.Config) error {
|
||||
conf, ok := cfg.Options().Struct.(*Cfg)
|
||||
nconf, ok := cfg.Options().Struct.(*Cfg)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to get Struct from options: %v", cfg.Options())
|
||||
}
|
||||
conf.StringValue = "after_load"
|
||||
nconf.StringValue = "after_load"
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -7,9 +7,9 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Value string
|
||||
SubConfig *SubConfig
|
||||
Config *Config
|
||||
Value string
|
||||
}
|
||||
|
||||
type SubConfig struct {
|
||||
|
@ -17,7 +17,6 @@ func TestFromError(t *testing.T) {
|
||||
if merr.Id != "go.micro.test" || merr.Code != 404 {
|
||||
t.Fatalf("invalid conversation %v != %v", err, merr)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
@ -32,7 +31,6 @@ func TestEqual(t *testing.T) {
|
||||
if Equal(err1, err3) {
|
||||
t.Fatal("errors must be not equal")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
|
@ -44,10 +44,10 @@ func TestDag(t *testing.T) {
|
||||
var steps [][]string
|
||||
fn := func(n dag.Vertex, idx int) error {
|
||||
if idx == 0 {
|
||||
steps = make([][]string, 1, 1)
|
||||
steps = make([][]string, 1)
|
||||
steps[0] = make([]string, 0, 1)
|
||||
} else if idx >= len(steps) {
|
||||
tsteps := make([][]string, idx+1, idx+1)
|
||||
tsteps := make([][]string, idx+1)
|
||||
copy(tsteps, steps)
|
||||
steps = tsteps
|
||||
steps[idx] = make([]string, 0, 1)
|
||||
|
14
flow/flow.go
14
flow/flow.go
@ -1,7 +1,17 @@
|
||||
// Package flow is an interface used for saga pattern messaging
|
||||
// Package flow is an interface used for saga pattern microservice workflow
|
||||
package flow
|
||||
|
||||
type Step interface {
|
||||
// Endpoint returns service_name.service_method
|
||||
// Endpoint returns rpc endpoint service_name.service_method or broker topic
|
||||
Endpoint() string
|
||||
}
|
||||
|
||||
type Workflow interface {
|
||||
Steps() [][]Step
|
||||
Stop() error
|
||||
}
|
||||
|
||||
type Flow interface {
|
||||
Start(Workflow) error
|
||||
Stop(Workflow)
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ func WithContext(ctx context.Context) Option {
|
||||
}
|
||||
|
||||
// WithName sets the name
|
||||
func withName(n string) Option {
|
||||
func WithName(n string) Option {
|
||||
return func(o *Options) {
|
||||
o.Name = n
|
||||
}
|
||||
|
@ -70,12 +70,14 @@ type lWrapper struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
type ClientCallObserver func(context.Context, client.Request, interface{}, []client.CallOption, error) []string
|
||||
type ClientStreamObserver func(context.Context, client.Request, []client.CallOption, client.Stream, error) []string
|
||||
type ClientPublishObserver func(context.Context, client.Message, []client.PublishOption, error) []string
|
||||
type ClientCallFuncObserver func(context.Context, string, client.Request, interface{}, client.CallOptions, error) []string
|
||||
type ServerHandlerObserver func(context.Context, server.Request, interface{}, error) []string
|
||||
type ServerSubscriberObserver func(context.Context, server.Message, error) []string
|
||||
type (
|
||||
ClientCallObserver func(context.Context, client.Request, interface{}, []client.CallOption, error) []string
|
||||
ClientStreamObserver func(context.Context, client.Request, []client.CallOption, client.Stream, error) []string
|
||||
ClientPublishObserver func(context.Context, client.Message, []client.PublishOption, error) []string
|
||||
ClientCallFuncObserver func(context.Context, string, client.Request, interface{}, client.CallOptions, error) []string
|
||||
ServerHandlerObserver func(context.Context, server.Request, interface{}, error) []string
|
||||
ServerSubscriberObserver func(context.Context, server.Message, error) []string
|
||||
)
|
||||
|
||||
// Options struct for wrapper
|
||||
type Options struct {
|
||||
@ -213,7 +215,7 @@ func (l *lWrapper) Call(ctx context.Context, req client.Request, rsp interface{}
|
||||
for _, o := range l.opts.ClientCallObservers {
|
||||
labels = append(labels, o(ctx, req, rsp, opts, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
@ -240,7 +242,7 @@ func (l *lWrapper) Stream(ctx context.Context, req client.Request, opts ...clien
|
||||
for _, o := range l.opts.ClientStreamObservers {
|
||||
labels = append(labels, o(ctx, req, opts, stream, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
@ -267,7 +269,7 @@ func (l *lWrapper) Publish(ctx context.Context, msg client.Message, opts ...clie
|
||||
for _, o := range l.opts.ClientPublishObservers {
|
||||
labels = append(labels, o(ctx, msg, opts, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
@ -294,7 +296,7 @@ func (l *lWrapper) ServerHandler(ctx context.Context, req server.Request, rsp in
|
||||
for _, o := range l.opts.ServerHandlerObservers {
|
||||
labels = append(labels, o(ctx, req, rsp, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
@ -321,7 +323,7 @@ func (l *lWrapper) ServerSubscriber(ctx context.Context, msg server.Message) err
|
||||
for _, o := range l.opts.ServerSubscriberObservers {
|
||||
labels = append(labels, o(ctx, msg, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
@ -372,7 +374,7 @@ func (l *lWrapper) ClientCallFunc(ctx context.Context, addr string, req client.R
|
||||
for _, o := range l.opts.ClientCallFuncObservers {
|
||||
labels = append(labels, o(ctx, addr, req, rsp, opts, err)...)
|
||||
}
|
||||
fields := make(map[string]interface{}, int(len(labels)/2))
|
||||
fields := make(map[string]interface{}, len(labels)/2)
|
||||
for i := 0; i < len(labels); i += 2 {
|
||||
fields[labels[i]] = labels[i+1]
|
||||
}
|
||||
|
@ -5,9 +5,11 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type mdIncomingKey struct{}
|
||||
type mdOutgoingKey struct{}
|
||||
type mdKey struct{}
|
||||
type (
|
||||
mdIncomingKey struct{}
|
||||
mdOutgoingKey struct{}
|
||||
mdKey struct{}
|
||||
)
|
||||
|
||||
// FromIncomingContext returns metadata from incoming ctx
|
||||
// returned metadata shoud not be modified or race condition happens
|
||||
|
@ -6,10 +6,8 @@ import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
var (
|
||||
// HeaderPrefix for all headers passed
|
||||
HeaderPrefix = "Micro-"
|
||||
)
|
||||
// HeaderPrefix for all headers passed
|
||||
var HeaderPrefix = "Micro-"
|
||||
|
||||
// Metadata is our way of representing request headers internally.
|
||||
// They're used at the RPC level and translate back and forth
|
||||
@ -20,10 +18,8 @@ type rawMetadata struct {
|
||||
md Metadata
|
||||
}
|
||||
|
||||
var (
|
||||
// defaultMetadataSize used when need to init new Metadata
|
||||
defaultMetadataSize = 2
|
||||
)
|
||||
// defaultMetadataSize used when need to init new Metadata
|
||||
var defaultMetadataSize = 2
|
||||
|
||||
// Iterator used to iterate over metadata with order
|
||||
type Iterator struct {
|
||||
|
@ -76,7 +76,7 @@ func TestIterator(t *testing.T) {
|
||||
var k, v string
|
||||
|
||||
for iter.Next(&k, &v) {
|
||||
//fmt.Printf("k: %s, v: %s\n", k, v)
|
||||
// fmt.Printf("k: %s, v: %s\n", k, v)
|
||||
}
|
||||
}
|
||||
|
||||
@ -102,7 +102,6 @@ func TestMedataCanonicalKey(t *testing.T) {
|
||||
} else if v != "12345" {
|
||||
t.Fatalf("invalid metadata value: %s != %s", "12345", v)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMetadataSet(t *testing.T) {
|
||||
@ -130,7 +129,6 @@ func TestMetadataDelete(t *testing.T) {
|
||||
if ok {
|
||||
t.Fatal("key Baz not deleted")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestNilContext(t *testing.T) {
|
||||
|
@ -9,12 +9,10 @@ import (
|
||||
"github.com/unistack-org/micro/v3/meter"
|
||||
)
|
||||
|
||||
var (
|
||||
// guard to fail early
|
||||
_ MeterServer = &handler{}
|
||||
)
|
||||
// guard to fail early
|
||||
var _ MeterServer = &Handler{}
|
||||
|
||||
type handler struct {
|
||||
type Handler struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
@ -52,12 +50,12 @@ func NewOptions(opts ...Option) Options {
|
||||
return options
|
||||
}
|
||||
|
||||
func NewHandler(opts ...Option) *handler {
|
||||
func NewHandler(opts ...Option) *Handler {
|
||||
options := NewOptions(opts...)
|
||||
return &handler{opts: options}
|
||||
return &Handler{opts: options}
|
||||
}
|
||||
|
||||
func (h *handler) Metrics(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
func (h *Handler) Metrics(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
if err := h.opts.Meter.Write(buf, h.opts.MeterOptions...); err != nil {
|
||||
return errors.InternalServerError(h.opts.Name, "%v", err)
|
||||
|
@ -87,11 +87,9 @@ type noopCounter struct {
|
||||
}
|
||||
|
||||
func (r *noopCounter) Add(int) {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopCounter) Dec() {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopCounter) Get() uint64 {
|
||||
@ -99,11 +97,9 @@ func (r *noopCounter) Get() uint64 {
|
||||
}
|
||||
|
||||
func (r *noopCounter) Inc() {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopCounter) Set(uint64) {
|
||||
|
||||
}
|
||||
|
||||
type noopFloatCounter struct {
|
||||
@ -111,7 +107,6 @@ type noopFloatCounter struct {
|
||||
}
|
||||
|
||||
func (r *noopFloatCounter) Add(float64) {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopFloatCounter) Get() float64 {
|
||||
@ -119,11 +114,9 @@ func (r *noopFloatCounter) Get() float64 {
|
||||
}
|
||||
|
||||
func (r *noopFloatCounter) Set(float64) {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopFloatCounter) Sub(float64) {
|
||||
|
||||
}
|
||||
|
||||
type noopGauge struct {
|
||||
@ -139,11 +132,9 @@ type noopSummary struct {
|
||||
}
|
||||
|
||||
func (r *noopSummary) Update(float64) {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopSummary) UpdateDuration(time.Time) {
|
||||
|
||||
}
|
||||
|
||||
type noopHistogram struct {
|
||||
@ -151,15 +142,12 @@ type noopHistogram struct {
|
||||
}
|
||||
|
||||
func (r *noopHistogram) Reset() {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopHistogram) Update(float64) {
|
||||
|
||||
}
|
||||
|
||||
func (r *noopHistogram) UpdateDuration(time.Time) {
|
||||
|
||||
}
|
||||
|
||||
//func (r *noopHistogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {}
|
||||
// func (r *noopHistogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {}
|
||||
|
@ -123,8 +123,8 @@ func (w *wrapper) CallFunc(ctx context.Context, addr string, req client.Request,
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
@ -151,8 +151,8 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
@ -179,8 +179,8 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
@ -202,8 +202,8 @@ func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
@ -238,8 +238,8 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(ServerRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(ServerRequestDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
@ -270,8 +270,8 @@ func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc
|
||||
labels := make([]string, 0, 4)
|
||||
labels = append(labels, labelEndpoint, endpoint)
|
||||
|
||||
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
|
||||
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, labels...).Update(te.Seconds())
|
||||
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, labels...).Update(te.Seconds())
|
||||
|
||||
if err == nil {
|
||||
labels = append(labels, labelStatus, labelSuccess)
|
||||
|
@ -27,9 +27,9 @@ func TestMemoryTransport(t *testing.T) {
|
||||
if len(os.Getenv("INTEGRATION_TESTS")) == 0 {
|
||||
t.Logf("Server Received %s", string(m.Body))
|
||||
}
|
||||
if err := sock.Send(&Message{
|
||||
if cerr := sock.Send(&Message{
|
||||
Body: []byte(`pong`),
|
||||
}); err != nil {
|
||||
}); cerr != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,6 @@ func TestMemoryTransport(t *testing.T) {
|
||||
t.Logf("Client Received %s", string(m.Body))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestListener(t *testing.T) {
|
||||
|
@ -30,8 +30,10 @@ type tunEvent struct {
|
||||
}
|
||||
|
||||
// used to access tunnel from options context
|
||||
type tunnelKey struct{}
|
||||
type tunnelAddr struct{}
|
||||
type (
|
||||
tunnelKey struct{}
|
||||
tunnelAddr struct{}
|
||||
)
|
||||
|
||||
func (t *tunBroker) Init(opts ...broker.Option) error {
|
||||
for _, o := range opts {
|
||||
|
@ -34,8 +34,8 @@ type Options struct {
|
||||
Token string
|
||||
// Name holds the tunnel name
|
||||
Name string
|
||||
// Id holds the tunnel id
|
||||
Id string
|
||||
// ID holds the tunnel id
|
||||
ID string
|
||||
// Address holds the tunnel address
|
||||
Address string
|
||||
// Nodes holds the tunnel nodes
|
||||
@ -68,10 +68,10 @@ type ListenOptions struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// Id sets the tunnel id
|
||||
func Id(id string) Option {
|
||||
// ID sets the tunnel id
|
||||
func ID(id string) Option {
|
||||
return func(o *Options) {
|
||||
o.Id = id
|
||||
o.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,7 +164,7 @@ func DialWait(b bool) DialOption {
|
||||
// NewOptions returns router default options with filled values
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
Id: uuid.New().String(),
|
||||
ID: uuid.New().String(),
|
||||
Address: DefaultAddress,
|
||||
Token: DefaultToken,
|
||||
Logger: logger.DefaultLogger,
|
||||
|
@ -87,7 +87,7 @@ func NewTransport(opts ...transport.Option) transport.Transport {
|
||||
}
|
||||
|
||||
// initialise
|
||||
//t.Init(opts...)
|
||||
// t.Init(opts...)
|
||||
|
||||
return t
|
||||
}
|
||||
|
@ -9,10 +9,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/network/transport"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultTunnel contains default tunnel implementation
|
||||
DefaultTunnel Tunnel
|
||||
)
|
||||
// DefaultTunnel contains default tunnel implementation
|
||||
var DefaultTunnel Tunnel
|
||||
|
||||
const (
|
||||
// Unicast send over one link
|
||||
|
@ -17,8 +17,6 @@ import (
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
"github.com/unistack-org/micro/v3/store"
|
||||
"github.com/unistack-org/micro/v3/tracer"
|
||||
// "github.com/unistack-org/micro/v3/profiler"
|
||||
// "github.com/unistack-org/micro/v3/runtime"
|
||||
)
|
||||
|
||||
// Options for micro service
|
||||
@ -78,8 +76,8 @@ func NewOptions(opts ...Option) Options {
|
||||
Meters: []meter.Meter{meter.DefaultMeter},
|
||||
Configs: []config.Config{config.DefaultConfig},
|
||||
Stores: []store.Store{store.DefaultStore},
|
||||
//Runtime runtime.Runtime
|
||||
//Profile profile.Profile
|
||||
// Runtime runtime.Runtime
|
||||
// Profile profile.Profile
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
|
@ -16,10 +16,8 @@ type httpProfile struct {
|
||||
running bool
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultAddress for http profiler
|
||||
DefaultAddress = ":6060"
|
||||
)
|
||||
// DefaultAddress for http profiler
|
||||
var DefaultAddress = ":6060"
|
||||
|
||||
// Start the profiler
|
||||
func (h *httpProfile) Start() error {
|
||||
|
@ -11,10 +11,8 @@ type Profiler interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
var (
|
||||
// DefaultProfiler holds the default profiler
|
||||
DefaultProfiler Profiler = NewProfiler()
|
||||
)
|
||||
// DefaultProfiler holds the default profiler
|
||||
var DefaultProfiler Profiler = NewProfiler()
|
||||
|
||||
// Options holds the options for profiler
|
||||
type Options struct {
|
||||
|
@ -7,10 +7,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultEndpoint holds default proxy address
|
||||
DefaultEndpoint = "localhost:9090"
|
||||
)
|
||||
// DefaultEndpoint holds default proxy address
|
||||
var DefaultEndpoint = "localhost:9090"
|
||||
|
||||
// Proxy can be used as a proxy server for micro services
|
||||
type Proxy interface {
|
||||
|
@ -64,7 +64,7 @@ func (m *memory) ttlPrune() {
|
||||
for id, n := range record.Nodes {
|
||||
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debugf(m.opts.Context, "Register TTL expired for node %s of service %s", n.Id, service)
|
||||
m.opts.Logger.Debugf(m.opts.Context, "Register TTL expired for node %s of service %s", n.ID, service)
|
||||
}
|
||||
delete(m.records[domain][service][version].Nodes, id)
|
||||
}
|
||||
@ -161,7 +161,7 @@ func (m *memory) Register(ctx context.Context, s *Service, opts ...RegisterOptio
|
||||
|
||||
for _, n := range s.Nodes {
|
||||
// check if already exists
|
||||
if _, ok := srvs[s.Name][s.Version].Nodes[n.Id]; ok {
|
||||
if _, ok := srvs[s.Name][s.Version].Nodes[n.ID]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
@ -176,9 +176,9 @@ func (m *memory) Register(ctx context.Context, s *Service, opts ...RegisterOptio
|
||||
metadata["domain"] = options.Domain
|
||||
|
||||
// add the node
|
||||
srvs[s.Name][s.Version].Nodes[n.Id] = &node{
|
||||
srvs[s.Name][s.Version].Nodes[n.ID] = &node{
|
||||
Node: &Node{
|
||||
Id: n.Id,
|
||||
ID: n.ID,
|
||||
Address: n.Address,
|
||||
Metadata: metadata,
|
||||
},
|
||||
@ -200,8 +200,8 @@ func (m *memory) Register(ctx context.Context, s *Service, opts ...RegisterOptio
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debugf(m.opts.Context, "Updated registration for service: %s, version: %s", s.Name, s.Version)
|
||||
}
|
||||
srvs[s.Name][s.Version].Nodes[n.Id].TTL = options.TTL
|
||||
srvs[s.Name][s.Version].Nodes[n.Id].LastSeen = time.Now()
|
||||
srvs[s.Name][s.Version].Nodes[n.ID].TTL = options.TTL
|
||||
srvs[s.Name][s.Version].Nodes[n.ID].LastSeen = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
@ -241,11 +241,11 @@ func (m *memory) Deregister(ctx context.Context, s *Service, opts ...DeregisterO
|
||||
|
||||
// deregister all of the service nodes from this version
|
||||
for _, n := range s.Nodes {
|
||||
if _, ok := version.Nodes[n.Id]; ok {
|
||||
if _, ok := version.Nodes[n.ID]; ok {
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debugf(m.opts.Context, "Register removed node from service: %s, version: %s", s.Name, s.Version)
|
||||
}
|
||||
delete(version.Nodes, n.Id)
|
||||
delete(version.Nodes, n.ID)
|
||||
}
|
||||
}
|
||||
|
||||
@ -457,7 +457,7 @@ func serviceToRecord(s *Service, ttl time.Duration) *record {
|
||||
|
||||
nodes := make(map[string]*node, len(s.Nodes))
|
||||
for _, n := range s.Nodes {
|
||||
nodes[n.Id] = &node{
|
||||
nodes[n.ID] = &node{
|
||||
Node: n,
|
||||
TTL: ttl,
|
||||
LastSeen: time.Now(),
|
||||
@ -489,31 +489,31 @@ func recordToService(r *record, domain string) *Service {
|
||||
|
||||
endpoints := make([]*Endpoint, len(r.Endpoints))
|
||||
for i, e := range r.Endpoints {
|
||||
metadata := make(map[string]string, len(e.Metadata))
|
||||
md := make(map[string]string, len(e.Metadata))
|
||||
for k, v := range e.Metadata {
|
||||
metadata[k] = v
|
||||
md[k] = v
|
||||
}
|
||||
|
||||
endpoints[i] = &Endpoint{
|
||||
Name: e.Name,
|
||||
Request: e.Request,
|
||||
Response: e.Response,
|
||||
Metadata: metadata,
|
||||
Metadata: md,
|
||||
}
|
||||
}
|
||||
|
||||
nodes := make([]*Node, len(r.Nodes))
|
||||
i := 0
|
||||
for _, n := range r.Nodes {
|
||||
metadata := make(map[string]string, len(n.Metadata))
|
||||
md := make(map[string]string, len(n.Metadata))
|
||||
for k, v := range n.Metadata {
|
||||
metadata[k] = v
|
||||
md[k] = v
|
||||
}
|
||||
|
||||
nodes[i] = &Node{
|
||||
Id: n.Id,
|
||||
ID: n.ID,
|
||||
Address: n.Address,
|
||||
Metadata: metadata,
|
||||
Metadata: md,
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
@ -8,72 +8,70 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
testData = map[string][]*Service{
|
||||
"foo": {
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
Id: "foo-1.0.0-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
{
|
||||
Id: "foo-1.0.0-321",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
var testData = map[string][]*Service{
|
||||
"foo": {
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.0",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
ID: "foo-1.0.0-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.1",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
Id: "foo-1.0.1-321",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.3",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
Id: "foo-1.0.3-345",
|
||||
Address: "localhost:8888",
|
||||
},
|
||||
{
|
||||
ID: "foo-1.0.0-321",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
},
|
||||
},
|
||||
"bar": {
|
||||
{
|
||||
Name: "bar",
|
||||
Version: "default",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
Id: "bar-1.0.0-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
{
|
||||
Id: "bar-1.0.0-321",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
Version: "latest",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
Id: "bar-1.0.1-321",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.1",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
ID: "foo-1.0.1-321",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
{
|
||||
Name: "foo",
|
||||
Version: "1.0.3",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
ID: "foo-1.0.3-345",
|
||||
Address: "localhost:8888",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"bar": {
|
||||
{
|
||||
Name: "bar",
|
||||
Version: "default",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
ID: "bar-1.0.0-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
{
|
||||
ID: "bar-1.0.0-321",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "bar",
|
||||
Version: "latest",
|
||||
Nodes: []*Node{
|
||||
{
|
||||
ID: "bar-1.0.1-321",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
//nolint:gocyclo
|
||||
func TestMemoryRegistry(t *testing.T) {
|
||||
|
@ -44,6 +44,7 @@ func NewOptions(opts ...Option) Options {
|
||||
return options
|
||||
}
|
||||
|
||||
// nolint: golint
|
||||
// RegisterOptions holds options for register method
|
||||
type RegisterOptions struct {
|
||||
Context context.Context
|
||||
@ -196,6 +197,7 @@ func TLSConfig(t *tls.Config) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint: golint
|
||||
// RegisterAttempts specifies register atempts count
|
||||
func RegisterAttempts(t int) RegisterOption {
|
||||
return func(o *RegisterOptions) {
|
||||
@ -203,6 +205,7 @@ func RegisterAttempts(t int) RegisterOption {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint: golint
|
||||
// RegisterTTL specifies register ttl
|
||||
func RegisterTTL(t time.Duration) RegisterOption {
|
||||
return func(o *RegisterOptions) {
|
||||
@ -210,6 +213,7 @@ func RegisterTTL(t time.Duration) RegisterOption {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint: golint
|
||||
// RegisterContext sets the register context
|
||||
func RegisterContext(ctx context.Context) RegisterOption {
|
||||
return func(o *RegisterOptions) {
|
||||
@ -217,6 +221,7 @@ func RegisterContext(ctx context.Context) RegisterOption {
|
||||
}
|
||||
}
|
||||
|
||||
// nolint: golint
|
||||
// RegisterDomain secifies register domain
|
||||
func RegisterDomain(d string) RegisterOption {
|
||||
return func(o *RegisterOptions) {
|
||||
|
@ -53,7 +53,7 @@ type Service struct {
|
||||
// Node holds node register info
|
||||
type Node struct {
|
||||
Metadata metadata.Metadata `json:"metadata"`
|
||||
Id string `json:"id"`
|
||||
ID string `json:"id"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
@ -69,6 +69,7 @@ type Endpoint struct {
|
||||
type Option func(*Options)
|
||||
|
||||
// RegisterOption option is used to register service
|
||||
// nolint: golint
|
||||
type RegisterOption func(*RegisterOptions)
|
||||
|
||||
// WatchOption option is used to watch service changes
|
||||
|
@ -52,8 +52,8 @@ type Event struct {
|
||||
Timestamp time.Time
|
||||
// Service is register service
|
||||
Service *Service
|
||||
// Id is register id
|
||||
Id string
|
||||
// ID is register id
|
||||
ID string
|
||||
// Type defines type of event
|
||||
Type EventType
|
||||
}
|
||||
|
@ -5,10 +5,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
||||
ErrWatcherStopped = errors.New("watcher stopped")
|
||||
)
|
||||
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
||||
var ErrWatcherStopped = errors.New("watcher stopped")
|
||||
|
||||
// EventType defines routing table event
|
||||
type EventType int
|
||||
|
@ -8,10 +8,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrAlreadyExists error
|
||||
ErrAlreadyExists = errors.New("already exists")
|
||||
)
|
||||
// ErrAlreadyExists error
|
||||
var ErrAlreadyExists = errors.New("already exists")
|
||||
|
||||
// Runtime is a service runtime manager
|
||||
type Runtime interface {
|
||||
|
@ -5,10 +5,8 @@ import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoneAvailable is returned by select when no routes were provided to select from
|
||||
ErrNoneAvailable = errors.New("none available")
|
||||
)
|
||||
// ErrNoneAvailable is returned by select when no routes were provided to select from
|
||||
var ErrNoneAvailable = errors.New("none available")
|
||||
|
||||
// Selector selects a route from a pool
|
||||
type Selector interface {
|
||||
|
@ -13,7 +13,7 @@ type rpcHandler struct {
|
||||
endpoints []*register.Endpoint
|
||||
}
|
||||
|
||||
func newRpcHandler(handler interface{}, opts ...HandlerOption) Handler {
|
||||
func newRPCHandler(handler interface{}, opts ...HandlerOption) Handler {
|
||||
options := NewHandlerOptions(opts...)
|
||||
|
||||
typ := reflect.TypeOf(handler)
|
||||
|
@ -5,17 +5,12 @@ import (
|
||||
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
"github.com/unistack-org/micro/v3/errors"
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
// guard to fail early
|
||||
_ HealthServer = &handler{}
|
||||
)
|
||||
var _ HealthServer = &Handler{}
|
||||
|
||||
type handler struct {
|
||||
server server.Server
|
||||
opts Options
|
||||
type Handler struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
type CheckFunc func(context.Context) error
|
||||
@ -53,15 +48,15 @@ func Version(version string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
func NewHandler(opts ...Option) *handler {
|
||||
func NewHandler(opts ...Option) *Handler {
|
||||
options := Options{}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
return &handler{opts: options}
|
||||
return &Handler{opts: options}
|
||||
}
|
||||
|
||||
func (h *handler) Live(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
func (h *Handler) Live(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
var err error
|
||||
for _, fn := range h.opts.LiveChecks {
|
||||
if err = fn(ctx); err != nil {
|
||||
@ -71,7 +66,7 @@ func (h *handler) Live(ctx context.Context, req *codec.Frame, rsp *codec.Frame)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *handler) Ready(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
func (h *Handler) Ready(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
var err error
|
||||
for _, fn := range h.opts.ReadyChecks {
|
||||
if err = fn(ctx); err != nil {
|
||||
@ -81,7 +76,7 @@ func (h *handler) Ready(ctx context.Context, req *codec.Frame, rsp *codec.Frame)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *handler) Version(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
func (h *Handler) Version(ctx context.Context, req *codec.Frame, rsp *codec.Frame) error {
|
||||
rsp.Data = []byte(h.opts.Version)
|
||||
return nil
|
||||
}
|
||||
|
@ -6,9 +6,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
// cjson "github.com/unistack-org/micro-codec-json"
|
||||
// cjsonrpc "github.com/unistack-org/micro-codec-jsonrpc"
|
||||
// cproto "github.com/unistack-org/micro-codec-proto"
|
||||
// cprotorpc "github.com/unistack-org/micro-codec-protorpc"
|
||||
"github.com/unistack-org/micro/v3/broker"
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
@ -16,16 +13,10 @@ import (
|
||||
"github.com/unistack-org/micro/v3/register"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultCodecs will be used to encode/decode
|
||||
DefaultCodecs = map[string]codec.Codec{
|
||||
//"application/json": cjson.NewCodec,
|
||||
//"application/json-rpc": cjsonrpc.NewCodec,
|
||||
//"application/protobuf": cproto.NewCodec,
|
||||
//"application/proto-rpc": cprotorpc.NewCodec,
|
||||
"application/octet-stream": codec.NewCodec(),
|
||||
}
|
||||
)
|
||||
// DefaultCodecs will be used to encode/decode
|
||||
var DefaultCodecs = map[string]codec.Codec{
|
||||
"application/octet-stream": codec.NewCodec(),
|
||||
}
|
||||
|
||||
const (
|
||||
defaultContentType = "application/json"
|
||||
@ -103,7 +94,7 @@ func (n *noopServer) Subscribe(sb Subscriber) error {
|
||||
}
|
||||
|
||||
func (n *noopServer) NewHandler(h interface{}, opts ...HandlerOption) Handler {
|
||||
return newRpcHandler(h, opts...)
|
||||
return newRPCHandler(h, opts...)
|
||||
}
|
||||
|
||||
func (n *noopServer) NewSubscriber(topic string, sb interface{}, opts ...SubscriberOption) Subscriber {
|
||||
@ -158,15 +149,14 @@ func (n *noopServer) Register() error {
|
||||
}
|
||||
|
||||
n.RLock()
|
||||
// Maps are ordered randomly, sort the keys for consistency
|
||||
var handlerList []string
|
||||
for n, _ := range n.handlers {
|
||||
handlerList := make([]string, 0, len(n.handlers))
|
||||
for n := range n.handlers {
|
||||
handlerList = append(handlerList, n)
|
||||
}
|
||||
|
||||
sort.Strings(handlerList)
|
||||
|
||||
var subscriberList []*subscriber
|
||||
subscriberList := make([]*subscriber, 0, len(n.subscribers))
|
||||
for e := range n.subscribers {
|
||||
subscriberList = append(subscriberList, e)
|
||||
}
|
||||
@ -184,7 +174,7 @@ func (n *noopServer) Register() error {
|
||||
n.RUnlock()
|
||||
|
||||
service.Nodes[0].Metadata["protocol"] = "noop"
|
||||
service.Nodes[0].Metadata["transport"] = "noop"
|
||||
service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"]
|
||||
service.Endpoints = endpoints
|
||||
|
||||
n.RLock()
|
||||
@ -193,7 +183,7 @@ func (n *noopServer) Register() error {
|
||||
|
||||
if !registered {
|
||||
if config.Logger.V(logger.InfoLevel) {
|
||||
config.Logger.Infof(n.opts.Context, "register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].Id)
|
||||
config.Logger.Infof(n.opts.Context, "register [%s] Registering node: %s", config.Register.String(), service.Nodes[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,7 +246,7 @@ func (n *noopServer) Deregister() error {
|
||||
}
|
||||
|
||||
if config.Logger.V(logger.InfoLevel) {
|
||||
config.Logger.Infof(n.opts.Context, "deregistering node: %s", service.Nodes[0].Id)
|
||||
config.Logger.Infof(n.opts.Context, "deregistering node: %s", service.Nodes[0].ID)
|
||||
}
|
||||
|
||||
if err := DefaultDeregisterFunc(service, config); err != nil {
|
||||
@ -338,9 +328,10 @@ func (n *noopServer) Start() error {
|
||||
}
|
||||
|
||||
// use RegisterCheck func before register
|
||||
// nolint: nestif
|
||||
if err := config.RegisterCheck(config.Context); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", config.Name, config.Id, err)
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", config.Name, config.ID, err)
|
||||
}
|
||||
} else {
|
||||
// announce self to the world
|
||||
@ -372,25 +363,26 @@ func (n *noopServer) Start() error {
|
||||
registered := n.registered
|
||||
n.RUnlock()
|
||||
rerr := config.RegisterCheck(config.Context)
|
||||
// nolint: nestif
|
||||
if rerr != nil && registered {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s, deregister it", config.Name, config.Id, rerr)
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s, deregister it", config.Name, config.ID, rerr)
|
||||
}
|
||||
// deregister self in case of error
|
||||
if err := n.Deregister(); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s deregister error: %s", config.Name, config.Id, err)
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s deregister error: %s", config.Name, config.ID, err)
|
||||
}
|
||||
}
|
||||
} else if rerr != nil && !registered {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", config.Name, config.Id, rerr)
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register check error: %s", config.Name, config.ID, rerr)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := n.Register(); err != nil {
|
||||
if config.Logger.V(logger.ErrorLevel) {
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register error: %s", config.Name, config.Id, err)
|
||||
config.Logger.Errorf(n.opts.Context, "server %s-%s register error: %s", config.Name, config.ID, err)
|
||||
}
|
||||
}
|
||||
// wait for exit
|
||||
|
@ -57,8 +57,8 @@ type Options struct {
|
||||
RegisterCheck func(context.Context) error
|
||||
// Codecs map to handle content-type
|
||||
Codecs map[string]codec.Codec
|
||||
// Id holds the id of the server
|
||||
Id string
|
||||
// ID holds the id of the server
|
||||
ID string
|
||||
// Namespace for te server
|
||||
Namespace string
|
||||
// Name holds the server name
|
||||
@ -104,7 +104,7 @@ func NewOptions(opts ...Option) Options {
|
||||
Address: DefaultAddress,
|
||||
Name: DefaultName,
|
||||
Version: DefaultVersion,
|
||||
Id: DefaultId,
|
||||
ID: DefaultID,
|
||||
Namespace: DefaultNamespace,
|
||||
}
|
||||
|
||||
@ -143,10 +143,10 @@ func Meter(m meter.Meter) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Id unique server id
|
||||
func Id(id string) Option {
|
||||
// ID unique server id
|
||||
func ID(id string) Option {
|
||||
return func(o *Options) {
|
||||
o.Id = id
|
||||
o.ID = id
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ func NewRegisterService(s Server) (*register.Service, error) {
|
||||
}
|
||||
|
||||
node := ®ister.Node{
|
||||
Id: opts.Name + "-" + opts.Id,
|
||||
ID: opts.Name + "-" + opts.ID,
|
||||
Address: net.JoinHostPort(addr, port),
|
||||
}
|
||||
node.Metadata = metadata.Copy(opts.Metadata)
|
||||
|
@ -11,10 +11,8 @@ import (
|
||||
"github.com/unistack-org/micro/v3/register"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultServer default server
|
||||
DefaultServer Server = NewServer()
|
||||
)
|
||||
// DefaultServer default server
|
||||
var DefaultServer Server = NewServer()
|
||||
|
||||
var (
|
||||
// DefaultAddress will be used if no address passed
|
||||
@ -23,8 +21,8 @@ var (
|
||||
DefaultName = "server"
|
||||
// DefaultVersion will be used if no version passed
|
||||
DefaultVersion = "latest"
|
||||
// DefaultId will be used if no id passed
|
||||
DefaultId = uuid.New().String()
|
||||
// DefaultID will be used if no id passed
|
||||
DefaultID = uuid.New().String()
|
||||
// DefaultRegisterCheck holds func that run before register server
|
||||
DefaultRegisterCheck = func(context.Context) error { return nil }
|
||||
// DefaultRegisterInterval holds interval for register
|
||||
|
@ -21,11 +21,9 @@ const (
|
||||
subSig = "func(context.Context, interface{}) error"
|
||||
)
|
||||
|
||||
var (
|
||||
// Precompute the reflect type for error. Can't use error directly
|
||||
// because Typeof takes an empty interface value. This is annoying.
|
||||
typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
||||
)
|
||||
// Precompute the reflect type for error. Can't use error directly
|
||||
// because Typeof takes an empty interface value. This is annoying.
|
||||
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
|
||||
|
||||
type handler struct {
|
||||
reqType reflect.Type
|
||||
@ -64,7 +62,8 @@ func ValidateSubscriber(sub Subscriber) error {
|
||||
typ := reflect.TypeOf(sub.Subscriber())
|
||||
var argType reflect.Type
|
||||
|
||||
if typ.Kind() == reflect.Func {
|
||||
switch typ.Kind() {
|
||||
case reflect.Func:
|
||||
name := "Func"
|
||||
switch typ.NumIn() {
|
||||
case 2:
|
||||
@ -82,7 +81,7 @@ func ValidateSubscriber(sub Subscriber) error {
|
||||
if returnType := typ.Out(0); returnType != typeOfError {
|
||||
return fmt.Errorf("subscriber %v returns %v not error", name, returnType.String())
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
hdlr := reflect.ValueOf(sub.Subscriber())
|
||||
name := reflect.Indirect(hdlr).Type().Name()
|
||||
|
||||
@ -276,14 +275,14 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
|
||||
if n.wg != nil {
|
||||
defer n.wg.Done()
|
||||
}
|
||||
err := fn(ctx, &rpcMessage{
|
||||
cerr := fn(ctx, &rpcMessage{
|
||||
topic: sb.topic,
|
||||
contentType: ct,
|
||||
payload: req.Interface(),
|
||||
header: msg.Header,
|
||||
body: msg.Body,
|
||||
})
|
||||
results <- err
|
||||
results <- cerr
|
||||
}()
|
||||
}
|
||||
var errors []string
|
||||
|
@ -162,7 +162,6 @@ func (s *service) Broker(names ...string) broker.Broker {
|
||||
idx = getNameIndex(names[0], s.opts.Brokers)
|
||||
}
|
||||
return s.opts.Brokers[idx]
|
||||
|
||||
}
|
||||
|
||||
func (s *service) Tracer(names ...string) tracer.Tracer {
|
||||
|
@ -5,10 +5,8 @@ import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrLockTimeout error
|
||||
ErrLockTimeout = errors.New("lock timeout")
|
||||
)
|
||||
// ErrLockTimeout error
|
||||
var ErrLockTimeout = errors.New("lock timeout")
|
||||
|
||||
// Sync is an interface for distributed synchronization
|
||||
type Sync interface {
|
||||
|
@ -38,7 +38,6 @@ type noopSpan struct {
|
||||
}
|
||||
|
||||
func (s *noopSpan) Finish(opts ...SpanOption) {
|
||||
|
||||
}
|
||||
|
||||
func (s *noopSpan) Context() context.Context {
|
||||
@ -50,7 +49,6 @@ func (s *noopSpan) Tracer() Tracer {
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddEvent(name string, opts ...EventOption) {
|
||||
|
||||
}
|
||||
|
||||
func (s *noopSpan) SetName(name string) {
|
||||
@ -58,7 +56,6 @@ func (s *noopSpan) SetName(name string) {
|
||||
}
|
||||
|
||||
func (s *noopSpan) SetLabels(labels ...Label) {
|
||||
|
||||
}
|
||||
|
||||
// NewTracer returns new memory tracer
|
||||
|
@ -2,13 +2,11 @@ package tracer
|
||||
|
||||
import "github.com/unistack-org/micro/v3/logger"
|
||||
|
||||
type SpanOptions struct {
|
||||
}
|
||||
type SpanOptions struct{}
|
||||
|
||||
type SpanOption func(o *SpanOptions)
|
||||
|
||||
type EventOptions struct {
|
||||
}
|
||||
type EventOptions struct{}
|
||||
|
||||
type EventOption func(o *EventOptions)
|
||||
|
||||
|
@ -5,10 +5,8 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultTracer is the global default tracer
|
||||
DefaultTracer Tracer = NewTracer()
|
||||
)
|
||||
// DefaultTracer is the global default tracer
|
||||
var DefaultTracer Tracer = NewTracer()
|
||||
|
||||
// Tracer is an interface for distributed tracing
|
||||
type Tracer interface {
|
||||
|
@ -113,12 +113,14 @@ type tWrapper struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
type ClientCallObserver func(context.Context, client.Request, interface{}, []client.CallOption, tracer.Span, error)
|
||||
type ClientStreamObserver func(context.Context, client.Request, []client.CallOption, client.Stream, tracer.Span, error)
|
||||
type ClientPublishObserver func(context.Context, client.Message, []client.PublishOption, tracer.Span, error)
|
||||
type ClientCallFuncObserver func(context.Context, string, client.Request, interface{}, client.CallOptions, tracer.Span, error)
|
||||
type ServerHandlerObserver func(context.Context, server.Request, interface{}, tracer.Span, error)
|
||||
type ServerSubscriberObserver func(context.Context, server.Message, tracer.Span, error)
|
||||
type (
|
||||
ClientCallObserver func(context.Context, client.Request, interface{}, []client.CallOption, tracer.Span, error)
|
||||
ClientStreamObserver func(context.Context, client.Request, []client.CallOption, client.Stream, tracer.Span, error)
|
||||
ClientPublishObserver func(context.Context, client.Message, []client.PublishOption, tracer.Span, error)
|
||||
ClientCallFuncObserver func(context.Context, string, client.Request, interface{}, client.CallOptions, tracer.Span, error)
|
||||
ServerHandlerObserver func(context.Context, server.Request, interface{}, tracer.Span, error)
|
||||
ServerSubscriberObserver func(context.Context, server.Message, tracer.Span, error)
|
||||
)
|
||||
|
||||
// Options struct
|
||||
type Options struct {
|
||||
|
@ -5,9 +5,7 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
privateBlocks []*net.IPNet
|
||||
)
|
||||
var privateBlocks []*net.IPNet
|
||||
|
||||
func init() {
|
||||
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "fd00::/8"} {
|
||||
|
@ -54,7 +54,6 @@ func TestExtractor(t *testing.T) {
|
||||
t.Errorf("Expected %s got %s", d.expect, addr)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestAppendPrivateBlocks(t *testing.T) {
|
||||
|
@ -4,20 +4,20 @@ import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type buffer struct {
|
||||
type Buffer struct {
|
||||
*bytes.Buffer
|
||||
}
|
||||
|
||||
// Close reset buffer contents
|
||||
func (b *buffer) Close() error {
|
||||
func (b *Buffer) Close() error {
|
||||
b.Buffer.Reset()
|
||||
return nil
|
||||
}
|
||||
|
||||
// New creates new buffer that satisfies Closer interface
|
||||
func New(b *bytes.Buffer) *buffer {
|
||||
func New(b *bytes.Buffer) *Buffer {
|
||||
if b == nil {
|
||||
b = bytes.NewBuffer(nil)
|
||||
}
|
||||
return &buffer{b}
|
||||
return &Buffer{b}
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ func HostPort(addr string, port interface{}) string {
|
||||
// Listen takes addr:portmin-portmax and binds to the first available port
|
||||
// Example: Listen("localhost:5000-6000", fn)
|
||||
func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
|
||||
|
||||
if strings.Count(addr, ":") == 1 && strings.Count(addr, "-") == 0 {
|
||||
return fn(addr)
|
||||
}
|
||||
|
@ -22,5 +22,4 @@ func TestListen(t *testing.T) {
|
||||
// TODO nats case test
|
||||
// natsAddr := "_INBOX.bID2CMRvlNp0vt4tgNBHWf"
|
||||
// Expect addr DO NOT has extra ":" at the end!
|
||||
|
||||
}
|
||||
|
@ -8,9 +8,8 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// GenerateKey returns an ed25519 key
|
||||
@ -46,14 +45,14 @@ func CA(opts ...CertOption) ([]byte, []byte, error) {
|
||||
return nil, nil, err
|
||||
}
|
||||
cert, key := &bytes.Buffer{}, &bytes.Buffer{}
|
||||
if err := pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
|
||||
if err = pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
x509Key, err := x509.MarshalPKCS8PrivateKey(options.Priv)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
|
||||
if err = pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
|
@ -63,9 +63,7 @@ func Unmarshal(dst interface{}, query string) error {
|
||||
// possible. Eg the example above would output:
|
||||
// {"bar":{"one":{"two":2,"red":112}}}
|
||||
func ToJSON(query string) ([]byte, error) {
|
||||
var (
|
||||
builder interface{} = make(map[string]interface{})
|
||||
)
|
||||
var builder interface{} = make(map[string]interface{})
|
||||
params := strings.Split(query, "&")
|
||||
for _, part := range params {
|
||||
tempMap, err := queryToMap(part)
|
||||
|
@ -32,9 +32,10 @@ type unmarshalT struct {
|
||||
A string `json:"a"`
|
||||
B unmarshalB `json:"b"`
|
||||
}
|
||||
|
||||
type unmarshalB struct {
|
||||
C int `json:"c"`
|
||||
D string `json:"D"`
|
||||
C int `json:"c"`
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
|
@ -11,7 +11,7 @@ type Rand struct {
|
||||
}
|
||||
|
||||
func (r *Rand) Int31() int32 {
|
||||
rand.Read(r.buf[:4])
|
||||
_, _ = rand.Read(r.buf[:4])
|
||||
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
|
||||
}
|
||||
|
||||
@ -54,7 +54,7 @@ func (r *Rand) Intn(n int) int {
|
||||
}
|
||||
|
||||
func (r *Rand) Int63() int64 {
|
||||
rand.Read(r.buf[:])
|
||||
_, _ = rand.Read(r.buf[:])
|
||||
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
)
|
||||
|
||||
func TestPath(t *testing.T) {
|
||||
|
||||
type Nested2 struct {
|
||||
Name string
|
||||
}
|
||||
|
@ -9,14 +9,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidParam specifies invalid url query params
|
||||
ErrInvalidParam = errors.New("invalid url query param provided")
|
||||
)
|
||||
// ErrInvalidParam specifies invalid url query params
|
||||
var ErrInvalidParam = errors.New("invalid url query param provided")
|
||||
|
||||
var (
|
||||
bracketSplitter = regexp.MustCompile(`\[|\]`)
|
||||
)
|
||||
var bracketSplitter = regexp.MustCompile(`\[|\]`)
|
||||
|
||||
// StructFields returns slice of struct fields
|
||||
func StructFields(src interface{}) ([]reflect.StructField, error) {
|
||||
@ -157,7 +153,7 @@ func StructURLValues(src interface{}, pref string, tags []string) (url.Values, e
|
||||
case reflect.Slice:
|
||||
for i := 0; i < val.Len(); i++ {
|
||||
va := val.Index(i)
|
||||
//if va.Type().Elem().Kind() != reflect.Ptr {
|
||||
// if va.Type().Elem().Kind() != reflect.Ptr {
|
||||
if va.Kind() != reflect.Ptr {
|
||||
data.Set(t.name, fmt.Sprintf("%v", va.Interface()))
|
||||
continue
|
||||
@ -193,9 +189,7 @@ func StructURLValues(src interface{}, pref string, tags []string) (url.Values, e
|
||||
|
||||
// URLMap returns map of url query params
|
||||
func URLMap(query string) (map[string]interface{}, error) {
|
||||
var (
|
||||
mp interface{} = make(map[string]interface{})
|
||||
)
|
||||
var mp interface{} = make(map[string]interface{})
|
||||
|
||||
params := strings.Split(query, "&")
|
||||
|
||||
|
@ -7,9 +7,9 @@ import (
|
||||
|
||||
func TestStructURLValues(t *testing.T) {
|
||||
type Str struct {
|
||||
Str *Str `json:"str"`
|
||||
Name string `json:"name"`
|
||||
Args []int `json:"args"`
|
||||
Str *Str `json:"str"`
|
||||
}
|
||||
|
||||
val := &Str{Name: "test_name", Args: []int{1, 2, 3}, Str: &Str{Name: "nested_name"}}
|
||||
@ -90,8 +90,8 @@ func TestIsZero(t *testing.T) {
|
||||
Nested string
|
||||
}
|
||||
type testStr2 struct {
|
||||
Name string
|
||||
Nested *testStr3
|
||||
Name string
|
||||
}
|
||||
vtest := &testStr2{
|
||||
Name: "test_name",
|
||||
@ -106,5 +106,5 @@ func TestIsZero(t *testing.T) {
|
||||
t.Fatalf("non zero ret on zero struct: %#+v", vtest)
|
||||
}
|
||||
|
||||
//t.Logf("XX %#+v\n", ok)
|
||||
// t.Logf("XX %#+v\n", ok)
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ import (
|
||||
)
|
||||
|
||||
func addNodes(old, neu []*register.Node) []*register.Node {
|
||||
nodes := make([]*register.Node, len(neu))
|
||||
nodes := make([]*register.Node, 0, len(neu))
|
||||
// add all new nodes
|
||||
for i, n := range neu {
|
||||
for _, n := range neu {
|
||||
node := *n
|
||||
nodes[i] = &node
|
||||
nodes = append(nodes, &node)
|
||||
}
|
||||
|
||||
// look at old nodes
|
||||
@ -19,7 +19,7 @@ func addNodes(old, neu []*register.Node) []*register.Node {
|
||||
// check against new nodes
|
||||
for _, n := range nodes {
|
||||
// ids match then skip
|
||||
if o.Id == n.Id {
|
||||
if o.ID == n.ID {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
@ -40,7 +40,7 @@ func delNodes(old, del []*register.Node) []*register.Node {
|
||||
for _, o := range old {
|
||||
var rem bool
|
||||
for _, n := range del {
|
||||
if o.Id == n.Id {
|
||||
if o.ID == n.ID {
|
||||
rem = true
|
||||
break
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ func TestRemove(t *testing.T) {
|
||||
Version: "1.0.0",
|
||||
Nodes: []*register.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
ID: "foo-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
},
|
||||
@ -24,7 +24,7 @@ func TestRemove(t *testing.T) {
|
||||
Version: "1.0.0",
|
||||
Nodes: []*register.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
ID: "foo-123",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
@ -47,11 +47,11 @@ func TestRemoveNodes(t *testing.T) {
|
||||
Version: "1.0.0",
|
||||
Nodes: []*register.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
ID: "foo-123",
|
||||
Address: "localhost:9999",
|
||||
},
|
||||
{
|
||||
Id: "foo-321",
|
||||
ID: "foo-321",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
@ -61,7 +61,7 @@ func TestRemoveNodes(t *testing.T) {
|
||||
Version: "1.0.0",
|
||||
Nodes: []*register.Node{
|
||||
{
|
||||
Id: "foo-123",
|
||||
ID: "foo-123",
|
||||
Address: "localhost:6666",
|
||||
},
|
||||
},
|
||||
|
@ -77,7 +77,7 @@ func (t template) Compile() Template {
|
||||
rawOps = append(rawOps, s.compile()...)
|
||||
}
|
||||
|
||||
//ops := make([]int, 0, len(rawOps))
|
||||
// ops := make([]int, 0, len(rawOps))
|
||||
var (
|
||||
ops []int
|
||||
pool []string
|
||||
|
@ -50,9 +50,7 @@ func tokenize(path string) (tokens []string, verb string) {
|
||||
field
|
||||
nested
|
||||
)
|
||||
var (
|
||||
st = init
|
||||
)
|
||||
st := init
|
||||
for path != "" {
|
||||
var idx int
|
||||
switch st {
|
||||
|
@ -209,7 +209,8 @@ func TestParseSegments(t *testing.T) {
|
||||
"a", "/", "b", "/", "*", "/", "c",
|
||||
"}", "/",
|
||||
"**",
|
||||
eof},
|
||||
eof,
|
||||
},
|
||||
want: []segment{
|
||||
literal("v1"),
|
||||
variable{
|
||||
|
@ -40,7 +40,7 @@ func (c *syncStore) processQueue(index int) {
|
||||
}
|
||||
var opts []store.WriteOption
|
||||
if !ir.expiresAt.IsZero() {
|
||||
opts = append(opts, store.WriteTTL(ir.expiresAt.Sub(time.Now())))
|
||||
opts = append(opts, store.WriteTTL(time.Until(ir.expiresAt)))
|
||||
}
|
||||
// Todo = internal queue also has to hold the corresponding store.WriteOptions
|
||||
if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, ir.key, ir.value, opts...); err != nil {
|
||||
|
@ -17,10 +17,8 @@ type Basic struct {
|
||||
store store.Store
|
||||
}
|
||||
|
||||
var (
|
||||
// StorePrefix to isolate tokens
|
||||
StorePrefix = "tokens/"
|
||||
)
|
||||
// StorePrefix to isolate tokens
|
||||
var StorePrefix = "tokens/"
|
||||
|
||||
// NewTokenProvider returns an initialized basic provider
|
||||
func NewTokenProvider(opts ...token.Option) token.Provider {
|
||||
|
@ -83,5 +83,4 @@ func TestInspect(t *testing.T) {
|
||||
t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func NewOptions(opts ...Option) Options {
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
//set default store
|
||||
// set default store
|
||||
if options.Store == nil {
|
||||
options.Store = store.DefaultStore
|
||||
}
|
||||
@ -75,7 +75,7 @@ func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
//set default Expiry of token
|
||||
// set default Expiry of token
|
||||
if options.Expiry == 0 {
|
||||
options.Expiry = time.Minute * 15
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user