Compare commits
No commits in common. "v3" and "v3.11.1" have entirely different histories.
@ -1,51 +0,0 @@
|
||||
name: coverage
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main, v3, v4 ]
|
||||
pull_request:
|
||||
branches: [ main, v3, v4 ]
|
||||
# Allows you to run this workflow manually from the Actions tab
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
|
||||
- name: test coverage
|
||||
run: |
|
||||
go test -v -cover ./... -coverprofile coverage.out -coverpkg ./...
|
||||
go tool cover -func coverage.out -o coverage.out
|
||||
|
||||
- name: coverage badge
|
||||
uses: tj-actions/coverage-badge-go@v1
|
||||
with:
|
||||
green: 80
|
||||
filename: coverage.out
|
||||
|
||||
- uses: stefanzweifel/git-auto-commit-action@v4
|
||||
id: auto-commit-action
|
||||
with:
|
||||
commit_message: Apply Code Coverage Badge
|
||||
skip_fetch: true
|
||||
skip_checkout: true
|
||||
file_pattern: ./README.md
|
||||
|
||||
- name: Push Changes
|
||||
if: steps.auto-commit-action.outputs.changes_detected == 'true'
|
||||
uses: ad-m/github-push-action@master
|
||||
with:
|
||||
github_token: ${{ github.token }}
|
||||
branch: ${{ github.ref }}
|
@ -1,29 +0,0 @@
|
||||
name: lint
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run lint
|
||||
uses: https://github.com/golangci/golangci-lint-action@v6
|
||||
with:
|
||||
version: 'latest'
|
@ -1,34 +0,0 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup deps
|
||||
run: go get -v ./...
|
||||
- name: run test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
@ -1,53 +0,0 @@
|
||||
name: test
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
- v4
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
filter: 'blob:none'
|
||||
- name: checkout tests
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: master
|
||||
filter: 'blob:none'
|
||||
repository: unistack-org/micro-tests
|
||||
path: micro-tests
|
||||
- name: setup go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
cache-dependency-path: "**/*.sum"
|
||||
go-version: 'stable'
|
||||
- name: setup go work
|
||||
env:
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: |
|
||||
go work init
|
||||
go work use .
|
||||
go work use micro-tests
|
||||
- name: setup deps
|
||||
env:
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: go get -v ./...
|
||||
- name: run tests
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
GOWORK: /workspace/${{ github.repository_owner }}/go.work
|
||||
run: |
|
||||
cd micro-tests
|
||||
go test -mod readonly -v ./... || true
|
24
.gitea/workflows/lint.yml
Normal file
24
.gitea/workflows/lint.yml
Normal file
@ -0,0 +1,24 @@
|
||||
name: lint
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
lint:
|
||||
name: lint
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: setup-go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.21
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: deps
|
||||
run: go get -v -d ./...
|
||||
- name: lint
|
||||
uses: https://github.com/golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
version: v1.52
|
23
.gitea/workflows/pr.yml
Normal file
23
.gitea/workflows/pr.yml
Normal file
@ -0,0 +1,23 @@
|
||||
name: pr
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
- v3
|
||||
jobs:
|
||||
test:
|
||||
name: test
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: setup-go
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: 1.21
|
||||
- name: deps
|
||||
run: go get -v -t -d ./...
|
||||
- name: test
|
||||
env:
|
||||
INTEGRATION_TESTS: yes
|
||||
run: go test -mod readonly -v ./...
|
@ -1,5 +1,44 @@
|
||||
run:
|
||||
concurrency: 8
|
||||
concurrency: 4
|
||||
deadline: 5m
|
||||
issues-exit-code: 1
|
||||
tests: true
|
||||
|
||||
linters-settings:
|
||||
govet:
|
||||
check-shadowing: true
|
||||
enable:
|
||||
- fieldalignment
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- govet
|
||||
- deadcode
|
||||
- errcheck
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- structcheck
|
||||
- typecheck
|
||||
- unused
|
||||
- varcheck
|
||||
- bodyclose
|
||||
- gci
|
||||
- goconst
|
||||
- gocritic
|
||||
- gosimple
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- goimports
|
||||
- revive
|
||||
- gosec
|
||||
- makezero
|
||||
- misspell
|
||||
- nakedret
|
||||
- nestif
|
||||
- nilerr
|
||||
- noctx
|
||||
- prealloc
|
||||
- unconvert
|
||||
- unparam
|
||||
disable-all: false
|
||||
|
@ -1,5 +1,4 @@
|
||||
# Micro
|
||||
![Coverage](https://img.shields.io/badge/Coverage-44.7%25-yellow)
|
||||
# Micro [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Doc](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/unistack-org/micro/v3?tab=overview) [![Status](https://github.com/unistack-org/micro/workflows/build/badge.svg?branch=master)](https://github.com/unistack-org/micro/actions?query=workflow%3Abuild+branch%3Amaster+event%3Apush) [![Lint](https://goreportcard.com/badge/go.unistack.org/micro/v3)](https://goreportcard.com/report/go.unistack.org/micro/v3) [![Coverage](https://codecov.io/gh/unistack-org/micro/branch/v3/graph/badge.svg?token=OZPO2LP7VS)](https://codecov.io/gh/unistack-org/micro)
|
||||
|
||||
Micro is a standard library for microservices.
|
||||
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Broker, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext returns broker from passed context
|
||||
func MustContext(ctx context.Context) Broker {
|
||||
b, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing broker")
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// NewContext savess broker in context
|
||||
func NewContext(ctx context.Context, s Broker) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -109,7 +109,7 @@ func (m *memoryBroker) Init(opts ...broker.Option) error {
|
||||
m.funcSubscribe = m.fnSubscribe
|
||||
m.funcBatchSubscribe = m.fnBatchSubscribe
|
||||
|
||||
m.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
m.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case broker.HookPublish:
|
||||
m.funcPublish = h(m.funcPublish)
|
||||
|
@ -74,7 +74,7 @@ func TestMemoryBroker(t *testing.T) {
|
||||
topic := "test"
|
||||
count := 10
|
||||
|
||||
fn := func(_ broker.Event) error {
|
||||
fn := func(p broker.Event) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ func (b *NoopBroker) Init(opts ...Option) error {
|
||||
b.funcSubscribe = b.fnSubscribe
|
||||
b.funcBatchSubscribe = b.fnBatchSubscribe
|
||||
|
||||
b.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
b.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case HookPublish:
|
||||
b.funcPublish = h(b.funcPublish)
|
||||
|
@ -16,9 +16,6 @@ import (
|
||||
|
||||
// Options struct
|
||||
type Options struct {
|
||||
// Name holds the broker name
|
||||
Name string
|
||||
|
||||
// Tracer used for tracing
|
||||
Tracer tracer.Tracer
|
||||
// Register can be used for clustering
|
||||
@ -31,25 +28,23 @@ type Options struct {
|
||||
Meter meter.Meter
|
||||
// Context holds external options
|
||||
Context context.Context
|
||||
|
||||
// Wait waits for a collection of goroutines to finish
|
||||
Wait *sync.WaitGroup
|
||||
// TLSConfig holds tls.TLSConfig options
|
||||
TLSConfig *tls.Config
|
||||
|
||||
// ErrorHandler used when broker can't unmarshal incoming message
|
||||
ErrorHandler Handler
|
||||
// BatchErrorHandler used when broker can't unmashal incoming messages
|
||||
BatchErrorHandler BatchHandler
|
||||
|
||||
// Name holds the broker name
|
||||
Name string
|
||||
// Addrs holds the broker address
|
||||
Addrs []string
|
||||
// Wait waits for a collection of goroutines to finish
|
||||
Wait *sync.WaitGroup
|
||||
// GracefulTimeout contains time to wait to finish in flight requests
|
||||
GracefulTimeout time.Duration
|
||||
// Hooks can be run before broker Publish/BatchPublish and
|
||||
// Subscribe/BatchSubscribe methods
|
||||
Hooks options.Hooks
|
||||
|
||||
// GracefulTimeout contains time to wait to finish in flight requests
|
||||
GracefulTimeout time.Duration
|
||||
}
|
||||
|
||||
// NewOptions create new Options
|
||||
|
@ -17,13 +17,13 @@ func BackoffExp(_ context.Context, _ Request, attempts int) (time.Duration, erro
|
||||
}
|
||||
|
||||
// BackoffInterval specifies randomization interval for backoff func
|
||||
func BackoffInterval(minTime time.Duration, maxTime time.Duration) BackoffFunc {
|
||||
func BackoffInterval(min time.Duration, max time.Duration) BackoffFunc {
|
||||
return func(_ context.Context, _ Request, attempts int) (time.Duration, error) {
|
||||
td := time.Duration(math.Pow(float64(attempts), math.E)) * time.Millisecond * 100
|
||||
if td < minTime {
|
||||
return minTime, nil
|
||||
} else if td > maxTime {
|
||||
return maxTime, nil
|
||||
if td < min {
|
||||
return min, nil
|
||||
} else if td > max {
|
||||
return max, nil
|
||||
}
|
||||
return td, nil
|
||||
}
|
||||
|
@ -34,23 +34,23 @@ func TestBackoffExp(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBackoffInterval(t *testing.T) {
|
||||
minTime := 100 * time.Millisecond
|
||||
maxTime := 300 * time.Millisecond
|
||||
min := 100 * time.Millisecond
|
||||
max := 300 * time.Millisecond
|
||||
|
||||
r := &testRequest{
|
||||
service: "test",
|
||||
method: "test",
|
||||
}
|
||||
|
||||
fn := BackoffInterval(minTime, maxTime)
|
||||
fn := BackoffInterval(min, max)
|
||||
for i := 0; i < 5; i++ {
|
||||
d, err := fn(context.TODO(), r, i)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if d < minTime || d > maxTime {
|
||||
t.Fatalf("Expected %v < %v < %v", minTime, d, maxTime)
|
||||
if d < min || d > max {
|
||||
t.Fatalf("Expected %v < %v < %v", min, d, max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Client, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext get client from context
|
||||
func MustContext(ctx context.Context) Client {
|
||||
c, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing client")
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// NewContext put client in context
|
||||
func NewContext(ctx context.Context, c Client) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -194,7 +194,7 @@ func (n *noopClient) Init(opts ...Option) error {
|
||||
n.funcPublish = n.fnPublish
|
||||
n.funcBatchPublish = n.fnBatchPublish
|
||||
|
||||
n.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
n.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case HookCall:
|
||||
n.funcCall = h(n.funcCall)
|
||||
@ -222,7 +222,7 @@ func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opt
|
||||
ts := time.Now()
|
||||
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
||||
var sp tracer.Span
|
||||
ctx, sp = n.opts.Tracer.Start(ctx, "rpc-client",
|
||||
ctx, sp = n.opts.Tracer.Start(ctx, req.Endpoint()+" rpc-client",
|
||||
tracer.WithSpanKind(tracer.SpanKindClient),
|
||||
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
||||
)
|
||||
@ -298,7 +298,7 @@ func (n *noopClient) fnCall(ctx context.Context, req Request, rsp interface{}, o
|
||||
// call backoff first. Someone may want an initial start delay
|
||||
t, err := callOpts.Backoff(ctx, req, i)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", "%s", err)
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
// only sleep if greater than 0
|
||||
@ -312,7 +312,7 @@ func (n *noopClient) fnCall(ctx context.Context, req Request, rsp interface{}, o
|
||||
// TODO apply any filtering here
|
||||
routes, err = n.opts.Lookup(ctx, req, callOpts)
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", "%s", err)
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
// balance the list of nodes
|
||||
@ -372,7 +372,7 @@ func (n *noopClient) fnCall(ctx context.Context, req Request, rsp interface{}, o
|
||||
return gerr
|
||||
}
|
||||
|
||||
func (n *noopClient) NewRequest(service, endpoint string, _ interface{}, _ ...RequestOption) Request {
|
||||
func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request {
|
||||
return &noopRequest{service: service, endpoint: endpoint}
|
||||
}
|
||||
|
||||
@ -385,7 +385,7 @@ func (n *noopClient) Stream(ctx context.Context, req Request, opts ...CallOption
|
||||
ts := time.Now()
|
||||
n.opts.Meter.Counter(semconv.ClientRequestInflight, "endpoint", req.Endpoint()).Inc()
|
||||
var sp tracer.Span
|
||||
ctx, sp = n.opts.Tracer.Start(ctx, "rpc-client",
|
||||
ctx, sp = n.opts.Tracer.Start(ctx, req.Endpoint()+" rpc-client",
|
||||
tracer.WithSpanKind(tracer.SpanKindClient),
|
||||
tracer.WithSpanLabels("endpoint", req.Endpoint()),
|
||||
)
|
||||
@ -466,7 +466,7 @@ func (n *noopClient) fnStream(ctx context.Context, req Request, opts ...CallOpti
|
||||
// call backoff first. Someone may want an initial start delay
|
||||
t, cerr := callOpts.Backoff(ctx, req, i)
|
||||
if cerr != nil {
|
||||
return nil, errors.InternalServerError("go.micro.client", "%s", cerr)
|
||||
return nil, errors.InternalServerError("go.micro.client", cerr.Error())
|
||||
}
|
||||
|
||||
// only sleep if greater than 0
|
||||
@ -480,7 +480,7 @@ func (n *noopClient) fnStream(ctx context.Context, req Request, opts ...CallOpti
|
||||
// TODO apply any filtering here
|
||||
routes, err = n.opts.Lookup(ctx, req, callOpts)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServerError("go.micro.client", "%s", err)
|
||||
return nil, errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
// balance the list of nodes
|
||||
@ -546,7 +546,7 @@ func (n *noopClient) fnStream(ctx context.Context, req Request, opts ...CallOpti
|
||||
return nil, grr
|
||||
}
|
||||
|
||||
func (n *noopClient) stream(ctx context.Context, _ string, _ Request, _ CallOptions) (Stream, error) {
|
||||
func (n *noopClient) stream(ctx context.Context, addr string, req Request, opts CallOptions) (Stream, error) {
|
||||
return &noopStream{ctx: ctx}, nil
|
||||
}
|
||||
|
||||
@ -609,13 +609,13 @@ func (n *noopClient) publish(ctx context.Context, ps []Message, opts ...PublishO
|
||||
// use codec for payload
|
||||
cf, err := n.newCodec(p.ContentType())
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", "%s", err)
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
|
||||
// set the body
|
||||
b, err := cf.Marshal(p.Payload())
|
||||
if err != nil {
|
||||
return errors.InternalServerError("go.micro.client", "%s", err)
|
||||
return errors.InternalServerError("go.micro.client", err.Error())
|
||||
}
|
||||
body = b
|
||||
}
|
||||
|
@ -21,16 +21,6 @@ import (
|
||||
|
||||
// Options holds client options
|
||||
type Options struct {
|
||||
// Codecs map
|
||||
Codecs map[string]codec.Codec
|
||||
|
||||
// Proxy is used for proxy requests
|
||||
Proxy string
|
||||
// ContentType is used to select codec
|
||||
ContentType string
|
||||
// Name is the client name
|
||||
Name string
|
||||
|
||||
// Selector used to select needed address
|
||||
Selector selector.Selector
|
||||
// Logger used to log messages
|
||||
@ -45,28 +35,31 @@ type Options struct {
|
||||
Context context.Context
|
||||
// Router used to get route
|
||||
Router router.Router
|
||||
|
||||
// TLSConfig specifies tls.Config for secure connection
|
||||
TLSConfig *tls.Config
|
||||
|
||||
// Codecs map
|
||||
Codecs map[string]codec.Codec
|
||||
// Lookup func used to get destination addr
|
||||
Lookup LookupFunc
|
||||
// ContextDialer used to connect
|
||||
ContextDialer func(context.Context, string) (net.Conn, error)
|
||||
|
||||
// Proxy is used for proxy requests
|
||||
Proxy string
|
||||
// ContentType is used to select codec
|
||||
ContentType string
|
||||
// Name is the client name
|
||||
Name string
|
||||
// Wrappers contains wrappers
|
||||
Wrappers []Wrapper
|
||||
// Hooks can be run before broker Publish/BatchPublish and
|
||||
// Subscribe/BatchSubscribe methods
|
||||
Hooks options.Hooks
|
||||
|
||||
// CallOptions contains default CallOptions
|
||||
CallOptions CallOptions
|
||||
|
||||
// PoolSize connection pool size
|
||||
PoolSize int
|
||||
// PoolTTL connection pool ttl
|
||||
PoolTTL time.Duration
|
||||
// ContextDialer used to connect
|
||||
ContextDialer func(context.Context, string) (net.Conn, error)
|
||||
// Hooks can be run before broker Publish/BatchPublish and
|
||||
// Subscribe/BatchSubscribe methods
|
||||
Hooks options.Hooks
|
||||
}
|
||||
|
||||
// NewCallOptions creates new call options struct
|
||||
@ -80,16 +73,6 @@ func NewCallOptions(opts ...CallOption) CallOptions {
|
||||
|
||||
// CallOptions holds client call options
|
||||
type CallOptions struct {
|
||||
// RequestMetadata holds additional metadata for call
|
||||
RequestMetadata metadata.Metadata
|
||||
|
||||
// Network name
|
||||
Network string
|
||||
// Content-Type
|
||||
ContentType string
|
||||
// AuthToken string
|
||||
AuthToken string
|
||||
|
||||
// Selector selects addr
|
||||
Selector selector.Selector
|
||||
// Context used for deadline
|
||||
@ -97,30 +80,33 @@ type CallOptions struct {
|
||||
// Router used for route
|
||||
Router router.Router
|
||||
// Retry func used for retries
|
||||
|
||||
// ResponseMetadata holds additional metadata from call
|
||||
ResponseMetadata *metadata.Metadata
|
||||
|
||||
Retry RetryFunc
|
||||
// Backoff func used for backoff when retry
|
||||
Backoff BackoffFunc
|
||||
// ContextDialer used to connect
|
||||
ContextDialer func(context.Context, string) (net.Conn, error)
|
||||
|
||||
// Network name
|
||||
Network string
|
||||
// Content-Type
|
||||
ContentType string
|
||||
// AuthToken string
|
||||
AuthToken string
|
||||
// Address specifies static addr list
|
||||
Address []string
|
||||
// SelectOptions selector options
|
||||
SelectOptions []selector.SelectOption
|
||||
|
||||
// StreamTimeout stream timeout
|
||||
StreamTimeout time.Duration
|
||||
// RequestTimeout request timeout
|
||||
RequestTimeout time.Duration
|
||||
|
||||
// RequestMetadata holds additional metadata for call
|
||||
RequestMetadata metadata.Metadata
|
||||
// ResponseMetadata holds additional metadata from call
|
||||
ResponseMetadata *metadata.Metadata
|
||||
// DialTimeout dial timeout
|
||||
DialTimeout time.Duration
|
||||
// Retries specifies retries num
|
||||
Retries int
|
||||
// ContextDialer used to connect
|
||||
ContextDialer func(context.Context, string) (net.Conn, error)
|
||||
}
|
||||
|
||||
// ContextDialer pass ContextDialer to client
|
||||
|
@ -3,8 +3,6 @@ package codec
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -56,22 +54,3 @@ func (m *RawMessage) UnmarshalJSON(data []byte) error {
|
||||
*m = append((*m)[0:0], data...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalYAML returns m as the JSON encoding of m.
|
||||
func (m *RawMessage) MarshalYAML() ([]byte, error) {
|
||||
if m == nil {
|
||||
return []byte("null"), nil
|
||||
} else if len(*m) == 0 {
|
||||
return []byte("null"), nil
|
||||
}
|
||||
return *m, nil
|
||||
}
|
||||
|
||||
// UnmarshalYAML sets *m to a copy of data.
|
||||
func (m *RawMessage) UnmarshalYAML(n *yaml.Node) error {
|
||||
if m == nil {
|
||||
return errors.New("RawMessage UnmarshalYAML on nil pointer")
|
||||
}
|
||||
*m = append((*m)[0:0], []byte(n.Value)...)
|
||||
return nil
|
||||
}
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Codec, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext returns codec from context
|
||||
func MustContext(ctx context.Context) Codec {
|
||||
c, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing codec")
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// NewContext put codec in context
|
||||
func NewContext(ctx context.Context, c Codec) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -1,7 +1,5 @@
|
||||
package codec
|
||||
|
||||
import "gopkg.in/yaml.v3"
|
||||
|
||||
// Frame gives us the ability to define raw data to send over the pipes
|
||||
type Frame struct {
|
||||
Data []byte
|
||||
@ -22,17 +20,6 @@ func (m *Frame) UnmarshalJSON(data []byte) error {
|
||||
return m.Unmarshal(data)
|
||||
}
|
||||
|
||||
// MarshalYAML returns frame data
|
||||
func (m *Frame) MarshalYAML() ([]byte, error) {
|
||||
return m.Marshal()
|
||||
}
|
||||
|
||||
// UnmarshalYAML set frame data
|
||||
func (m *Frame) UnmarshalYAML(n *yaml.Node) error {
|
||||
m.Data = []byte(n.Value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProtoMessage noop func
|
||||
func (m *Frame) ProtoMessage() {}
|
||||
|
||||
|
@ -13,7 +13,7 @@ type Validator interface {
|
||||
}
|
||||
|
||||
// DefaultConfig default config
|
||||
var DefaultConfig = NewConfig()
|
||||
var DefaultConfig Config = NewConfig()
|
||||
|
||||
// DefaultWatcherMinInterval default min interval for poll changes
|
||||
var DefaultWatcherMinInterval = 5 * time.Second
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Config, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext returns store from context
|
||||
func MustContext(ctx context.Context) Config {
|
||||
c, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing config")
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// NewContext put store in context
|
||||
func NewContext(ctx context.Context, c Config) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -37,7 +37,7 @@ func (c *defaultConfig) Init(opts ...Option) error {
|
||||
c.funcLoad = c.fnLoad
|
||||
c.funcSave = c.fnSave
|
||||
|
||||
c.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
c.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case HookLoad:
|
||||
c.funcLoad = h(c.funcLoad)
|
||||
|
@ -3,26 +3,24 @@ package config_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/config"
|
||||
mid "go.unistack.org/micro/v3/util/id"
|
||||
mtime "go.unistack.org/micro/v3/util/time"
|
||||
)
|
||||
|
||||
type cfg struct {
|
||||
MapValue map[string]bool `default:"key1=true,key2=false"`
|
||||
StructValue *cfgStructValue
|
||||
|
||||
StringValue string `default:"string_value"`
|
||||
IgnoreValue string `json:"-"`
|
||||
UUIDValue string `default:"micro:generate uuid"`
|
||||
IDValue string `default:"micro:generate id"`
|
||||
|
||||
StructValue *cfgStructValue
|
||||
IntValue int `default:"99"`
|
||||
DurationValue time.Duration `default:"10s"`
|
||||
MDurationValue mtime.Duration `default:"10s"`
|
||||
IntValue int `default:"99"`
|
||||
MapValue map[string]bool `default:"key1=true,key2=false"`
|
||||
UUIDValue string `default:"micro:generate uuid"`
|
||||
IDValue string `default:"micro:generate id"`
|
||||
}
|
||||
|
||||
type cfgStructValue struct {
|
||||
@ -114,6 +112,8 @@ func TestDefault(t *testing.T) {
|
||||
|
||||
if conf.IDValue == "" {
|
||||
t.Fatalf("id value empty")
|
||||
} else if len(conf.IDValue) != mid.DefaultSize {
|
||||
t.Fatalf("id value invalid: %s", conf.IDValue)
|
||||
}
|
||||
_ = conf
|
||||
// t.Logf("%#+v\n", conf)
|
||||
@ -134,13 +134,3 @@ func TestValidate(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SizeOf(t *testing.T) {
|
||||
st := cfg{}
|
||||
|
||||
tVal := reflect.TypeOf(st)
|
||||
for i := 0; i < tVal.NumField(); i++ {
|
||||
field := tVal.Field(i)
|
||||
fmt.Printf("Field: %s, Offset: %d, Size: %d\n", field.Name, field.Offset, field.Type.Size())
|
||||
}
|
||||
}
|
||||
|
@ -41,16 +41,14 @@ type Options struct {
|
||||
BeforeInit []func(context.Context, Config) error
|
||||
// AfterInit contains slice of funcs that runs after Init
|
||||
AfterInit []func(context.Context, Config) error
|
||||
|
||||
// AllowFail flag to allow fail in config source
|
||||
AllowFail bool
|
||||
// SkipLoad runs only if condition returns true
|
||||
SkipLoad func(context.Context, Config) bool
|
||||
// SkipSave runs only if condition returns true
|
||||
SkipSave func(context.Context, Config) bool
|
||||
// Hooks can be run before/after config Save/Load
|
||||
Hooks options.Hooks
|
||||
|
||||
// AllowFail flag to allow fail in config source
|
||||
AllowFail bool
|
||||
}
|
||||
|
||||
// Option function signature
|
||||
@ -280,10 +278,10 @@ func WatchCoalesce(b bool) WatchOption {
|
||||
}
|
||||
|
||||
// WatchInterval specifies min and max time.Duration for pulling changes
|
||||
func WatchInterval(minTime, maxTime time.Duration) WatchOption {
|
||||
func WatchInterval(min, max time.Duration) WatchOption {
|
||||
return func(o *WatchOptions) {
|
||||
o.MinInterval = minTime
|
||||
o.MaxInterval = maxTime
|
||||
o.MinInterval = min
|
||||
o.MaxInterval = max
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package errors
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
er "errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
@ -26,7 +26,7 @@ func TestMarshalJSON(t *testing.T) {
|
||||
func TestEmpty(t *testing.T) {
|
||||
msg := "test"
|
||||
var err *Error
|
||||
err = FromError(errors.New(msg))
|
||||
err = FromError(fmt.Errorf(msg))
|
||||
if err.Detail != msg {
|
||||
t.Fatalf("invalid error %v", err)
|
||||
}
|
||||
@ -42,7 +42,7 @@ func TestFromError(t *testing.T) {
|
||||
if merr.ID != "go.micro.test" || merr.Code != 404 {
|
||||
t.Fatalf("invalid conversation %v != %v", err, merr)
|
||||
}
|
||||
err = errors.New(err.Error())
|
||||
err = er.New(err.Error())
|
||||
merr = FromError(err)
|
||||
if merr.ID != "go.micro.test" || merr.Code != 404 {
|
||||
t.Fatalf("invalid conversation %v != %v", err, merr)
|
||||
@ -57,7 +57,7 @@ func TestEqual(t *testing.T) {
|
||||
t.Fatal("errors must be equal")
|
||||
}
|
||||
|
||||
err3 := errors.New("my test err")
|
||||
err3 := er.New("my test err")
|
||||
if Equal(err1, err3) {
|
||||
t.Fatal("errors must be not equal")
|
||||
}
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Flow, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext returns Flow from context
|
||||
func MustContext(ctx context.Context) Flow {
|
||||
f, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing flow")
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// NewContext stores Flow to context
|
||||
func NewContext(ctx context.Context, f Flow) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -32,7 +32,7 @@ type fsm struct {
|
||||
|
||||
// NewFSM creates a new finite state machine having the specified initial state
|
||||
// with specified options
|
||||
func NewFSM(opts ...Option) FSM {
|
||||
func NewFSM(opts ...Option) *fsm {
|
||||
return &fsm{
|
||||
statesMap: map[string]StateFunc{},
|
||||
opts: NewOptions(opts...),
|
||||
|
47
go.mod
47
go.mod
@ -1,43 +1,32 @@
|
||||
module go.unistack.org/micro/v3
|
||||
|
||||
go 1.22.0
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.1
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
dario.cat/mergo v1.0.0
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0
|
||||
github.com/KimMachineGun/automemlimit v0.6.1
|
||||
github.com/ash3in/uuidv8 v1.2.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/matoous/go-nanoid v1.5.1
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5
|
||||
go.uber.org/automaxprocs v1.6.0
|
||||
go.unistack.org/micro-proto/v3 v3.4.1
|
||||
golang.org/x/sync v0.10.0
|
||||
google.golang.org/grpc v1.69.2
|
||||
google.golang.org/protobuf v1.36.1
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
golang.org/x/sync v0.3.0
|
||||
google.golang.org/grpc v1.57.0
|
||||
google.golang.org/protobuf v1.33.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cilium/ebpf v0.16.0 // indirect
|
||||
github.com/containerd/cgroups/v3 v3.0.4 // indirect
|
||||
github.com/containerd/log v0.1.0 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/godbus/dbus/v5 v5.1.0 // indirect
|
||||
github.com/moby/sys/userns v0.1.0 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.2.0 // indirect
|
||||
github.com/cilium/ebpf v0.9.1 // indirect
|
||||
github.com/containerd/cgroups/v3 v3.0.1 // indirect
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
|
||||
github.com/docker/go-units v0.4.0 // indirect
|
||||
github.com/godbus/dbus/v5 v5.0.4 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.0.2 // indirect
|
||||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
go.uber.org/goleak v1.3.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20241210194714-1829a127f884 // indirect
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
golang.org/x/net v0.14.0 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e // indirect
|
||||
)
|
||||
|
137
go.sum
137
go.sum
@ -1,103 +1,78 @@
|
||||
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
|
||||
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
||||
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/KimMachineGun/automemlimit v0.6.1 h1:ILa9j1onAAMadBsyyUJv5cack8Y1WT26yLj/V+ulKp8=
|
||||
github.com/KimMachineGun/automemlimit v0.6.1/go.mod h1:T7xYht7B8r6AG/AqFcUdc7fzd2bIdBKmepfP2S1svPY=
|
||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||
github.com/cilium/ebpf v0.16.0 h1:+BiEnHL6Z7lXnlGUsXQPPAE7+kenAd4ES8MQ5min0Ok=
|
||||
github.com/cilium/ebpf v0.16.0/go.mod h1:L7u2Blt2jMM/vLAVgjxluxtBKlz3/GWjB0dMOEngfwE=
|
||||
github.com/containerd/cgroups/v3 v3.0.4 h1:2fs7l3P0Qxb1nKWuJNFiwhp2CqiKzho71DQkDrHJIo4=
|
||||
github.com/containerd/cgroups/v3 v3.0.4/go.mod h1:SA5DLYnXO8pTGYiAHXz94qvLQTKfVM5GEVisn4jpins=
|
||||
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/cilium/ebpf v0.9.1 h1:64sn2K3UKw8NbP/blsixRpF3nXuyhz/VjRlRzvlBRu4=
|
||||
github.com/cilium/ebpf v0.9.1/go.mod h1:+OhNOIXx/Fnu1IE8bJz2dzOA+VSfyTfdNUVdlQnxUFY=
|
||||
github.com/containerd/cgroups/v3 v3.0.1 h1:4hfGvu8rfGIwVIDd+nLzn/B9ZXx4BcCjzt5ToenJRaE=
|
||||
github.com/containerd/cgroups/v3 v3.0.1/go.mod h1:/vtwk1VXrtoa5AaZLkypuOJgA/6DyPMZHJPGQNtlHnw=
|
||||
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
|
||||
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
|
||||
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
|
||||
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
|
||||
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss=
|
||||
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
|
||||
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
|
||||
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
|
||||
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
|
||||
github.com/jsimonetti/rtnetlink/v2 v2.0.1 h1:xda7qaHDSVOsADNouv7ukSuicKZO7GgVUCXxpaIEIlM=
|
||||
github.com/jsimonetti/rtnetlink/v2 v2.0.1/go.mod h1:7MoNYNbb3UaDHtF8udiJo/RH6VsTKP1pqKLUTVCvToE=
|
||||
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
|
||||
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
|
||||
github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U=
|
||||
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
|
||||
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
|
||||
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
||||
github.com/opencontainers/runtime-spec v1.2.0 h1:z97+pHb3uELt/yiAWD691HNHQIF07bE7dzrbT927iTk=
|
||||
github.com/opencontainers/runtime-spec v1.2.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/opencontainers/runtime-spec v1.0.2 h1:UfAcuLBJB9Coz72x1hgl8O5RVzTdNiaglX6v2DM6FI0=
|
||||
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
||||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
|
||||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
|
||||
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
|
||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5 h1:G/FZtUu7a6NTWl3KUHMV9jkLAh/Rvtf03NWMHaEDl+E=
|
||||
github.com/silas/dag v0.0.0-20220518035006-a7e85ada93c5/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
|
||||
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
|
||||
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q=
|
||||
go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo=
|
||||
golang.org/x/exp v0.0.0-20241210194714-1829a127f884 h1:Y/Mj/94zIQQGHVSv1tTtQBDaQaJe62U9bkDZKKyhPCU=
|
||||
golang.org/x/exp v0.0.0-20241210194714-1829a127f884/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
|
||||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
|
||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484 h1:Z7FRVJPSMaHQxD0uXU8WdgFh8PseLM8Q8NzhnpMrBhQ=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241216192217-9240e9c98484/go.mod h1:lcTa1sDdWEIHMWlITnIczmw5w60CF9ffkb8Z+DVmmjA=
|
||||
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
|
||||
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
|
||||
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
|
||||
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
|
||||
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
|
||||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e h1:NumxXLPfHSndr3wBBdeKiVHjGVFzi9RX2HwwQke94iY=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
|
||||
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
|
||||
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -4,6 +4,17 @@ import "context"
|
||||
|
||||
type loggerKey struct{}
|
||||
|
||||
// MustContext returns logger from passed context or DefaultLogger if empty
|
||||
func MustContext(ctx context.Context) Logger {
|
||||
if ctx == nil {
|
||||
return DefaultLogger
|
||||
}
|
||||
if l, ok := ctx.Value(loggerKey{}).(Logger); ok && l != nil {
|
||||
return l
|
||||
}
|
||||
return DefaultLogger
|
||||
}
|
||||
|
||||
// FromContext returns logger from passed context
|
||||
func FromContext(ctx context.Context) (Logger, bool) {
|
||||
if ctx == nil {
|
||||
@ -13,15 +24,6 @@ func FromContext(ctx context.Context) (Logger, bool) {
|
||||
return l, ok
|
||||
}
|
||||
|
||||
// MustContext returns logger from passed context or DefaultLogger if empty
|
||||
func MustContext(ctx context.Context) Logger {
|
||||
l, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing logger")
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// NewContext stores logger into passed context
|
||||
func NewContext(ctx context.Context, l Logger) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -11,7 +11,7 @@ var DefaultContextAttrFuncs []ContextAttrFunc
|
||||
|
||||
var (
|
||||
// DefaultLogger variable
|
||||
DefaultLogger = NewLogger()
|
||||
DefaultLogger Logger = NewLogger()
|
||||
// DefaultLevel used by logger
|
||||
DefaultLevel = InfoLevel
|
||||
)
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
@ -16,6 +15,18 @@ type Option func(*Options)
|
||||
|
||||
// Options holds logger options
|
||||
type Options struct {
|
||||
// Out holds the output writer
|
||||
Out io.Writer
|
||||
// Context holds exernal options
|
||||
Context context.Context
|
||||
// Name holds the logger name
|
||||
Name string
|
||||
// Fields holds additional metadata
|
||||
Fields []interface{}
|
||||
// callerSkipCount number of frmaes to skip
|
||||
CallerSkipCount int
|
||||
// ContextAttrFuncs contains funcs that executed before log func on context
|
||||
ContextAttrFuncs []ContextAttrFunc
|
||||
// TimeKey is the key used for the time of the log call
|
||||
TimeKey string
|
||||
// LevelKey is the key used for the level of the log call
|
||||
@ -28,30 +39,16 @@ type Options struct {
|
||||
SourceKey string
|
||||
// StacktraceKey is the key used for the stacktrace
|
||||
StacktraceKey string
|
||||
// Name holds the logger name
|
||||
Name string
|
||||
// Out holds the output writer
|
||||
Out io.Writer
|
||||
// Context holds exernal options
|
||||
Context context.Context
|
||||
// Meter used to count logs for specific level
|
||||
Meter meter.Meter
|
||||
// TimeFunc used to obtain current time
|
||||
TimeFunc func() time.Time
|
||||
// Fields holds additional metadata
|
||||
Fields []interface{}
|
||||
// ContextAttrFuncs contains funcs that executed before log func on context
|
||||
ContextAttrFuncs []ContextAttrFunc
|
||||
// callerSkipCount number of frmaes to skip
|
||||
CallerSkipCount int
|
||||
// The logging level the logger should log
|
||||
Level Level
|
||||
// AddSource enabled writing source file and position in log
|
||||
AddSource bool
|
||||
// AddStacktrace controls writing of stacktaces on error
|
||||
AddStacktrace bool
|
||||
// DedupKeys deduplicate keys in log output
|
||||
DedupKeys bool
|
||||
// AddSource enabled writing source file and position in log
|
||||
AddSource bool
|
||||
// The logging level the logger should log
|
||||
Level Level
|
||||
// TimeFunc used to obtain current time
|
||||
TimeFunc func() time.Time
|
||||
// Meter used to count logs for specific level
|
||||
Meter meter.Meter
|
||||
}
|
||||
|
||||
// NewOptions creates new options struct
|
||||
@ -83,33 +80,11 @@ func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithDedupKeys dont log duplicate keys
|
||||
func WithDedupKeys(b bool) Option {
|
||||
return func(o *Options) {
|
||||
o.DedupKeys = b
|
||||
}
|
||||
}
|
||||
|
||||
// WithAddFields add fields for the logger
|
||||
func WithAddFields(fields ...interface{}) Option {
|
||||
return func(o *Options) {
|
||||
if o.DedupKeys {
|
||||
for i := 0; i < len(o.Fields); i += 2 {
|
||||
for j := 0; j < len(fields); j += 2 {
|
||||
iv, iok := o.Fields[i].(string)
|
||||
jv, jok := fields[j].(string)
|
||||
if iok && jok && iv == jv {
|
||||
fields = slices.Delete(fields, j, j+2)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(fields) > 0 {
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
}
|
||||
} else {
|
||||
o.Fields = append(o.Fields, fields...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithFields set default fields for the logger
|
||||
@ -178,8 +153,8 @@ func WithTimeFunc(fn func() time.Time) Option {
|
||||
func WithZapKeys() Option {
|
||||
return func(o *Options) {
|
||||
o.TimeKey = "@timestamp"
|
||||
o.LevelKey = slog.LevelKey
|
||||
o.MessageKey = slog.MessageKey
|
||||
o.LevelKey = "level"
|
||||
o.MessageKey = "msg"
|
||||
o.SourceKey = "caller"
|
||||
o.StacktraceKey = "stacktrace"
|
||||
o.ErrorKey = "error"
|
||||
@ -188,8 +163,8 @@ func WithZapKeys() Option {
|
||||
|
||||
func WithZerologKeys() Option {
|
||||
return func(o *Options) {
|
||||
o.TimeKey = slog.TimeKey
|
||||
o.LevelKey = slog.LevelKey
|
||||
o.TimeKey = "time"
|
||||
o.LevelKey = "level"
|
||||
o.MessageKey = "message"
|
||||
o.SourceKey = "caller"
|
||||
o.StacktraceKey = "stacktrace"
|
||||
@ -211,8 +186,8 @@ func WithSlogKeys() Option {
|
||||
func WithMicroKeys() Option {
|
||||
return func(o *Options) {
|
||||
o.TimeKey = "timestamp"
|
||||
o.LevelKey = slog.LevelKey
|
||||
o.MessageKey = slog.MessageKey
|
||||
o.LevelKey = "level"
|
||||
o.MessageKey = "msg"
|
||||
o.SourceKey = "caller"
|
||||
o.StacktraceKey = "stacktrace"
|
||||
o.ErrorKey = "error"
|
||||
|
@ -2,16 +2,13 @@ package slog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/semconv"
|
||||
@ -22,7 +19,6 @@ const (
|
||||
badKey = "!BADKEY"
|
||||
// defaultCallerSkipCount used by logger
|
||||
defaultCallerSkipCount = 3
|
||||
timeFormat = "2006-01-02T15:04:05.000000000Z07:00"
|
||||
)
|
||||
|
||||
var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`)
|
||||
@ -50,11 +46,11 @@ func (h *wrapper) Handle(ctx context.Context, rec slog.Record) error {
|
||||
}
|
||||
|
||||
func (h *wrapper) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return h.h.WithAttrs(attrs)
|
||||
return h.WithAttrs(attrs)
|
||||
}
|
||||
|
||||
func (h *wrapper) WithGroup(name string) slog.Handler {
|
||||
return h.h.WithGroup(name)
|
||||
return h.WithGroup(name)
|
||||
}
|
||||
|
||||
func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
|
||||
@ -65,7 +61,6 @@ func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
|
||||
a.Key = s.opts.SourceKey
|
||||
case slog.TimeKey:
|
||||
a.Key = s.opts.TimeKey
|
||||
a.Value = slog.StringValue(a.Value.Time().Format(timeFormat))
|
||||
case slog.MessageKey:
|
||||
a.Key = s.opts.MessageKey
|
||||
case slog.LevelKey:
|
||||
@ -94,6 +89,7 @@ func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
|
||||
}
|
||||
|
||||
type slogLogger struct {
|
||||
leveler *slog.LevelVar
|
||||
handler *wrapper
|
||||
opts logger.Options
|
||||
mu sync.RWMutex
|
||||
@ -146,7 +142,6 @@ func (s *slogLogger) Fields(fields ...interface{}) logger.Logger {
|
||||
s.mu.RUnlock()
|
||||
|
||||
l := &slogLogger{opts: options}
|
||||
logger.WithAddFields(fields...)(&l.opts)
|
||||
|
||||
if len(options.ContextAttrFuncs) == 0 {
|
||||
options.ContextAttrFuncs = logger.DefaultContextAttrFuncs
|
||||
@ -177,29 +172,7 @@ func (s *slogLogger) Init(opts ...logger.Option) error {
|
||||
}
|
||||
|
||||
attrs, _ := s.argsAttrs(s.opts.Fields)
|
||||
|
||||
var h slog.Handler
|
||||
if s.opts.Context != nil {
|
||||
if v, ok := s.opts.Context.Value(handlerKey{}).(slog.Handler); ok && v != nil {
|
||||
h = v
|
||||
}
|
||||
|
||||
if fn := s.opts.Context.Value(handlerFnKey{}); fn != nil {
|
||||
if rfn := reflect.ValueOf(fn); rfn.Kind() == reflect.Func {
|
||||
if ret := rfn.Call([]reflect.Value{reflect.ValueOf(s.opts.Out), reflect.ValueOf(handleOpt)}); len(ret) == 1 {
|
||||
if iface, ok := ret[0].Interface().(slog.Handler); ok && iface != nil {
|
||||
h = iface
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if h == nil {
|
||||
h = slog.NewJSONHandler(s.opts.Out, handleOpt)
|
||||
}
|
||||
|
||||
s.handler = &wrapper{h: h.WithAttrs(attrs)}
|
||||
s.handler = &wrapper{h: slog.NewJSONHandler(s.opts.Out, handleOpt).WithAttrs(attrs)}
|
||||
s.handler.level.Store(int64(loggerToSlogLevel(s.opts.Level)))
|
||||
s.mu.Unlock()
|
||||
|
||||
@ -228,10 +201,6 @@ func (s *slogLogger) Error(ctx context.Context, msg string, attrs ...interface{}
|
||||
|
||||
func (s *slogLogger) Fatal(ctx context.Context, msg string, attrs ...interface{}) {
|
||||
s.printLog(ctx, logger.FatalLevel, msg, attrs...)
|
||||
if closer, ok := s.opts.Out.(io.Closer); ok {
|
||||
closer.Close()
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@ -278,7 +247,7 @@ func (s *slogLogger) printLog(ctx context.Context, lvl logger.Level, msg string,
|
||||
}
|
||||
}
|
||||
|
||||
if (s.opts.AddStacktrace || lvl == logger.FatalLevel) || (s.opts.AddStacktrace && lvl == logger.ErrorLevel) {
|
||||
if s.opts.AddStacktrace && lvl == logger.ErrorLevel {
|
||||
stackInfo := make([]byte, 1024*1024)
|
||||
if stackSize := runtime.Stack(stackInfo, false); stackSize > 0 {
|
||||
traceLines := reTrace.Split(string(stackInfo[:stackSize]), -1)
|
||||
@ -349,7 +318,7 @@ func (s *slogLogger) argsAttrs(args []interface{}) ([]slog.Attr, error) {
|
||||
case string:
|
||||
if idx+1 < len(args) {
|
||||
attrs = append(attrs, slog.Any(arg, args[idx+1]))
|
||||
idx++
|
||||
idx += 1
|
||||
} else {
|
||||
attrs = append(attrs, slog.String(badKey, arg))
|
||||
}
|
||||
@ -361,15 +330,3 @@ func (s *slogLogger) argsAttrs(args []interface{}) ([]slog.Attr, error) {
|
||||
|
||||
return attrs, err
|
||||
}
|
||||
|
||||
type handlerKey struct{}
|
||||
|
||||
func WithHandler(h slog.Handler) logger.Option {
|
||||
return logger.SetOption(handlerKey{}, h)
|
||||
}
|
||||
|
||||
type handlerFnKey struct{}
|
||||
|
||||
func WithHandlerFunc(fn any) logger.Option {
|
||||
return logger.SetOption(handlerFnKey{}, fn)
|
||||
}
|
||||
|
@ -6,146 +6,15 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/util/buffer"
|
||||
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
)
|
||||
|
||||
// always first to have proper check
|
||||
func TestStacktrace(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithAddStacktrace(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Error(ctx, "msg1", errors.New("err"))
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`slog_test.go:32`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelayedBuffer(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
dbuf := buffer.NewDelayedBuffer(100, 100*time.Millisecond, buf)
|
||||
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(dbuf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithAddStacktrace(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Error(ctx, "msg1", errors.New("err"))
|
||||
time.Sleep(120 * time.Millisecond)
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`key1=val1`)) {
|
||||
t.Fatalf("logger delayed buffer not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithAddStacktrace(true),
|
||||
logger.WithTimeFunc(func() time.Time {
|
||||
return time.Unix(0, 0)
|
||||
}),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Error(ctx, "msg1", errors.New("err"))
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T03:00:00.000000000+03:00`)) &&
|
||||
!bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T00:00:00.000000000Z`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithDedupKeys(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg1")
|
||||
|
||||
l = l.Fields("key1", "val2")
|
||||
|
||||
l.Info(ctx, "msg2")
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`msg=msg2 key1=val1`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDedupKeysWithAddFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithDedupKeys(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg1")
|
||||
|
||||
if err := l.Init(logger.WithAddFields("key2", "val2")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg2")
|
||||
|
||||
if err := l.Init(logger.WithAddFields("key2", "val3", "key1", "val4")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg3")
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`msg=msg3 key1=val1 key2=val2`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithHandlerFunc(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.InfoLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
)
|
||||
if err := l.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Info(ctx, "msg1")
|
||||
|
||||
if !bytes.Contains(buf.Bytes(), []byte(`msg=msg1`)) {
|
||||
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithAddFields(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
@ -246,7 +115,7 @@ func TestErrorf(t *testing.T) {
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf), logger.WithAddStacktrace(true))
|
||||
if err := l.Init(logger.WithContextAttrFuncs(func(_ context.Context) []interface{} {
|
||||
if err := l.Init(logger.WithContextAttrFuncs(func(ctx context.Context) []interface{} {
|
||||
return nil
|
||||
})); err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -36,8 +36,8 @@ var (
|
||||
circularShortBytes = []byte("<shown>")
|
||||
invalidAngleBytes = []byte("<invalid>")
|
||||
filteredBytes = []byte("<filtered>")
|
||||
// openBracketBytes = []byte("[")
|
||||
// closeBracketBytes = []byte("]")
|
||||
openBracketBytes = []byte("[")
|
||||
closeBracketBytes = []byte("]")
|
||||
percentBytes = []byte("%")
|
||||
precisionBytes = []byte(".")
|
||||
openAngleBytes = []byte("<")
|
||||
@ -52,15 +52,13 @@ type protoMessage interface {
|
||||
}
|
||||
|
||||
type Wrapper struct {
|
||||
pointers map[uintptr]int
|
||||
takeMap map[int]bool
|
||||
|
||||
val interface{}
|
||||
s fmt.State
|
||||
pointers map[uintptr]int
|
||||
opts *Options
|
||||
|
||||
depth int
|
||||
ignoreNextType bool
|
||||
takeMap map[int]bool
|
||||
protoWrapperType bool
|
||||
sqlWrapperType bool
|
||||
}
|
||||
|
@ -82,11 +82,11 @@ func TestTagged(t *testing.T) {
|
||||
func TestTaggedNested(t *testing.T) {
|
||||
type val struct {
|
||||
key string `logger:"take"`
|
||||
// val string `logger:"omit"`
|
||||
val string `logger:"omit"`
|
||||
unk string
|
||||
}
|
||||
type str struct {
|
||||
// key string `logger:"omit"`
|
||||
key string `logger:"omit"`
|
||||
val *val `logger:"take"`
|
||||
}
|
||||
|
||||
|
@ -24,17 +24,6 @@ func FromIncomingContext(ctx context.Context) (Metadata, bool) {
|
||||
return md.md, ok
|
||||
}
|
||||
|
||||
// MustIncomingContext returns metadata from incoming ctx
|
||||
// returned metadata shoud not be modified or race condition happens.
|
||||
// If metadata not exists panics.
|
||||
func MustIncomingContext(ctx context.Context) Metadata {
|
||||
md, ok := FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
panic("missing metadata")
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
// FromOutgoingContext returns metadata from outgoing ctx
|
||||
// returned metadata shoud not be modified or race condition happens
|
||||
func FromOutgoingContext(ctx context.Context) (Metadata, bool) {
|
||||
@ -48,17 +37,6 @@ func FromOutgoingContext(ctx context.Context) (Metadata, bool) {
|
||||
return md.md, ok
|
||||
}
|
||||
|
||||
// MustOutgoingContext returns metadata from outgoing ctx
|
||||
// returned metadata shoud not be modified or race condition happens.
|
||||
// If metadata not exists panics.
|
||||
func MustOutgoingContext(ctx context.Context) Metadata {
|
||||
md, ok := FromOutgoingContext(ctx)
|
||||
if !ok {
|
||||
panic("missing metadata")
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
// FromContext returns metadata from the given context
|
||||
// returned metadata shoud not be modified or race condition happens
|
||||
func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
@ -72,16 +50,6 @@ func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
return md.md, ok
|
||||
}
|
||||
|
||||
// MustContext returns metadata from the given context
|
||||
// returned metadata shoud not be modified or race condition happens
|
||||
func MustContext(ctx context.Context) Metadata {
|
||||
md, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing metadata")
|
||||
}
|
||||
return md
|
||||
}
|
||||
|
||||
// NewContext creates a new context with the given metadata
|
||||
func NewContext(ctx context.Context, md Metadata) context.Context {
|
||||
if ctx == nil {
|
||||
@ -143,7 +111,7 @@ func AppendOutgoingContext(ctx context.Context, kv ...string) context.Context {
|
||||
for k, v := range md {
|
||||
omd.Set(k, v)
|
||||
}
|
||||
return ctx
|
||||
return NewOutgoingContext(ctx, omd)
|
||||
}
|
||||
|
||||
// AppendIncomingContext apends new md to context
|
||||
@ -159,21 +127,5 @@ func AppendIncomingContext(ctx context.Context, kv ...string) context.Context {
|
||||
for k, v := range md {
|
||||
omd.Set(k, v)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// AppendContext apends new md to context
|
||||
func AppendContext(ctx context.Context, kv ...string) context.Context {
|
||||
md, ok := Pairs(kv...)
|
||||
if !ok {
|
||||
return ctx
|
||||
}
|
||||
omd, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
return NewContext(ctx, md)
|
||||
}
|
||||
for k, v := range md {
|
||||
omd.Set(k, v)
|
||||
}
|
||||
return ctx
|
||||
return NewIncomingContext(ctx, omd)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ package metadata
|
||||
import (
|
||||
"net/textproto"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -67,14 +66,6 @@ func (md Metadata) Iterator() *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
func (md Metadata) MustGet(key string) string {
|
||||
val, ok := md.Get(key)
|
||||
if !ok {
|
||||
panic("missing metadata key")
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Get returns value from metadata by key
|
||||
func (md Metadata) Get(key string) (string, bool) {
|
||||
// fast path
|
||||
@ -82,9 +73,6 @@ func (md Metadata) Get(key string) (string, bool) {
|
||||
if !ok {
|
||||
// slow path
|
||||
val, ok = md[textproto.CanonicalMIMEHeaderKey(key)]
|
||||
if !ok {
|
||||
val, ok = md[strings.ToLower(key)]
|
||||
}
|
||||
}
|
||||
return val, ok
|
||||
}
|
||||
@ -106,23 +94,14 @@ func (md Metadata) Del(keys ...string) {
|
||||
delete(md, key)
|
||||
// slow path
|
||||
delete(md, textproto.CanonicalMIMEHeaderKey(key))
|
||||
// very slow path
|
||||
delete(md, strings.ToLower(key))
|
||||
}
|
||||
}
|
||||
|
||||
// Copy makes a copy of the metadata
|
||||
func (md Metadata) CopyTo(dst Metadata) {
|
||||
for k, v := range md {
|
||||
dst[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Copy makes a copy of the metadata
|
||||
func Copy(md Metadata, exclude ...string) Metadata {
|
||||
nmd := New(len(md))
|
||||
for k, v := range md {
|
||||
nmd[k] = v
|
||||
for key, val := range md {
|
||||
nmd.Set(key, val)
|
||||
}
|
||||
nmd.Del(exclude...)
|
||||
return nmd
|
||||
@ -146,7 +125,7 @@ func Merge(omd Metadata, mmd Metadata, overwrite bool) Metadata {
|
||||
case ok && !overwrite:
|
||||
continue
|
||||
case val != "":
|
||||
nmd[key] = val
|
||||
nmd.Set(key, val)
|
||||
case ok && val == "":
|
||||
nmd.Del(key)
|
||||
}
|
||||
@ -160,8 +139,6 @@ func Pairs(kv ...string) (Metadata, bool) {
|
||||
return nil, false
|
||||
}
|
||||
md := New(len(kv) / 2)
|
||||
for idx := 0; idx < len(kv); idx += 2 {
|
||||
md[kv[idx]] = kv[idx+1]
|
||||
}
|
||||
md.Set(kv...)
|
||||
return md, true
|
||||
}
|
||||
|
@ -5,15 +5,6 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLowercase(t *testing.T) {
|
||||
md := New(1)
|
||||
md["x-request-id"] = "12345"
|
||||
v, ok := md.Get("X-Request-Id")
|
||||
if !ok || v == "" {
|
||||
t.Fatalf("metadata invalid %#+v", md)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleUsage(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
md := New(0)
|
||||
@ -32,8 +23,8 @@ func TestMultipleUsage(t *testing.T) {
|
||||
m.Del("key1_3")
|
||||
return ctx
|
||||
}(ctx)
|
||||
_ = imd
|
||||
_ = omd
|
||||
t.Logf("imd %#+v", imd)
|
||||
t.Logf("omd %#+v", omd)
|
||||
}
|
||||
|
||||
func TestMetadataSetMultiple(t *testing.T) {
|
||||
@ -88,14 +79,14 @@ func TestPassing(t *testing.T) {
|
||||
|
||||
ctx = NewIncomingContext(ctx, md1)
|
||||
testCtx(ctx)
|
||||
_, ok := FromOutgoingContext(ctx)
|
||||
md, ok := FromOutgoingContext(ctx)
|
||||
if ok {
|
||||
t.Fatalf("create outgoing context")
|
||||
}
|
||||
|
||||
ctx = NewOutgoingContext(ctx, New(1))
|
||||
testCtx(ctx)
|
||||
md, ok := FromOutgoingContext(ctx)
|
||||
md, ok = FromOutgoingContext(ctx)
|
||||
if !ok {
|
||||
t.Fatalf("missing metadata from outgoing context")
|
||||
}
|
||||
@ -118,7 +109,7 @@ func TestMerge(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIterator(_ *testing.T) {
|
||||
func TestIterator(t *testing.T) {
|
||||
md := Metadata{
|
||||
"1Last": "last",
|
||||
"2First": "first",
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Meter, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext get meter from context
|
||||
func MustContext(ctx context.Context) Meter {
|
||||
m, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing meter")
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// NewContext put meter in context
|
||||
func NewContext(ctx context.Context, c Meter) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
|
||||
var (
|
||||
// DefaultMeter is the default meter
|
||||
DefaultMeter = NewMeter()
|
||||
DefaultMeter Meter = NewMeter()
|
||||
// DefaultAddress data will be made available on this host:port
|
||||
DefaultAddress = ":9090"
|
||||
// DefaultPath the meter endpoint where the Meter data will be made available
|
||||
|
@ -37,32 +37,32 @@ func (r *noopMeter) Init(opts ...Option) error {
|
||||
}
|
||||
|
||||
// Counter implements the Meter interface
|
||||
func (r *noopMeter) Counter(_ string, labels ...string) Counter {
|
||||
func (r *noopMeter) Counter(name string, labels ...string) Counter {
|
||||
return &noopCounter{labels: labels}
|
||||
}
|
||||
|
||||
// FloatCounter implements the Meter interface
|
||||
func (r *noopMeter) FloatCounter(_ string, labels ...string) FloatCounter {
|
||||
func (r *noopMeter) FloatCounter(name string, labels ...string) FloatCounter {
|
||||
return &noopFloatCounter{labels: labels}
|
||||
}
|
||||
|
||||
// Gauge implements the Meter interface
|
||||
func (r *noopMeter) Gauge(_ string, _ func() float64, labels ...string) Gauge {
|
||||
func (r *noopMeter) Gauge(name string, f func() float64, labels ...string) Gauge {
|
||||
return &noopGauge{labels: labels}
|
||||
}
|
||||
|
||||
// Summary implements the Meter interface
|
||||
func (r *noopMeter) Summary(_ string, labels ...string) Summary {
|
||||
func (r *noopMeter) Summary(name string, labels ...string) Summary {
|
||||
return &noopSummary{labels: labels}
|
||||
}
|
||||
|
||||
// SummaryExt implements the Meter interface
|
||||
func (r *noopMeter) SummaryExt(_ string, _ time.Duration, _ []float64, labels ...string) Summary {
|
||||
func (r *noopMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) Summary {
|
||||
return &noopSummary{labels: labels}
|
||||
}
|
||||
|
||||
// Histogram implements the Meter interface
|
||||
func (r *noopMeter) Histogram(_ string, labels ...string) Histogram {
|
||||
func (r *noopMeter) Histogram(name string, labels ...string) Histogram {
|
||||
return &noopHistogram{labels: labels}
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ func (r *noopMeter) Set(opts ...Option) Meter {
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *noopMeter) Write(_ io.Writer, _ ...Option) error {
|
||||
func (r *noopMeter) Write(w io.Writer, opts ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
36
micro.go
36
micro.go
@ -65,8 +65,6 @@ func As(b any, target any) bool {
|
||||
break
|
||||
case targetType.Implements(routerType):
|
||||
break
|
||||
case targetType.Implements(tracerType):
|
||||
break
|
||||
default:
|
||||
return false
|
||||
}
|
||||
@ -78,21 +76,19 @@ func As(b any, target any) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
var (
|
||||
brokerType = reflect.TypeOf((*broker.Broker)(nil)).Elem()
|
||||
loggerType = reflect.TypeOf((*logger.Logger)(nil)).Elem()
|
||||
clientType = reflect.TypeOf((*client.Client)(nil)).Elem()
|
||||
serverType = reflect.TypeOf((*server.Server)(nil)).Elem()
|
||||
codecType = reflect.TypeOf((*codec.Codec)(nil)).Elem()
|
||||
flowType = reflect.TypeOf((*flow.Flow)(nil)).Elem()
|
||||
fsmType = reflect.TypeOf((*fsm.FSM)(nil)).Elem()
|
||||
meterType = reflect.TypeOf((*meter.Meter)(nil)).Elem()
|
||||
registerType = reflect.TypeOf((*register.Register)(nil)).Elem()
|
||||
resolverType = reflect.TypeOf((*resolver.Resolver)(nil)).Elem()
|
||||
routerType = reflect.TypeOf((*router.Router)(nil)).Elem()
|
||||
selectorType = reflect.TypeOf((*selector.Selector)(nil)).Elem()
|
||||
storeType = reflect.TypeOf((*store.Store)(nil)).Elem()
|
||||
syncType = reflect.TypeOf((*sync.Sync)(nil)).Elem()
|
||||
tracerType = reflect.TypeOf((*tracer.Tracer)(nil)).Elem()
|
||||
serviceType = reflect.TypeOf((*Service)(nil)).Elem()
|
||||
)
|
||||
var brokerType = reflect.TypeOf((*broker.Broker)(nil)).Elem()
|
||||
var loggerType = reflect.TypeOf((*logger.Logger)(nil)).Elem()
|
||||
var clientType = reflect.TypeOf((*client.Client)(nil)).Elem()
|
||||
var serverType = reflect.TypeOf((*server.Server)(nil)).Elem()
|
||||
var codecType = reflect.TypeOf((*codec.Codec)(nil)).Elem()
|
||||
var flowType = reflect.TypeOf((*flow.Flow)(nil)).Elem()
|
||||
var fsmType = reflect.TypeOf((*fsm.FSM)(nil)).Elem()
|
||||
var meterType = reflect.TypeOf((*meter.Meter)(nil)).Elem()
|
||||
var registerType = reflect.TypeOf((*register.Register)(nil)).Elem()
|
||||
var resolverType = reflect.TypeOf((*resolver.Resolver)(nil)).Elem()
|
||||
var routerType = reflect.TypeOf((*router.Router)(nil)).Elem()
|
||||
var selectorType = reflect.TypeOf((*selector.Selector)(nil)).Elem()
|
||||
var storeType = reflect.TypeOf((*store.Store)(nil)).Elem()
|
||||
var syncType = reflect.TypeOf((*sync.Sync)(nil)).Elem()
|
||||
var tracerType = reflect.TypeOf((*tracer.Tracer)(nil)).Elem()
|
||||
var serviceType = reflect.TypeOf((*Service)(nil)).Elem()
|
||||
|
@ -18,27 +18,26 @@ func TestAs(t *testing.T) {
|
||||
testCases := []struct {
|
||||
b any
|
||||
target any
|
||||
want any
|
||||
|
||||
match bool
|
||||
want any
|
||||
}{
|
||||
{
|
||||
b: broTarget,
|
||||
target: &b,
|
||||
match: true,
|
||||
want: broTarget,
|
||||
broTarget,
|
||||
&b,
|
||||
true,
|
||||
broTarget,
|
||||
},
|
||||
{
|
||||
b: nil,
|
||||
target: &b,
|
||||
match: false,
|
||||
want: nil,
|
||||
nil,
|
||||
&b,
|
||||
false,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
b: fsmTarget,
|
||||
target: &b,
|
||||
match: false,
|
||||
want: nil,
|
||||
fsmTarget,
|
||||
&b,
|
||||
false,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
for i, tc := range testCases {
|
||||
@ -73,7 +72,7 @@ func (p *bro) Ready() bool { return true }
|
||||
|
||||
func (p *bro) Health() bool { return true }
|
||||
|
||||
func (p *bro) Init(_ ...broker.Option) error { return nil }
|
||||
func (p *bro) Init(opts ...broker.Option) error { return nil }
|
||||
|
||||
// Options returns broker options
|
||||
func (p *bro) Options() broker.Options { return broker.Options{} }
|
||||
@ -82,28 +81,28 @@ func (p *bro) Options() broker.Options { return broker.Options{} }
|
||||
func (p *bro) Address() string { return "" }
|
||||
|
||||
// Connect connects to broker
|
||||
func (p *bro) Connect(_ context.Context) error { return nil }
|
||||
func (p *bro) Connect(ctx context.Context) error { return nil }
|
||||
|
||||
// Disconnect disconnect from broker
|
||||
func (p *bro) Disconnect(_ context.Context) error { return nil }
|
||||
func (p *bro) Disconnect(ctx context.Context) error { return nil }
|
||||
|
||||
// Publish message, msg can be single broker.Message or []broker.Message
|
||||
func (p *bro) Publish(_ context.Context, _ string, _ *broker.Message, _ ...broker.PublishOption) error {
|
||||
func (p *bro) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchPublish messages to broker with multiple topics
|
||||
func (p *bro) BatchPublish(_ context.Context, _ []*broker.Message, _ ...broker.PublishOption) error {
|
||||
func (p *bro) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchSubscribe subscribes to topic messages via handler
|
||||
func (p *bro) BatchSubscribe(_ context.Context, _ string, _ broker.BatchHandler, _ ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||
func (p *bro) BatchSubscribe(ctx context.Context, topic string, h broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Subscribe subscribes to topic message via handler
|
||||
func (p *bro) Subscribe(_ context.Context, _ string, _ broker.Handler, _ ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||
func (p *bro) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@ -114,9 +113,9 @@ type fsmT struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (f *fsmT) Start(_ context.Context, _ interface{}, _ ...Option) (interface{}, error) {
|
||||
func (f *fsmT) Start(ctx context.Context, a interface{}, o ...Option) (interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fsmT) Current() string { return f.name }
|
||||
func (f *fsmT) Reset() {}
|
||||
func (f *fsmT) State(_ string, _ fsm.StateFunc) {}
|
||||
func (f *fsmT) State(s string, sf fsm.StateFunc) {}
|
||||
|
@ -8,18 +8,17 @@ import (
|
||||
|
||||
// CertificateOptions holds options for x509.CreateCertificate
|
||||
type CertificateOptions struct {
|
||||
Organization []string
|
||||
OrganizationalUnit []string
|
||||
CommonName string
|
||||
OCSPServer []string
|
||||
IssuingCertificateURL []string
|
||||
SerialNumber *big.Int
|
||||
NotAfter time.Time
|
||||
NotBefore time.Time
|
||||
CommonName string
|
||||
Organization []string
|
||||
OrganizationalUnit []string
|
||||
OCSPServer []string
|
||||
IssuingCertificateURL []string
|
||||
ExtKeyUsage []x509.ExtKeyUsage
|
||||
|
||||
SignatureAlgorithm x509.SignatureAlgorithm
|
||||
PublicKeyAlgorithm x509.PublicKeyAlgorithm
|
||||
ExtKeyUsage []x509.ExtKeyUsage
|
||||
KeyUsage x509.KeyUsage
|
||||
IsCA bool
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ func Tracer(t tracer.Tracer) Option {
|
||||
// NewOptions returns network default options
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
ID: id.MustNew(),
|
||||
ID: id.Must(),
|
||||
Name: "go.micro",
|
||||
Address: ":0",
|
||||
Logger: logger.DefaultLogger,
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
var (
|
||||
// DefaultTransport is the global default transport
|
||||
DefaultTransport = NewTransport()
|
||||
DefaultTransport Transport = NewTransport()
|
||||
// DefaultDialTimeout the default dial timeout
|
||||
DefaultDialTimeout = time.Second * 5
|
||||
)
|
||||
|
@ -84,7 +84,7 @@ func (t *tunBroker) Disconnect(ctx context.Context) error {
|
||||
return t.tunnel.Close(ctx)
|
||||
}
|
||||
|
||||
func (t *tunBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, _ ...broker.PublishOption) error {
|
||||
func (t *tunBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
||||
// TODO: this is probably inefficient, we might want to just maintain an open connection
|
||||
// it may be easier to add broadcast to the tunnel
|
||||
topicMap := make(map[string]tunnel.Session)
|
||||
@ -114,7 +114,7 @@ func (t *tunBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, _
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *tunBroker) Publish(ctx context.Context, topic string, m *broker.Message, _ ...broker.PublishOption) error {
|
||||
func (t *tunBroker) Publish(ctx context.Context, topic string, m *broker.Message, opts ...broker.PublishOption) error {
|
||||
// TODO: this is probably inefficient, we might want to just maintain an open connection
|
||||
// it may be easier to add broadcast to the tunnel
|
||||
c, err := t.tunnel.Dial(ctx, topic, tunnel.DialMode(tunnel.Multicast))
|
||||
|
@ -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: id.MustNew(),
|
||||
ID: id.Must(),
|
||||
Address: DefaultAddress,
|
||||
Token: DefaultToken,
|
||||
Logger: logger.DefaultLogger,
|
||||
|
@ -294,7 +294,7 @@ type loggerOptions struct {
|
||||
brokers []string
|
||||
registers []string
|
||||
stores []string
|
||||
// meters []string
|
||||
meters []string
|
||||
tracers []string
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,6 @@ func (p *noopProfiler) String() string {
|
||||
}
|
||||
|
||||
// NewProfiler returns new noop profiler
|
||||
func NewProfiler(_ ...Option) Profiler {
|
||||
func NewProfiler(opts ...Option) Profiler {
|
||||
return &noopProfiler{}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ type Profiler interface {
|
||||
}
|
||||
|
||||
// DefaultProfiler holds the default profiler
|
||||
var DefaultProfiler = NewProfiler()
|
||||
var DefaultProfiler Profiler = NewProfiler()
|
||||
|
||||
// Options holds the options for profiler
|
||||
type Options struct {
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Register, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext get register from context
|
||||
func MustContext(ctx context.Context) Register {
|
||||
r, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing register")
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// NewContext put register in context
|
||||
func NewContext(ctx context.Context, c Register) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -1,9 +1,12 @@
|
||||
package register
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
)
|
||||
|
||||
// ExtractValue from reflect.Type from specified depth
|
||||
@ -35,6 +38,53 @@ func ExtractValue(v reflect.Type, d int) string {
|
||||
return v.Name()
|
||||
}
|
||||
|
||||
// ExtractEndpoint extract *Endpoint from reflect.Method
|
||||
func ExtractEndpoint(method reflect.Method) *Endpoint {
|
||||
if method.PkgPath != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var rspType, reqType reflect.Type
|
||||
var stream bool
|
||||
mt := method.Type
|
||||
|
||||
switch mt.NumIn() {
|
||||
case 3:
|
||||
reqType = mt.In(1)
|
||||
rspType = mt.In(2)
|
||||
case 4:
|
||||
reqType = mt.In(2)
|
||||
rspType = mt.In(3)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
// are we dealing with a stream?
|
||||
switch rspType.Kind() {
|
||||
case reflect.Func, reflect.Interface:
|
||||
stream = true
|
||||
}
|
||||
|
||||
request := ExtractValue(reqType, 0)
|
||||
response := ExtractValue(rspType, 0)
|
||||
if request == "" || response == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
ep := &Endpoint{
|
||||
Name: method.Name,
|
||||
Request: request,
|
||||
Response: response,
|
||||
Metadata: metadata.New(0),
|
||||
}
|
||||
|
||||
if stream {
|
||||
ep.Metadata.Set("stream", fmt.Sprintf("%v", stream))
|
||||
}
|
||||
|
||||
return ep
|
||||
}
|
||||
|
||||
// ExtractSubValue exctact *Value from reflect.Type
|
||||
func ExtractSubValue(typ reflect.Type) string {
|
||||
var reqType reflect.Type
|
||||
|
@ -2,6 +2,8 @@ package register
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestHandler struct{}
|
||||
@ -13,3 +15,40 @@ type TestResponse struct{}
|
||||
func (t *TestHandler) Test(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestExtractEndpoint(t *testing.T) {
|
||||
handler := &TestHandler{}
|
||||
typ := reflect.TypeOf(handler)
|
||||
|
||||
var endpoints []*Endpoint
|
||||
|
||||
for m := 0; m < typ.NumMethod(); m++ {
|
||||
if e := ExtractEndpoint(typ.Method(m)); e != nil {
|
||||
endpoints = append(endpoints, e)
|
||||
}
|
||||
}
|
||||
|
||||
if i := len(endpoints); i != 1 {
|
||||
t.Fatalf("Expected 1 endpoint, have %d", i)
|
||||
}
|
||||
|
||||
if endpoints[0].Name != "Test" {
|
||||
t.Fatalf("Expected handler Test, got %s", endpoints[0].Name)
|
||||
}
|
||||
|
||||
if endpoints[0].Request == "" {
|
||||
t.Fatal("Expected non nil Request")
|
||||
}
|
||||
|
||||
if endpoints[0].Response == "" {
|
||||
t.Fatal("Expected non nil Request")
|
||||
}
|
||||
|
||||
if endpoints[0].Request != "TestRequest" {
|
||||
t.Fatalf("Expected TestRequest got %s", endpoints[0].Request)
|
||||
}
|
||||
|
||||
if endpoints[0].Response != "TestResponse" {
|
||||
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
|
||||
}
|
||||
}
|
||||
|
@ -27,13 +27,14 @@ type record struct {
|
||||
Version string
|
||||
Metadata map[string]string
|
||||
Nodes map[string]*node
|
||||
Endpoints []*register.Endpoint
|
||||
}
|
||||
|
||||
type memory struct {
|
||||
sync.RWMutex
|
||||
records map[string]services
|
||||
watchers map[string]*watcher
|
||||
opts register.Options
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
// services is a KV map with service name as the key and a map of records as the value
|
||||
@ -58,7 +59,7 @@ func (m *memory) ttlPrune() {
|
||||
|
||||
for range prune.C {
|
||||
m.Lock()
|
||||
for namespace, services := range m.records {
|
||||
for domain, services := range m.records {
|
||||
for service, versions := range services {
|
||||
for version, record := range versions {
|
||||
for id, n := range record.Nodes {
|
||||
@ -66,7 +67,7 @@ func (m *memory) ttlPrune() {
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register TTL expired for node %s of service %s", n.ID, service))
|
||||
}
|
||||
delete(m.records[namespace][service][version].Nodes, id)
|
||||
delete(m.records[domain][service][version].Nodes, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -99,11 +100,11 @@ func (m *memory) sendEvent(r *register.Result) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *memory) Connect(_ context.Context) error {
|
||||
func (m *memory) Connect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memory) Disconnect(_ context.Context) error {
|
||||
func (m *memory) Disconnect(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -123,19 +124,24 @@ func (m *memory) Options() register.Options {
|
||||
return m.opts
|
||||
}
|
||||
|
||||
func (m *memory) Register(_ context.Context, s *register.Service, opts ...register.RegisterOption) error {
|
||||
func (m *memory) Register(ctx context.Context, s *register.Service, opts ...register.RegisterOption) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
options := register.NewRegisterOptions(opts...)
|
||||
|
||||
// get the services for this domain from the register
|
||||
srvs, ok := m.records[options.Namespace]
|
||||
srvs, ok := m.records[options.Domain]
|
||||
if !ok {
|
||||
srvs = make(services)
|
||||
}
|
||||
|
||||
s.Namespace = options.Namespace
|
||||
// domain is set in metadata so it can be passed to watchers
|
||||
if s.Metadata == nil {
|
||||
s.Metadata = map[string]string{"domain": options.Domain}
|
||||
} else {
|
||||
s.Metadata["domain"] = options.Domain
|
||||
}
|
||||
|
||||
// ensure the service name exists
|
||||
r := serviceToRecord(s, options.TTL)
|
||||
@ -148,8 +154,8 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register added new service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
m.records[options.Namespace] = srvs
|
||||
go m.sendEvent(®ister.Result{Action: register.EventCreate, Service: s})
|
||||
m.records[options.Domain] = srvs
|
||||
go m.sendEvent(®ister.Result{Action: "create", Service: s})
|
||||
}
|
||||
|
||||
var addedNodes bool
|
||||
@ -167,6 +173,9 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
metadata[k] = v
|
||||
}
|
||||
|
||||
// set the domain
|
||||
metadata["domain"] = options.Domain
|
||||
|
||||
// add the node
|
||||
srvs[s.Name][s.Version].Nodes[n.ID] = &node{
|
||||
Node: ®ister.Node{
|
||||
@ -185,7 +194,7 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register added new node to service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
go m.sendEvent(®ister.Result{Action: register.EventUpdate, Service: s})
|
||||
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
||||
} else {
|
||||
// refresh TTL and timestamp
|
||||
for _, n := range s.Nodes {
|
||||
@ -197,7 +206,7 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
||||
}
|
||||
}
|
||||
|
||||
m.records[options.Namespace] = srvs
|
||||
m.records[options.Domain] = srvs
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -207,8 +216,15 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
|
||||
options := register.NewDeregisterOptions(opts...)
|
||||
|
||||
// domain is set in metadata so it can be passed to watchers
|
||||
if s.Metadata == nil {
|
||||
s.Metadata = map[string]string{"domain": options.Domain}
|
||||
} else {
|
||||
s.Metadata["domain"] = options.Domain
|
||||
}
|
||||
|
||||
// if the domain doesn't exist, there is nothing to deregister
|
||||
services, ok := m.records[options.Namespace]
|
||||
services, ok := m.records[options.Domain]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
@ -237,16 +253,16 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
// if the nodes not empty, we replace the version in the store and exist, the rest of the logic
|
||||
// is cleanup
|
||||
if len(version.Nodes) > 0 {
|
||||
m.records[options.Namespace][s.Name][s.Version] = version
|
||||
go m.sendEvent(®ister.Result{Action: register.EventUpdate, Service: s})
|
||||
m.records[options.Domain][s.Name][s.Version] = version
|
||||
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
||||
return nil
|
||||
}
|
||||
|
||||
// if this version was the only version of the service, we can remove the whole service from the
|
||||
// register and exit
|
||||
if len(versions) == 1 {
|
||||
delete(m.records[options.Namespace], s.Name)
|
||||
go m.sendEvent(®ister.Result{Action: register.EventDelete, Service: s})
|
||||
delete(m.records[options.Domain], s.Name)
|
||||
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
||||
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register removed service: %s", s.Name))
|
||||
@ -255,8 +271,8 @@ func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...re
|
||||
}
|
||||
|
||||
// there are other versions of the service running, so only remove this version of it
|
||||
delete(m.records[options.Namespace][s.Name], s.Version)
|
||||
go m.sendEvent(®ister.Result{Action: register.EventDelete, Service: s})
|
||||
delete(m.records[options.Domain][s.Name], s.Version)
|
||||
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
||||
if m.opts.Logger.V(logger.DebugLevel) {
|
||||
m.opts.Logger.Debug(m.opts.Context, fmt.Sprintf("Register removed service: %s, version: %s", s.Name, s.Version))
|
||||
}
|
||||
@ -268,15 +284,15 @@ func (m *memory) LookupService(ctx context.Context, name string, opts ...registe
|
||||
options := register.NewLookupOptions(opts...)
|
||||
|
||||
// if it's a wildcard domain, return from all domains
|
||||
if options.Namespace == register.WildcardNamespace {
|
||||
if options.Domain == register.WildcardDomain {
|
||||
m.RLock()
|
||||
recs := m.records
|
||||
m.RUnlock()
|
||||
|
||||
var services []*register.Service
|
||||
|
||||
for namespace := range recs {
|
||||
srvs, err := m.LookupService(ctx, name, append(opts, register.LookupNamespace(namespace))...)
|
||||
for domain := range recs {
|
||||
srvs, err := m.LookupService(ctx, name, append(opts, register.LookupDomain(domain))...)
|
||||
if err == register.ErrNotFound {
|
||||
continue
|
||||
} else if err != nil {
|
||||
@ -295,7 +311,7 @@ func (m *memory) LookupService(ctx context.Context, name string, opts ...registe
|
||||
defer m.RUnlock()
|
||||
|
||||
// check the domain exists
|
||||
services, ok := m.records[options.Namespace]
|
||||
services, ok := m.records[options.Domain]
|
||||
if !ok {
|
||||
return nil, register.ErrNotFound
|
||||
}
|
||||
@ -312,7 +328,7 @@ func (m *memory) LookupService(ctx context.Context, name string, opts ...registe
|
||||
var i int
|
||||
|
||||
for _, r := range versions {
|
||||
result[i] = recordToService(r, options.Namespace)
|
||||
result[i] = recordToService(r, options.Domain)
|
||||
i++
|
||||
}
|
||||
|
||||
@ -323,15 +339,15 @@ func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption)
|
||||
options := register.NewListOptions(opts...)
|
||||
|
||||
// if it's a wildcard domain, list from all domains
|
||||
if options.Namespace == register.WildcardNamespace {
|
||||
if options.Domain == register.WildcardDomain {
|
||||
m.RLock()
|
||||
recs := m.records
|
||||
m.RUnlock()
|
||||
|
||||
var services []*register.Service
|
||||
|
||||
for namespace := range recs {
|
||||
srvs, err := m.ListServices(ctx, append(opts, register.ListNamespace(namespace))...)
|
||||
for domain := range recs {
|
||||
srvs, err := m.ListServices(ctx, append(opts, register.ListDomain(domain))...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -345,7 +361,7 @@ func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption)
|
||||
defer m.RUnlock()
|
||||
|
||||
// ensure the domain exists
|
||||
services, ok := m.records[options.Namespace]
|
||||
services, ok := m.records[options.Domain]
|
||||
if !ok {
|
||||
return make([]*register.Service, 0), nil
|
||||
}
|
||||
@ -355,7 +371,7 @@ func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption)
|
||||
|
||||
for _, service := range services {
|
||||
for _, version := range service {
|
||||
result = append(result, recordToService(version, options.Namespace))
|
||||
result = append(result, recordToService(version, options.Domain))
|
||||
}
|
||||
}
|
||||
|
||||
@ -410,13 +426,16 @@ func (m *watcher) Next() (*register.Result, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
namespace := register.DefaultNamespace
|
||||
if r.Service.Namespace != "" {
|
||||
namespace = r.Service.Namespace
|
||||
// extract domain from service metadata
|
||||
var domain string
|
||||
if r.Service.Metadata != nil && len(r.Service.Metadata["domain"]) > 0 {
|
||||
domain = r.Service.Metadata["domain"]
|
||||
} else {
|
||||
domain = register.DefaultDomain
|
||||
}
|
||||
|
||||
// only send the event if watching the wildcard or this specific domain
|
||||
if m.wo.Namespace == register.WildcardNamespace || m.wo.Namespace == namespace {
|
||||
if m.wo.Domain == register.WildcardDomain || m.wo.Domain == domain {
|
||||
return r, nil
|
||||
}
|
||||
case <-m.exit:
|
||||
@ -435,6 +454,11 @@ func (m *watcher) Stop() {
|
||||
}
|
||||
|
||||
func serviceToRecord(s *register.Service, ttl time.Duration) *record {
|
||||
metadata := make(map[string]string, len(s.Metadata))
|
||||
for k, v := range s.Metadata {
|
||||
metadata[k] = v
|
||||
}
|
||||
|
||||
nodes := make(map[string]*node, len(s.Nodes))
|
||||
for _, n := range s.Nodes {
|
||||
nodes[n.ID] = &node{
|
||||
@ -444,19 +468,44 @@ func serviceToRecord(s *register.Service, ttl time.Duration) *record {
|
||||
}
|
||||
}
|
||||
|
||||
endpoints := make([]*register.Endpoint, len(s.Endpoints))
|
||||
for i, e := range s.Endpoints {
|
||||
endpoints[i] = e
|
||||
}
|
||||
|
||||
return &record{
|
||||
Name: s.Name,
|
||||
Version: s.Version,
|
||||
Metadata: metadata,
|
||||
Nodes: nodes,
|
||||
Endpoints: endpoints,
|
||||
}
|
||||
}
|
||||
|
||||
func recordToService(r *record, namespace string) *register.Service {
|
||||
func recordToService(r *record, domain string) *register.Service {
|
||||
metadata := make(map[string]string, len(r.Metadata))
|
||||
for k, v := range r.Metadata {
|
||||
metadata[k] = v
|
||||
}
|
||||
|
||||
// set the domain in metadata so it can be determined when a wildcard query is performed
|
||||
metadata["domain"] = domain
|
||||
|
||||
endpoints := make([]*register.Endpoint, len(r.Endpoints))
|
||||
for i, e := range r.Endpoints {
|
||||
md := make(map[string]string, len(e.Metadata))
|
||||
for k, v := range e.Metadata {
|
||||
md[k] = v
|
||||
}
|
||||
|
||||
endpoints[i] = ®ister.Endpoint{
|
||||
Name: e.Name,
|
||||
Request: e.Request,
|
||||
Response: e.Response,
|
||||
Metadata: md,
|
||||
}
|
||||
}
|
||||
|
||||
nodes := make([]*register.Node, len(r.Nodes))
|
||||
i := 0
|
||||
for _, n := range r.Nodes {
|
||||
@ -476,7 +525,8 @@ func recordToService(r *record, namespace string) *register.Service {
|
||||
return ®ister.Service{
|
||||
Name: r.Name,
|
||||
Version: r.Version,
|
||||
Metadata: metadata,
|
||||
Endpoints: endpoints,
|
||||
Nodes: nodes,
|
||||
Namespace: namespace,
|
||||
}
|
||||
}
|
||||
|
@ -253,32 +253,32 @@ func TestMemoryWildcard(t *testing.T) {
|
||||
|
||||
testSrv := ®ister.Service{Name: "foo", Version: "1.0.0"}
|
||||
|
||||
if err := m.Register(ctx, testSrv, register.RegisterNamespace("one")); err != nil {
|
||||
if err := m.Register(ctx, testSrv, register.RegisterDomain("one")); err != nil {
|
||||
t.Fatalf("Register err: %v", err)
|
||||
}
|
||||
if err := m.Register(ctx, testSrv, register.RegisterNamespace("two")); err != nil {
|
||||
if err := m.Register(ctx, testSrv, register.RegisterDomain("two")); err != nil {
|
||||
t.Fatalf("Register err: %v", err)
|
||||
}
|
||||
|
||||
if recs, err := m.ListServices(ctx, register.ListNamespace("one")); err != nil {
|
||||
if recs, err := m.ListServices(ctx, register.ListDomain("one")); err != nil {
|
||||
t.Errorf("List err: %v", err)
|
||||
} else if len(recs) != 1 {
|
||||
t.Errorf("Expected 1 record, got %v", len(recs))
|
||||
}
|
||||
|
||||
if recs, err := m.ListServices(ctx, register.ListNamespace("*")); err != nil {
|
||||
if recs, err := m.ListServices(ctx, register.ListDomain("*")); err != nil {
|
||||
t.Errorf("List err: %v", err)
|
||||
} else if len(recs) != 2 {
|
||||
t.Errorf("Expected 2 records, got %v", len(recs))
|
||||
}
|
||||
|
||||
if recs, err := m.LookupService(ctx, testSrv.Name, register.LookupNamespace("one")); err != nil {
|
||||
if recs, err := m.LookupService(ctx, testSrv.Name, register.LookupDomain("one")); err != nil {
|
||||
t.Errorf("Lookup err: %v", err)
|
||||
} else if len(recs) != 1 {
|
||||
t.Errorf("Expected 1 record, got %v", len(recs))
|
||||
}
|
||||
|
||||
if recs, err := m.LookupService(ctx, testSrv.Name, register.LookupNamespace("*")); err != nil {
|
||||
if recs, err := m.LookupService(ctx, testSrv.Name, register.LookupDomain("*")); err != nil {
|
||||
t.Errorf("Lookup err: %v", err)
|
||||
} else if len(recs) != 2 {
|
||||
t.Errorf("Expected 2 records, got %v", len(recs))
|
||||
@ -290,29 +290,27 @@ func TestWatcher(t *testing.T) {
|
||||
|
||||
ctx := context.TODO()
|
||||
m := NewRegister()
|
||||
if err := m.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.Connect(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.Init()
|
||||
m.Connect(ctx)
|
||||
wc, err := m.Watch(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("cant watch: %v", err)
|
||||
}
|
||||
defer wc.Stop()
|
||||
|
||||
cherr := make(chan error, 10)
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
for {
|
||||
_, err := wc.Next()
|
||||
if err != nil {
|
||||
cherr <- fmt.Errorf("unexpected err %v", err)
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
// t.Logf("changes %#+v", ch.Service)
|
||||
wc.Stop()
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
if err := m.Register(ctx, testSrv); err != nil {
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"crypto/tls"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
@ -27,8 +26,6 @@ type Options struct {
|
||||
Name string
|
||||
// Addrs specifies register addrs
|
||||
Addrs []string
|
||||
// Codec used to marshal/unmarshal data in register
|
||||
Codec codec.Codec
|
||||
// Timeout specifies timeout
|
||||
Timeout time.Duration
|
||||
}
|
||||
@ -40,7 +37,6 @@ func NewOptions(opts ...Option) Options {
|
||||
Meter: meter.DefaultMeter,
|
||||
Tracer: tracer.DefaultTracer,
|
||||
Context: context.Background(),
|
||||
Codec: codec.NewCodec(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
@ -51,7 +47,7 @@ func NewOptions(opts ...Option) Options {
|
||||
// RegisterOptions holds options for register method
|
||||
type RegisterOptions struct { // nolint: golint,revive
|
||||
Context context.Context
|
||||
Namespace string
|
||||
Domain string
|
||||
TTL time.Duration
|
||||
Attempts int
|
||||
}
|
||||
@ -59,7 +55,7 @@ type RegisterOptions struct { // nolint: golint,revive
|
||||
// NewRegisterOptions returns register options struct filled by opts
|
||||
func NewRegisterOptions(opts ...RegisterOption) RegisterOptions {
|
||||
options := RegisterOptions{
|
||||
Namespace: DefaultNamespace,
|
||||
Domain: DefaultDomain,
|
||||
Context: context.Background(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
@ -76,14 +72,14 @@ type WatchOptions struct {
|
||||
// Other options for implementations of the interface
|
||||
// can be stored in a context
|
||||
Context context.Context
|
||||
// Namespace to watch
|
||||
Namespace string
|
||||
// Domain to watch
|
||||
Domain string
|
||||
}
|
||||
|
||||
// NewWatchOptions returns watch options filled by opts
|
||||
func NewWatchOptions(opts ...WatchOption) WatchOptions {
|
||||
options := WatchOptions{
|
||||
Namespace: DefaultNamespace,
|
||||
Domain: DefaultDomain,
|
||||
Context: context.Background(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
@ -95,8 +91,8 @@ func NewWatchOptions(opts ...WatchOption) WatchOptions {
|
||||
// DeregisterOptions holds options for deregister method
|
||||
type DeregisterOptions struct {
|
||||
Context context.Context
|
||||
// Namespace the service was registered in
|
||||
Namespace string
|
||||
// Domain the service was registered in
|
||||
Domain string
|
||||
// Atempts specify max attempts for deregister
|
||||
Attempts int
|
||||
}
|
||||
@ -104,7 +100,7 @@ type DeregisterOptions struct {
|
||||
// NewDeregisterOptions returns options for deregister filled by opts
|
||||
func NewDeregisterOptions(opts ...DeregisterOption) DeregisterOptions {
|
||||
options := DeregisterOptions{
|
||||
Namespace: DefaultNamespace,
|
||||
Domain: DefaultDomain,
|
||||
Context: context.Background(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
@ -116,14 +112,14 @@ func NewDeregisterOptions(opts ...DeregisterOption) DeregisterOptions {
|
||||
// LookupOptions holds lookup options
|
||||
type LookupOptions struct {
|
||||
Context context.Context
|
||||
// Namespace to scope the request to
|
||||
Namespace string
|
||||
// Domain to scope the request to
|
||||
Domain string
|
||||
}
|
||||
|
||||
// NewLookupOptions returns lookup options filled by opts
|
||||
func NewLookupOptions(opts ...LookupOption) LookupOptions {
|
||||
options := LookupOptions{
|
||||
Namespace: DefaultNamespace,
|
||||
Domain: DefaultDomain,
|
||||
Context: context.Background(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
@ -134,16 +130,15 @@ func NewLookupOptions(opts ...LookupOption) LookupOptions {
|
||||
|
||||
// ListOptions holds the list options for list method
|
||||
type ListOptions struct {
|
||||
// Context used to store additional options
|
||||
Context context.Context
|
||||
// Namespace to scope the request to
|
||||
Namespace string
|
||||
// Domain to scope the request to
|
||||
Domain string
|
||||
}
|
||||
|
||||
// NewListOptions returns list options filled by opts
|
||||
func NewListOptions(opts ...ListOption) ListOptions {
|
||||
options := ListOptions{
|
||||
Namespace: DefaultNamespace,
|
||||
Domain: DefaultDomain,
|
||||
Context: context.Background(),
|
||||
}
|
||||
for _, o := range opts {
|
||||
@ -222,10 +217,10 @@ func RegisterContext(ctx context.Context) RegisterOption { // nolint: golint,rev
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterNamespace secifies register Namespace
|
||||
func RegisterNamespace(d string) RegisterOption { // nolint: golint,revive
|
||||
// RegisterDomain secifies register domain
|
||||
func RegisterDomain(d string) RegisterOption { // nolint: golint,revive
|
||||
return func(o *RegisterOptions) {
|
||||
o.Namespace = d
|
||||
o.Domain = d
|
||||
}
|
||||
}
|
||||
|
||||
@ -243,10 +238,10 @@ func WatchContext(ctx context.Context) WatchOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WatchNamespace sets the Namespace for watch
|
||||
func WatchNamespace(d string) WatchOption {
|
||||
// WatchDomain sets the domain for watch
|
||||
func WatchDomain(d string) WatchOption {
|
||||
return func(o *WatchOptions) {
|
||||
o.Namespace = d
|
||||
o.Domain = d
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,10 +259,10 @@ func DeregisterContext(ctx context.Context) DeregisterOption {
|
||||
}
|
||||
}
|
||||
|
||||
// DeregisterNamespace specifies deregister Namespace
|
||||
func DeregisterNamespace(d string) DeregisterOption {
|
||||
// DeregisterDomain specifies deregister domain
|
||||
func DeregisterDomain(d string) DeregisterOption {
|
||||
return func(o *DeregisterOptions) {
|
||||
o.Namespace = d
|
||||
o.Domain = d
|
||||
}
|
||||
}
|
||||
|
||||
@ -278,10 +273,10 @@ func LookupContext(ctx context.Context) LookupOption {
|
||||
}
|
||||
}
|
||||
|
||||
// LookupNamespace sets the Namespace for lookup
|
||||
func LookupNamespace(d string) LookupOption {
|
||||
// LookupDomain sets the domain for lookup
|
||||
func LookupDomain(d string) LookupOption {
|
||||
return func(o *LookupOptions) {
|
||||
o.Namespace = d
|
||||
o.Domain = d
|
||||
}
|
||||
}
|
||||
|
||||
@ -292,10 +287,10 @@ func ListContext(ctx context.Context) ListOption {
|
||||
}
|
||||
}
|
||||
|
||||
// ListNamespace sets the Namespace for list method
|
||||
func ListNamespace(d string) ListOption {
|
||||
// ListDomain sets the domain for list method
|
||||
func ListDomain(d string) ListOption {
|
||||
return func(o *ListOptions) {
|
||||
o.Namespace = d
|
||||
o.Domain = d
|
||||
}
|
||||
}
|
||||
|
||||
@ -305,9 +300,3 @@ func Name(n string) Option {
|
||||
o.Name = n
|
||||
}
|
||||
}
|
||||
|
||||
func Codec(c codec.Codec) Option {
|
||||
return func(o *Options) {
|
||||
o.Codec = c
|
||||
}
|
||||
}
|
||||
|
@ -9,16 +9,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// WildcardNamespace indicates any Namespace
|
||||
WildcardNamespace = "*"
|
||||
// WildcardDomain indicates any domain
|
||||
WildcardDomain = "*"
|
||||
)
|
||||
|
||||
// DefaultNamespace to use if none was provided in options
|
||||
var DefaultNamespace = "micro"
|
||||
// DefaultDomain to use if none was provided in options
|
||||
var DefaultDomain = "micro"
|
||||
|
||||
var (
|
||||
// DefaultRegister is the global default register
|
||||
DefaultRegister = NewRegister()
|
||||
DefaultRegister Register = NewRegister()
|
||||
// ErrNotFound returned when LookupService is called and no services found
|
||||
ErrNotFound = errors.New("service not found")
|
||||
// ErrWatcherStopped returned when when watcher is stopped
|
||||
@ -59,17 +59,26 @@ type Register interface {
|
||||
|
||||
// Service holds service register info
|
||||
type Service struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Nodes []*Node `json:"nodes,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Metadata metadata.Metadata `json:"metadata"`
|
||||
Endpoints []*Endpoint `json:"endpoints"`
|
||||
Nodes []*Node `json:"nodes"`
|
||||
}
|
||||
|
||||
// Node holds node register info
|
||||
type Node struct {
|
||||
Metadata metadata.Metadata `json:"metadata,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Metadata metadata.Metadata `json:"metadata"`
|
||||
ID string `json:"id"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
// Endpoint holds endpoint register info
|
||||
type Endpoint struct {
|
||||
Request string `json:"request"`
|
||||
Response string `json:"response"`
|
||||
Metadata metadata.Metadata `json:"metadata"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// Option func signature
|
||||
|
@ -15,31 +15,31 @@ type Watcher interface {
|
||||
// the watcher. Actions can be create, update, delete
|
||||
type Result struct {
|
||||
// Service holds register service
|
||||
Service *Service `json:"service,omitempty"`
|
||||
Service *Service
|
||||
// Action holds the action
|
||||
Action EventType `json:"action,omitempty"`
|
||||
Action string
|
||||
}
|
||||
|
||||
// EventType defines register event type
|
||||
type EventType int
|
||||
|
||||
const (
|
||||
// EventCreate is emitted when a new service is registered
|
||||
EventCreate EventType = iota
|
||||
// EventDelete is emitted when an existing service is deregistered
|
||||
EventDelete
|
||||
// EventUpdate is emitted when an existing service is updated
|
||||
EventUpdate
|
||||
// Create is emitted when a new service is registered
|
||||
Create EventType = iota
|
||||
// Delete is emitted when an existing service is deregistered
|
||||
Delete
|
||||
// Update is emitted when an existing service is updated
|
||||
Update
|
||||
)
|
||||
|
||||
// String returns human readable event type
|
||||
func (t EventType) String() string {
|
||||
switch t {
|
||||
case EventCreate:
|
||||
case Create:
|
||||
return "create"
|
||||
case EventDelete:
|
||||
case Delete:
|
||||
return "delete"
|
||||
case EventUpdate:
|
||||
case Update:
|
||||
return "update"
|
||||
default:
|
||||
return "unknown"
|
||||
@ -49,11 +49,11 @@ func (t EventType) String() string {
|
||||
// Event is register event
|
||||
type Event struct {
|
||||
// Timestamp is event timestamp
|
||||
Timestamp time.Time `json:"timestamp,omitempty"`
|
||||
Timestamp time.Time
|
||||
// Service is register service
|
||||
Service *Service `json:"service,omitempty"`
|
||||
Service *Service
|
||||
// ID is register id
|
||||
ID string `json:"id,omitempty"`
|
||||
ID string
|
||||
// Type defines type of event
|
||||
Type EventType `json:"type,omitempty"`
|
||||
Type EventType
|
||||
}
|
||||
|
@ -9,6 +9,6 @@ import (
|
||||
type Resolver struct{}
|
||||
|
||||
// Resolve returns the list of nodes
|
||||
func (r *Resolver) Resolve(_ string) ([]*resolver.Record, error) {
|
||||
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
||||
return []*resolver.Record{}, nil
|
||||
}
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Router, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext get router from context
|
||||
func MustContext(ctx context.Context) Router {
|
||||
r, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing router")
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// NewContext put router in context
|
||||
func NewContext(ctx context.Context, c Router) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -80,7 +80,7 @@ func Name(n string) Option {
|
||||
// NewOptions returns router default options
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
ID: id.MustNew(),
|
||||
ID: id.Must(),
|
||||
Network: DefaultNetwork,
|
||||
Register: register.DefaultRegister,
|
||||
Logger: logger.DefaultLogger,
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
|
||||
var (
|
||||
// DefaultRouter is the global default router
|
||||
DefaultRouter = NewRouter()
|
||||
DefaultRouter Router = NewRouter()
|
||||
// DefaultNetwork is default micro network
|
||||
DefaultNetwork = "micro"
|
||||
// ErrRouteNotFound is returned when no route was found in the routing table
|
||||
|
@ -25,7 +25,7 @@ func (r *random) Select(routes []string, opts ...selector.SelectOption) (selecto
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *random) Record(_ string, _ error) error {
|
||||
func (r *random) Record(addr string, err error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -6,14 +6,14 @@ import (
|
||||
)
|
||||
|
||||
// NewSelector returns an initialised round robin selector
|
||||
func NewSelector(_ ...selector.Option) selector.Selector {
|
||||
func NewSelector(opts ...selector.Option) selector.Selector {
|
||||
return new(roundrobin)
|
||||
}
|
||||
|
||||
type roundrobin struct{}
|
||||
|
||||
// Select return routes based on algo
|
||||
func (r *roundrobin) Select(routes []string, _ ...selector.SelectOption) (selector.Next, error) {
|
||||
func (r *roundrobin) Select(routes []string, opts ...selector.SelectOption) (selector.Next, error) {
|
||||
if len(routes) == 0 {
|
||||
return nil, selector.ErrNoneAvailable
|
||||
}
|
||||
@ -28,7 +28,7 @@ func (r *roundrobin) Select(routes []string, _ ...selector.SelectOption) (select
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *roundrobin) Record(_ string, _ error) error { return nil }
|
||||
func (r *roundrobin) Record(addr string, err error) error { return nil }
|
||||
|
||||
func (r *roundrobin) Reset() error { return nil }
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
package semconv
|
||||
|
||||
var (
|
||||
// CacheRequestDurationSeconds specifies meter metric name
|
||||
CacheRequestDurationSeconds = "micro_cache_request_duration_seconds"
|
||||
// CacheRequestLatencyMicroseconds specifies meter metric name
|
||||
CacheRequestLatencyMicroseconds = "micro_cache_request_latency_microseconds"
|
||||
// CacheRequestTotal specifies meter metric name
|
||||
CacheRequestTotal = "micro_cache_request_total"
|
||||
// CacheRequestInflight specifies meter metric name
|
||||
CacheRequestInflight = "micro_cache_request_inflight"
|
||||
// CacheItemsTotal specifies total cache items
|
||||
CacheItemsTotal = "micro_cache_items_total"
|
||||
)
|
@ -1,18 +0,0 @@
|
||||
package semconv
|
||||
|
||||
var (
|
||||
// HeaderTopic is the header name that contains topic name
|
||||
HeaderTopic = "Micro-Topic"
|
||||
// HeaderContentType specifies content type of message
|
||||
HeaderContentType = "Content-Type"
|
||||
// HeaderEndpoint specifies endpoint in service
|
||||
HeaderEndpoint = "Micro-Endpoint"
|
||||
// HeaderService specifies service
|
||||
HeaderService = "Micro-Service"
|
||||
// HeaderTimeout specifies timeout of operation
|
||||
HeaderTimeout = "Micro-Timeout"
|
||||
// HeaderAuthorization specifies Authorization header
|
||||
HeaderAuthorization = "Authorization"
|
||||
// HeaderXRequestID specifies request id
|
||||
HeaderXRequestID = "X-Request-Id"
|
||||
)
|
@ -3,7 +3,7 @@ package semconv
|
||||
var (
|
||||
// StoreRequestDurationSeconds specifies meter metric name
|
||||
StoreRequestDurationSeconds = "micro_store_request_duration_seconds"
|
||||
// StoreRequestLatencyMicroseconds specifies meter metric name
|
||||
// ClientRequestLatencyMicroseconds specifies meter metric name
|
||||
StoreRequestLatencyMicroseconds = "micro_store_request_latency_microseconds"
|
||||
// StoreRequestTotal specifies meter metric name
|
||||
StoreRequestTotal = "micro_store_request_total"
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Server, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext returns Server from context
|
||||
func MustContext(ctx context.Context) Server {
|
||||
s, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing server")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// NewContext stores Server to context
|
||||
func NewContext(ctx context.Context, s Server) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -34,17 +35,34 @@ type rpcHandler struct {
|
||||
opts HandlerOptions
|
||||
handler interface{}
|
||||
name string
|
||||
endpoints []*register.Endpoint
|
||||
}
|
||||
|
||||
func newRPCHandler(handler interface{}, opts ...HandlerOption) Handler {
|
||||
options := NewHandlerOptions(opts...)
|
||||
|
||||
typ := reflect.TypeOf(handler)
|
||||
hdlr := reflect.ValueOf(handler)
|
||||
name := reflect.Indirect(hdlr).Type().Name()
|
||||
|
||||
var endpoints []*register.Endpoint
|
||||
|
||||
for m := 0; m < typ.NumMethod(); m++ {
|
||||
if e := register.ExtractEndpoint(typ.Method(m)); e != nil {
|
||||
e.Name = name + "." + e.Name
|
||||
|
||||
for k, v := range options.Metadata[e.Name] {
|
||||
e.Metadata[k] = v
|
||||
}
|
||||
|
||||
endpoints = append(endpoints, e)
|
||||
}
|
||||
}
|
||||
|
||||
return &rpcHandler{
|
||||
name: name,
|
||||
handler: handler,
|
||||
endpoints: endpoints,
|
||||
opts: options,
|
||||
}
|
||||
}
|
||||
@ -57,6 +75,10 @@ func (r *rpcHandler) Handler() interface{} {
|
||||
return r.handler
|
||||
}
|
||||
|
||||
func (r *rpcHandler) Endpoints() []*register.Endpoint {
|
||||
return r.endpoints
|
||||
}
|
||||
|
||||
func (r *rpcHandler) Options() HandlerOptions {
|
||||
return r.opts
|
||||
}
|
||||
@ -149,6 +171,7 @@ type rpcMessage struct {
|
||||
header metadata.Metadata
|
||||
topic string
|
||||
contentType string
|
||||
body []byte
|
||||
}
|
||||
|
||||
func (r *rpcMessage) ContentType() string {
|
||||
@ -227,6 +250,35 @@ func (n *noopServer) Register() error {
|
||||
return err
|
||||
}
|
||||
|
||||
n.RLock()
|
||||
handlerList := make([]string, 0, len(n.handlers))
|
||||
for n := range n.handlers {
|
||||
handlerList = append(handlerList, n)
|
||||
}
|
||||
|
||||
sort.Strings(handlerList)
|
||||
|
||||
subscriberList := make([]*subscriber, 0, len(n.subscribers))
|
||||
for e := range n.subscribers {
|
||||
subscriberList = append(subscriberList, e)
|
||||
}
|
||||
sort.Slice(subscriberList, func(i, j int) bool {
|
||||
return subscriberList[i].topic > subscriberList[j].topic
|
||||
})
|
||||
|
||||
endpoints := make([]*register.Endpoint, 0, len(handlerList)+len(subscriberList))
|
||||
for _, h := range handlerList {
|
||||
endpoints = append(endpoints, n.handlers[h].Endpoints()...)
|
||||
}
|
||||
for _, e := range subscriberList {
|
||||
endpoints = append(endpoints, e.Endpoints()...)
|
||||
}
|
||||
n.RUnlock()
|
||||
|
||||
service.Nodes[0].Metadata["protocol"] = "noop"
|
||||
service.Nodes[0].Metadata["transport"] = service.Nodes[0].Metadata["protocol"]
|
||||
service.Endpoints = endpoints
|
||||
|
||||
n.RLock()
|
||||
registered := n.registered
|
||||
n.RUnlock()
|
||||
@ -525,6 +577,7 @@ func (n *noopServer) Stop() error {
|
||||
}
|
||||
|
||||
func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subscriber {
|
||||
var endpoints []*register.Endpoint
|
||||
var handlers []*handler
|
||||
|
||||
options := NewSubscriberOptions(opts...)
|
||||
@ -543,7 +596,18 @@ func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subs
|
||||
}
|
||||
|
||||
handlers = append(handlers, h)
|
||||
ep := ®ister.Endpoint{
|
||||
Name: "Func",
|
||||
Request: register.ExtractSubValue(typ),
|
||||
Metadata: metadata.New(2),
|
||||
}
|
||||
ep.Metadata.Set("topic", topic)
|
||||
ep.Metadata.Set("subscriber", "true")
|
||||
endpoints = append(endpoints, ep)
|
||||
} else {
|
||||
hdlr := reflect.ValueOf(sub)
|
||||
name := reflect.Indirect(hdlr).Type().Name()
|
||||
|
||||
for m := 0; m < typ.NumMethod(); m++ {
|
||||
method := typ.Method(m)
|
||||
h := &handler{
|
||||
@ -559,6 +623,14 @@ func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subs
|
||||
}
|
||||
|
||||
handlers = append(handlers, h)
|
||||
ep := ®ister.Endpoint{
|
||||
Name: name + "." + method.Name,
|
||||
Request: register.ExtractSubValue(method.Type),
|
||||
Metadata: metadata.New(2),
|
||||
}
|
||||
ep.Metadata.Set("topic", topic)
|
||||
ep.Metadata.Set("subscriber", "true")
|
||||
endpoints = append(endpoints, ep)
|
||||
}
|
||||
}
|
||||
|
||||
@ -568,6 +640,7 @@ func newSubscriber(topic string, sub interface{}, opts ...SubscriberOption) Subs
|
||||
topic: topic,
|
||||
subscriber: sub,
|
||||
handlers: handlers,
|
||||
endpoints: endpoints,
|
||||
opts: options,
|
||||
}
|
||||
}
|
||||
@ -651,7 +724,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
|
||||
return nil
|
||||
}
|
||||
|
||||
opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
if h, ok := hook.(HookSubHandler); ok {
|
||||
fn = h(fn)
|
||||
}
|
||||
@ -694,20 +767,22 @@ func (s *subscriber) Subscriber() interface{} {
|
||||
return s.subscriber
|
||||
}
|
||||
|
||||
func (s *subscriber) Endpoints() []*register.Endpoint {
|
||||
return s.endpoints
|
||||
}
|
||||
|
||||
func (s *subscriber) Options() SubscriberOptions {
|
||||
return s.opts
|
||||
}
|
||||
|
||||
type subscriber struct {
|
||||
topic string
|
||||
|
||||
typ reflect.Type
|
||||
subscriber interface{}
|
||||
|
||||
topic string
|
||||
endpoints []*register.Endpoint
|
||||
handlers []*handler
|
||||
|
||||
rcvr reflect.Value
|
||||
opts SubscriberOptions
|
||||
rcvr reflect.Value
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"go.unistack.org/micro/v3/client"
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/micro/v3/options"
|
||||
"go.unistack.org/micro/v3/server"
|
||||
)
|
||||
|
||||
@ -39,9 +38,7 @@ func TestNoopSub(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logger.DefaultLogger.Init(logger.WithLevel(logger.ErrorLevel))
|
||||
s := server.NewServer(
|
||||
server.Broker(b),
|
||||
server.Codec("application/octet-stream", codec.NewCodec()),
|
||||
@ -85,40 +82,3 @@ func TestNoopSub(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func TestHooks_Wrap(t *testing.T) {
|
||||
n := 5
|
||||
fn1 := func(next server.FuncSubHandler) server.FuncSubHandler {
|
||||
return func(ctx context.Context, msg server.Message) (err error) {
|
||||
n *= 2
|
||||
return next(ctx, msg)
|
||||
}
|
||||
}
|
||||
fn2 := func(next server.FuncSubHandler) server.FuncSubHandler {
|
||||
return func(ctx context.Context, msg server.Message) (err error) {
|
||||
n -= 10
|
||||
return next(ctx, msg)
|
||||
}
|
||||
}
|
||||
|
||||
hs := &options.Hooks{}
|
||||
hs.Append(server.HookSubHandler(fn1), server.HookSubHandler(fn2))
|
||||
|
||||
var fn = func(ctx context.Context, msg server.Message) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
hs.EachPrev(func(hook options.Hook) {
|
||||
if h, ok := hook.(server.HookSubHandler); ok {
|
||||
fn = h(fn)
|
||||
}
|
||||
})
|
||||
|
||||
if err := fn(nil, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if n != 0 {
|
||||
t.Fatalf("uncorrected hooks call")
|
||||
}
|
||||
}
|
||||
|
@ -24,24 +24,6 @@ type Option func(*Options)
|
||||
|
||||
// Options server struct
|
||||
type Options struct {
|
||||
// Codecs map to handle content-type
|
||||
Codecs map[string]codec.Codec
|
||||
// Metadata holds the server metadata
|
||||
Metadata metadata.Metadata
|
||||
|
||||
// ID holds the id of the server
|
||||
ID string
|
||||
// Namespace for te server
|
||||
Namespace string
|
||||
// Name holds the server name
|
||||
Name string
|
||||
// Address holds the server address
|
||||
Address string
|
||||
// Advertise holds the advertise address
|
||||
Advertise string
|
||||
// Version holds the server version
|
||||
Version string
|
||||
|
||||
// Context holds the external options and can be used for server shutdown
|
||||
Context context.Context
|
||||
// Broker holds the server broker
|
||||
@ -54,33 +36,51 @@ type Options struct {
|
||||
Logger logger.Logger
|
||||
// Meter holds the meter
|
||||
Meter meter.Meter
|
||||
|
||||
/*
|
||||
// Router for requests
|
||||
Router Router
|
||||
*/
|
||||
|
||||
// Listener may be passed if already created
|
||||
Listener net.Listener
|
||||
|
||||
// TLSConfig specifies tls.Config for secure serving
|
||||
TLSConfig *tls.Config
|
||||
// Wait group
|
||||
Wait *msync.WaitGroup
|
||||
|
||||
// TLSConfig specifies tls.Config for secure serving
|
||||
TLSConfig *tls.Config
|
||||
// Metadata holds the server metadata
|
||||
Metadata metadata.Metadata
|
||||
// RegisterCheck run before register server
|
||||
RegisterCheck func(context.Context) error
|
||||
|
||||
// Hooks may contains hook actions that performs before/after server handler
|
||||
// or server subscriber handler
|
||||
Hooks options.Hooks
|
||||
|
||||
// Codecs map to handle content-type
|
||||
Codecs map[string]codec.Codec
|
||||
// ID holds the id of the server
|
||||
ID string
|
||||
// Namespace for te server
|
||||
Namespace string
|
||||
// Name holds the server name
|
||||
Name string
|
||||
// Address holds the server address
|
||||
Address string
|
||||
// Advertise holds the advertise address
|
||||
Advertise string
|
||||
// Version holds the server version
|
||||
Version string
|
||||
// RegisterAttempts holds the number of register attempts before error
|
||||
RegisterAttempts int
|
||||
// RegisterInterval holds he interval for re-register
|
||||
RegisterInterval time.Duration
|
||||
// RegisterTTL specifies TTL for register record
|
||||
RegisterTTL time.Duration
|
||||
// GracefulTimeout timeout for graceful stop server
|
||||
GracefulTimeout time.Duration
|
||||
// MaxConn limits number of connections
|
||||
MaxConn int
|
||||
// DeregisterAttempts holds the number of deregister attempts before error
|
||||
DeregisterAttempts int
|
||||
// RegisterAttempts holds the number of register attempts before error
|
||||
RegisterAttempts int
|
||||
// Hooks may contains hook actions that performs before/after server handler
|
||||
// or server subscriber handler
|
||||
Hooks options.Hooks
|
||||
// GracefulTimeout timeout for graceful stop server
|
||||
GracefulTimeout time.Duration
|
||||
}
|
||||
|
||||
// NewOptions returns new options struct with default or passed values
|
||||
@ -100,7 +100,7 @@ func NewOptions(opts ...Option) Options {
|
||||
Address: DefaultAddress,
|
||||
Name: DefaultName,
|
||||
Version: DefaultVersion,
|
||||
ID: id.MustNew(),
|
||||
ID: id.Must(),
|
||||
Namespace: DefaultNamespace,
|
||||
GracefulTimeout: DefaultGracefulTimeout,
|
||||
}
|
||||
@ -318,14 +318,14 @@ type SubscriberOptions struct {
|
||||
Context context.Context
|
||||
// Queue holds the subscription queue
|
||||
Queue string
|
||||
// BatchWait flag specifies max wait time for batch filling
|
||||
BatchWait time.Duration
|
||||
// BatchSize flag specifies max size of batch
|
||||
BatchSize int
|
||||
// AutoAck flag for auto ack messages after processing
|
||||
AutoAck bool
|
||||
// BodyOnly flag specifies that message without headers
|
||||
BodyOnly bool
|
||||
// BatchSize flag specifies max size of batch
|
||||
BatchSize int
|
||||
// BatchWait flag specifies max wait time for batch filling
|
||||
BatchWait time.Duration
|
||||
}
|
||||
|
||||
// NewSubscriberOptions create new SubscriberOptions
|
||||
|
@ -17,7 +17,7 @@ var (
|
||||
|
||||
opts := []register.RegisterOption{
|
||||
register.RegisterTTL(config.RegisterTTL),
|
||||
register.RegisterNamespace(config.Namespace),
|
||||
register.RegisterDomain(config.Namespace),
|
||||
}
|
||||
|
||||
for i := 0; i <= config.RegisterAttempts; i++ {
|
||||
@ -36,7 +36,7 @@ var (
|
||||
var err error
|
||||
|
||||
opts := []register.DeregisterOption{
|
||||
register.DeregisterNamespace(config.Namespace),
|
||||
register.DeregisterDomain(config.Namespace),
|
||||
}
|
||||
|
||||
for i := 0; i <= config.DeregisterAttempts; i++ {
|
||||
@ -85,5 +85,6 @@ func NewRegisterService(s Server) (*register.Service, error) {
|
||||
Name: opts.Name,
|
||||
Version: opts.Version,
|
||||
Nodes: []*register.Node{node},
|
||||
Metadata: metadata.New(0),
|
||||
}, nil
|
||||
}
|
||||
|
@ -7,11 +7,12 @@ import (
|
||||
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/metadata"
|
||||
"go.unistack.org/micro/v3/register"
|
||||
)
|
||||
|
||||
// DefaultServer default server
|
||||
var (
|
||||
DefaultServer = NewServer()
|
||||
DefaultServer Server = NewServer()
|
||||
)
|
||||
|
||||
var (
|
||||
@ -169,6 +170,7 @@ type Stream interface {
|
||||
type Handler interface {
|
||||
Name() string
|
||||
Handler() interface{}
|
||||
Endpoints() []*register.Endpoint
|
||||
Options() HandlerOptions
|
||||
}
|
||||
|
||||
@ -178,5 +180,6 @@ type Handler interface {
|
||||
type Subscriber interface {
|
||||
Topic() string
|
||||
Subscriber() interface{}
|
||||
Endpoints() []*register.Endpoint
|
||||
Options() SubscriberOptions
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
_, _ = maxprocs.Set()
|
||||
_, _ = memlimit.SetGoMemLimitWithOpts(
|
||||
maxprocs.Set()
|
||||
memlimit.SetGoMemLimitWithOpts(
|
||||
memlimit.WithRatio(0.9),
|
||||
memlimit.WithProvider(
|
||||
memlimit.ApplyFallback(
|
||||
@ -34,10 +34,7 @@ func init() {
|
||||
),
|
||||
)
|
||||
|
||||
net.DefaultResolver = utildns.NewNetResolver(
|
||||
utildns.Timeout(1*time.Second),
|
||||
utildns.MinCacheTTL(5*time.Second),
|
||||
)
|
||||
net.DefaultResolver = utildns.NewNetResolver(utildns.Timeout(1 * time.Second))
|
||||
}
|
||||
|
||||
// Service is an interface that wraps the lower level components.
|
||||
|
@ -121,10 +121,8 @@ func TestNewService(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want Service
|
||||
|
||||
args args
|
||||
want Service
|
||||
}{
|
||||
{
|
||||
name: "NewService",
|
||||
@ -149,9 +147,8 @@ func Test_service_Name(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Test_service_Name",
|
||||
@ -249,11 +246,9 @@ func Test_service_Broker(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want broker.Broker
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want broker.Broker
|
||||
}{
|
||||
{
|
||||
name: "service.Broker",
|
||||
@ -307,11 +302,9 @@ func Test_service_Tracer(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want tracer.Tracer
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want tracer.Tracer
|
||||
}{
|
||||
{
|
||||
name: "service.Tracer",
|
||||
@ -346,10 +339,9 @@ func Test_service_Config(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
want config.Config
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want config.Config
|
||||
}{
|
||||
{
|
||||
name: "service.Config",
|
||||
@ -384,11 +376,9 @@ func Test_service_Client(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want client.Client
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want client.Client
|
||||
}{
|
||||
{
|
||||
name: "service.Client",
|
||||
@ -423,11 +413,9 @@ func Test_service_Server(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want server.Server
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want server.Server
|
||||
}{
|
||||
{
|
||||
name: "service.Server",
|
||||
@ -462,11 +450,9 @@ func Test_service_Store(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want store.Store
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want store.Store
|
||||
}{
|
||||
{
|
||||
name: "service.Store",
|
||||
@ -501,11 +487,9 @@ func Test_service_Register(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want register.Register
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want register.Register
|
||||
}{
|
||||
{
|
||||
name: "service.Register",
|
||||
@ -540,11 +524,9 @@ func Test_service_Logger(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want logger.Logger
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want logger.Logger
|
||||
}{
|
||||
{
|
||||
name: "service.Logger",
|
||||
@ -579,11 +561,9 @@ func Test_service_Router(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want router.Router
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want router.Router
|
||||
}{
|
||||
{
|
||||
name: "service.Router",
|
||||
@ -618,11 +598,9 @@ func Test_service_Meter(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
want meter.Meter
|
||||
|
||||
fields fields
|
||||
args args
|
||||
want meter.Meter
|
||||
}{
|
||||
{
|
||||
name: "service.Meter",
|
||||
@ -653,8 +631,8 @@ func Test_service_String(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
fields fields
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "service.String",
|
||||
|
@ -15,15 +15,6 @@ func FromContext(ctx context.Context) (Store, bool) {
|
||||
return c, ok
|
||||
}
|
||||
|
||||
// MustContext get store from context
|
||||
func MustContext(ctx context.Context) Store {
|
||||
s, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing store")
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// NewContext put store in context
|
||||
func NewContext(ctx context.Context, c Store) context.Context {
|
||||
if ctx == nil {
|
||||
|
@ -96,15 +96,15 @@ func (m *memoryStore) list(prefix string, limit, offset uint) []string {
|
||||
if limit != 0 || offset != 0 {
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
end := uint(len(allKeys))
|
||||
end := len(allKeys)
|
||||
if limit > 0 {
|
||||
calcLimit := offset + limit
|
||||
calcLimit := int(offset + limit)
|
||||
if calcLimit < end {
|
||||
end = calcLimit
|
||||
}
|
||||
}
|
||||
|
||||
if offset >= end {
|
||||
if int(offset) >= end {
|
||||
return nil
|
||||
}
|
||||
return allKeys[offset:end]
|
||||
@ -123,7 +123,7 @@ func (m *memoryStore) Init(opts ...store.Option) error {
|
||||
m.funcList = m.fnList
|
||||
m.funcDelete = m.fnDelete
|
||||
|
||||
m.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
m.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case store.HookRead:
|
||||
m.funcRead = h(m.funcRead)
|
||||
|
@ -12,18 +12,15 @@ import (
|
||||
var _ Store = (*noopStore)(nil)
|
||||
|
||||
type noopStore struct {
|
||||
mu sync.Mutex
|
||||
watchers map[string]Watcher
|
||||
|
||||
funcRead FuncRead
|
||||
funcWrite FuncWrite
|
||||
funcExists FuncExists
|
||||
funcList FuncList
|
||||
funcDelete FuncDelete
|
||||
|
||||
opts Options
|
||||
isConnected atomic.Int32
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (n *noopStore) Live() bool {
|
||||
@ -38,7 +35,7 @@ func (n *noopStore) Health() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func NewStore(opts ...Option) Store {
|
||||
func NewStore(opts ...Option) *noopStore {
|
||||
options := NewOptions(opts...)
|
||||
return &noopStore{opts: options}
|
||||
}
|
||||
@ -54,7 +51,7 @@ func (n *noopStore) Init(opts ...Option) error {
|
||||
n.funcList = n.fnList
|
||||
n.funcDelete = n.fnDelete
|
||||
|
||||
n.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||
n.opts.Hooks.EachNext(func(hook options.Hook) {
|
||||
switch h := hook.(type) {
|
||||
case HookRead:
|
||||
n.funcRead = h(n.funcRead)
|
||||
@ -97,7 +94,7 @@ func (n *noopStore) Read(ctx context.Context, key string, val interface{}, opts
|
||||
return n.funcRead(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
func (n *noopStore) fnRead(ctx context.Context, _ string, _ interface{}, _ ...ReadOption) error {
|
||||
func (n *noopStore) fnRead(ctx context.Context, key string, val interface{}, opts ...ReadOption) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
@ -115,7 +112,7 @@ func (n *noopStore) Delete(ctx context.Context, key string, opts ...DeleteOption
|
||||
return n.funcDelete(ctx, key, opts...)
|
||||
}
|
||||
|
||||
func (n *noopStore) fnDelete(ctx context.Context, _ string, _ ...DeleteOption) error {
|
||||
func (n *noopStore) fnDelete(ctx context.Context, key string, opts ...DeleteOption) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
@ -133,7 +130,7 @@ func (n *noopStore) Exists(ctx context.Context, key string, opts ...ExistsOption
|
||||
return n.funcExists(ctx, key, opts...)
|
||||
}
|
||||
|
||||
func (n *noopStore) fnExists(ctx context.Context, _ string, _ ...ExistsOption) error {
|
||||
func (n *noopStore) fnExists(ctx context.Context, key string, opts ...ExistsOption) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
@ -151,7 +148,7 @@ func (n *noopStore) Write(ctx context.Context, key string, val interface{}, opts
|
||||
return n.funcWrite(ctx, key, val, opts...)
|
||||
}
|
||||
|
||||
func (n *noopStore) fnWrite(ctx context.Context, _ string, _ interface{}, _ ...WriteOption) error {
|
||||
func (n *noopStore) fnWrite(ctx context.Context, key string, val interface{}, opts ...WriteOption) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
@ -203,13 +200,13 @@ func (n *noopStore) connect(ctx context.Context) error {
|
||||
}
|
||||
|
||||
type watcher struct {
|
||||
opts WatchOptions
|
||||
ch chan Event
|
||||
exit chan bool
|
||||
id string
|
||||
ch chan Event
|
||||
opts WatchOptions
|
||||
}
|
||||
|
||||
func (n *noopStore) Watch(_ context.Context, opts ...WatchOption) (Watcher, error) {
|
||||
func (m *noopStore) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) {
|
||||
id, err := id.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -226,9 +223,9 @@ func (n *noopStore) Watch(_ context.Context, opts ...WatchOption) (Watcher, erro
|
||||
opts: wo,
|
||||
}
|
||||
|
||||
n.mu.Lock()
|
||||
n.watchers[w.id] = w
|
||||
n.mu.Unlock()
|
||||
m.mu.Lock()
|
||||
m.watchers[w.id] = w
|
||||
m.mu.Unlock()
|
||||
|
||||
return w, nil
|
||||
}
|
||||
|
@ -15,13 +15,6 @@ import (
|
||||
|
||||
// Options contains configuration for the Store
|
||||
type Options struct {
|
||||
// Name specifies store name
|
||||
Name string
|
||||
// Namespace of the records
|
||||
Namespace string
|
||||
// Separator used as key parts separator
|
||||
Separator string
|
||||
|
||||
// Meter used for metrics
|
||||
Meter meter.Meter
|
||||
// Tracer used for tracing
|
||||
@ -32,17 +25,22 @@ type Options struct {
|
||||
Codec codec.Codec
|
||||
// Logger used for logging
|
||||
Logger logger.Logger
|
||||
|
||||
// TLSConfig holds tls.TLSConfig options
|
||||
TLSConfig *tls.Config
|
||||
|
||||
// Name specifies store name
|
||||
Name string
|
||||
// Namespace of the records
|
||||
Namespace string
|
||||
// Separator used as key parts separator
|
||||
Separator string
|
||||
// Addrs contains store address
|
||||
Addrs []string
|
||||
// Hooks can be run before/after store Read/List/Write/Exists/Delete
|
||||
Hooks options.Hooks
|
||||
|
||||
// Wrappers store wrapper that called before actual functions
|
||||
// Wrappers []Wrapper
|
||||
// Timeout specifies timeout duration for all operations
|
||||
Timeout time.Duration
|
||||
// Hooks can be run before/after store Read/List/Write/Exists/Delete
|
||||
Hooks options.Hooks
|
||||
// LazyConnect creates a connection when using store
|
||||
LazyConnect bool
|
||||
}
|
||||
|
@ -10,13 +10,13 @@ import (
|
||||
var (
|
||||
ErrWatcherStopped = errors.New("watcher stopped")
|
||||
// ErrNotConnected is returned when a store is not connected
|
||||
ErrNotConnected = errors.New("not connected")
|
||||
ErrNotConnected = errors.New("not conected")
|
||||
// ErrNotFound is returned when a key doesn't exist
|
||||
ErrNotFound = errors.New("not found")
|
||||
// ErrInvalidKey is returned when a key has empty or have invalid format
|
||||
ErrInvalidKey = errors.New("invalid key")
|
||||
// DefaultStore is the global default store
|
||||
DefaultStore = NewStore()
|
||||
DefaultStore Store = NewStore()
|
||||
// DefaultSeparator is the gloabal default key parts separator
|
||||
DefaultSeparator = "/"
|
||||
)
|
||||
@ -105,7 +105,3 @@ func NewWatchOptions(opts ...WatchOption) (WatchOptions, error) {
|
||||
|
||||
return options, err
|
||||
}
|
||||
|
||||
func Watch(context.Context) (Watcher, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -6,10 +6,9 @@ import (
|
||||
)
|
||||
|
||||
type memorySync struct {
|
||||
mtx gosync.RWMutex
|
||||
locks map[string]*memoryLock
|
||||
options Options
|
||||
|
||||
mtx gosync.RWMutex
|
||||
}
|
||||
|
||||
type memoryLock struct {
|
||||
|
@ -18,15 +18,6 @@ func FromContext(ctx context.Context) (Tracer, bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// MustContext returns a tracer from context
|
||||
func MustContext(ctx context.Context) Tracer {
|
||||
t, ok := FromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing tracer")
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// NewContext saves the tracer in the context
|
||||
func NewContext(ctx context.Context, tracer Tracer) context.Context {
|
||||
if ctx == nil {
|
||||
@ -37,15 +28,6 @@ func NewContext(ctx context.Context, tracer Tracer) context.Context {
|
||||
|
||||
type spanKey struct{}
|
||||
|
||||
// SpanFromContext returns a span from context
|
||||
func SpanMustContext(ctx context.Context) Span {
|
||||
sp, ok := SpanFromContext(ctx)
|
||||
if !ok {
|
||||
panic("missing span")
|
||||
}
|
||||
return sp
|
||||
}
|
||||
|
||||
// SpanFromContext returns a span from context
|
||||
func SpanFromContext(ctx context.Context) (Span, bool) {
|
||||
if ctx == nil {
|
||||
|
@ -25,7 +25,6 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOpti
|
||||
name: name,
|
||||
ctx: ctx,
|
||||
tracer: t,
|
||||
labels: options.Labels,
|
||||
kind: options.Kind,
|
||||
startTime: time.Now(),
|
||||
}
|
||||
@ -38,14 +37,6 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...tracer.SpanOpti
|
||||
return tracer.NewSpanContext(ctx, span), span
|
||||
}
|
||||
|
||||
type memoryStringer struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (s memoryStringer) String() string {
|
||||
return s.s
|
||||
}
|
||||
|
||||
func (t *Tracer) Flush(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
@ -61,6 +52,14 @@ func (t *Tracer) Name() string {
|
||||
return t.opts.Name
|
||||
}
|
||||
|
||||
type noopStringer struct {
|
||||
s string
|
||||
}
|
||||
|
||||
func (s noopStringer) String() string {
|
||||
return s.s
|
||||
}
|
||||
|
||||
type Span struct {
|
||||
ctx context.Context
|
||||
tracer tracer.Tracer
|
||||
@ -68,8 +67,8 @@ type Span struct {
|
||||
statusMsg string
|
||||
startTime time.Time
|
||||
finishTime time.Time
|
||||
traceID memoryStringer
|
||||
spanID memoryStringer
|
||||
traceID noopStringer
|
||||
spanID noopStringer
|
||||
events []*Event
|
||||
labels []interface{}
|
||||
logs []interface{}
|
||||
|
@ -2,7 +2,6 @@ package tracer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/util/id"
|
||||
)
|
||||
@ -24,7 +23,6 @@ func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption)
|
||||
name: name,
|
||||
ctx: ctx,
|
||||
tracer: t,
|
||||
startTime: time.Now(),
|
||||
labels: options.Labels,
|
||||
kind: options.Kind,
|
||||
}
|
||||
@ -33,6 +31,7 @@ func (t *noopTracer) Start(ctx context.Context, name string, opts ...SpanOption)
|
||||
if span.ctx == nil {
|
||||
span.ctx = context.Background()
|
||||
}
|
||||
t.spans = append(t.spans, span)
|
||||
return NewSpanContext(ctx, span), span
|
||||
}
|
||||
|
||||
@ -59,16 +58,21 @@ func (t *noopTracer) Name() string {
|
||||
return t.opts.Name
|
||||
}
|
||||
|
||||
type noopEvent struct {
|
||||
name string
|
||||
labels []interface{}
|
||||
}
|
||||
|
||||
type noopSpan struct {
|
||||
ctx context.Context
|
||||
tracer Tracer
|
||||
name string
|
||||
statusMsg string
|
||||
startTime time.Time
|
||||
finishTime time.Time
|
||||
traceID noopStringer
|
||||
spanID noopStringer
|
||||
events []*noopEvent
|
||||
labels []interface{}
|
||||
logs []interface{}
|
||||
kind SpanKind
|
||||
status SpanStatus
|
||||
}
|
||||
@ -82,7 +86,6 @@ func (s *noopSpan) SpanID() string {
|
||||
}
|
||||
|
||||
func (s *noopSpan) Finish(_ ...SpanOption) {
|
||||
s.finishTime = time.Now()
|
||||
}
|
||||
|
||||
func (s *noopSpan) Context() context.Context {
|
||||
@ -94,6 +97,8 @@ func (s *noopSpan) Tracer() Tracer {
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddEvent(name string, opts ...EventOption) {
|
||||
options := NewEventOptions(opts...)
|
||||
s.events = append(s.events, &noopEvent{name: name, labels: options.Labels})
|
||||
}
|
||||
|
||||
func (s *noopSpan) SetName(name string) {
|
||||
@ -101,6 +106,7 @@ func (s *noopSpan) SetName(name string) {
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddLogs(kv ...interface{}) {
|
||||
s.logs = append(s.logs, kv...)
|
||||
}
|
||||
|
||||
func (s *noopSpan) AddLabels(kv ...interface{}) {
|
||||
|
27
util/buf/buf.go
Normal file
27
util/buf/buf.go
Normal file
@ -0,0 +1,27 @@
|
||||
package buf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
var _ io.Closer = &Buffer{}
|
||||
|
||||
// Buffer bytes.Buffer wrapper to satisfie io.Closer interface
|
||||
type Buffer struct {
|
||||
*bytes.Buffer
|
||||
}
|
||||
|
||||
// Close reset buffer contents
|
||||
func (b *Buffer) Close() error {
|
||||
b.Buffer.Reset()
|
||||
return nil
|
||||
}
|
||||
|
||||
// New creates new buffer that satisfies Closer interface
|
||||
func New(b *bytes.Buffer) *Buffer {
|
||||
if b == nil {
|
||||
b = bytes.NewBuffer(nil)
|
||||
}
|
||||
return &Buffer{b}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ io.WriteCloser = (*DelayedBuffer)(nil)
|
||||
|
||||
// DelayedBuffer is the buffer that holds items until either the buffer filled or a specified time limit is reached
|
||||
type DelayedBuffer struct {
|
||||
mu sync.Mutex
|
||||
maxWait time.Duration
|
||||
flushTime time.Time
|
||||
buffer chan []byte
|
||||
ticker *time.Ticker
|
||||
w io.Writer
|
||||
err error
|
||||
}
|
||||
|
||||
func NewDelayedBuffer(size int, maxWait time.Duration, w io.Writer) *DelayedBuffer {
|
||||
b := &DelayedBuffer{
|
||||
buffer: make(chan []byte, size),
|
||||
ticker: time.NewTicker(maxWait),
|
||||
w: w,
|
||||
flushTime: time.Now(),
|
||||
maxWait: maxWait,
|
||||
}
|
||||
b.loop()
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *DelayedBuffer) loop() {
|
||||
go func() {
|
||||
for range b.ticker.C {
|
||||
b.mu.Lock()
|
||||
if time.Since(b.flushTime) > b.maxWait {
|
||||
b.flush()
|
||||
}
|
||||
b.mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (b *DelayedBuffer) flush() {
|
||||
bufLen := len(b.buffer)
|
||||
if bufLen > 0 {
|
||||
tmp := make([][]byte, bufLen)
|
||||
for i := 0; i < bufLen; i++ {
|
||||
tmp[i] = <-b.buffer
|
||||
}
|
||||
for _, t := range tmp {
|
||||
_, b.err = b.w.Write(t)
|
||||
}
|
||||
b.flushTime = time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *DelayedBuffer) Put(items ...[]byte) {
|
||||
b.mu.Lock()
|
||||
for _, item := range items {
|
||||
select {
|
||||
case b.buffer <- item:
|
||||
default:
|
||||
b.flush()
|
||||
b.buffer <- item
|
||||
}
|
||||
}
|
||||
b.mu.Unlock()
|
||||
}
|
||||
|
||||
func (b *DelayedBuffer) Close() error {
|
||||
b.mu.Lock()
|
||||
b.flush()
|
||||
close(b.buffer)
|
||||
b.ticker.Stop()
|
||||
b.mu.Unlock()
|
||||
return b.err
|
||||
}
|
||||
|
||||
func (b *DelayedBuffer) Write(data []byte) (int, error) {
|
||||
b.Put(data)
|
||||
return len(data), b.err
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package buffer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTimedBuffer(t *testing.T) {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
b := NewDelayedBuffer(100, 300*time.Millisecond, buf)
|
||||
for i := 0; i < 100; i++ {
|
||||
_, _ = b.Write([]byte(`test`))
|
||||
}
|
||||
if buf.Len() != 0 {
|
||||
t.Fatal("delayed write not worked")
|
||||
}
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
if buf.Len() == 0 {
|
||||
t.Fatal("delayed write not worked")
|
||||
}
|
||||
}
|
@ -6,9 +6,6 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
"go.unistack.org/micro/v3/semconv"
|
||||
)
|
||||
|
||||
// DialFunc is a [net.Resolver.Dial] function.
|
||||
@ -22,11 +19,6 @@ func NewNetResolver(opts ...Option) *net.Resolver {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.Meter == nil {
|
||||
options.Meter = meter.DefaultMeter
|
||||
opts = append(opts, Meter(options.Meter))
|
||||
}
|
||||
|
||||
return &net.Resolver{
|
||||
PreferGo: true,
|
||||
StrictErrors: options.Resolver.StrictErrors,
|
||||
@ -43,7 +35,7 @@ func NewNetDialer(parent DialFunc, opts ...Option) DialFunc {
|
||||
if cache.opts.MaxCacheEntries == 0 {
|
||||
cache.opts.MaxCacheEntries = DefaultMaxCacheEntries
|
||||
}
|
||||
return func(_ context.Context, network, address string) (net.Conn, error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
conn := &dnsConn{}
|
||||
conn.roundTrip = cachingRoundTrip(&cache, network, address)
|
||||
return conn, nil
|
||||
@ -64,7 +56,6 @@ type Options struct {
|
||||
PreferIPV4 bool
|
||||
PreferIPV6 bool
|
||||
Timeout time.Duration
|
||||
Meter meter.Meter
|
||||
}
|
||||
|
||||
// MaxCacheEntries sets the maximum number of entries to cache.
|
||||
@ -96,13 +87,6 @@ func NegativeCache(b bool) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Meter sets meter.Meter
|
||||
func Meter(m meter.Meter) Option {
|
||||
return func(o *Options) {
|
||||
o.Meter = m
|
||||
}
|
||||
}
|
||||
|
||||
// Timeout sets upstream *net.Resolver timeout
|
||||
func Timeout(td time.Duration) Option {
|
||||
return func(o *Options) {
|
||||
@ -132,12 +116,12 @@ func PreferIPV6(b bool) Option {
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
entries map[string]cacheEntry
|
||||
sync.RWMutex
|
||||
|
||||
dial DialFunc
|
||||
entries map[string]cacheEntry
|
||||
|
||||
opts Options
|
||||
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type cacheEntry struct {
|
||||
@ -172,6 +156,7 @@ func (c *cache) put(req string, res string) {
|
||||
}
|
||||
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
if c.entries == nil {
|
||||
c.entries = make(map[string]cacheEntry)
|
||||
}
|
||||
@ -180,8 +165,6 @@ func (c *cache) put(req string, res string) {
|
||||
var tested, evicted int
|
||||
for k, e := range c.entries {
|
||||
if time.Until(e.deadline) <= 0 {
|
||||
c.opts.Meter.Counter(semconv.CacheItemsTotal, "type", "dns").Dec()
|
||||
c.opts.Meter.Counter(semconv.CacheRequestTotal, "type", "dns", "method", "evict").Inc()
|
||||
// delete expired entry
|
||||
delete(c.entries, k)
|
||||
evicted++
|
||||
@ -192,8 +175,6 @@ func (c *cache) put(req string, res string) {
|
||||
continue
|
||||
}
|
||||
if evicted == 0 && c.opts.MaxCacheEntries > 0 && len(c.entries) >= c.opts.MaxCacheEntries {
|
||||
c.opts.Meter.Counter(semconv.CacheItemsTotal, "type", "dns").Dec()
|
||||
c.opts.Meter.Counter(semconv.CacheRequestTotal, "type", "dns", "method", "evict").Inc()
|
||||
// delete at least one entry
|
||||
delete(c.entries, k)
|
||||
}
|
||||
@ -205,9 +186,6 @@ func (c *cache) put(req string, res string) {
|
||||
deadline: time.Now().Add(ttl),
|
||||
value: res[2:],
|
||||
}
|
||||
|
||||
c.opts.Meter.Counter(semconv.CacheItemsTotal, "type", "dns").Inc()
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *cache) get(req string) (res string) {
|
||||
@ -232,7 +210,6 @@ func (c *cache) get(req string) (res string) {
|
||||
// prepend correct ID
|
||||
return req[:2] + entry.value
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@ -306,7 +283,7 @@ func getNameLen(msg string) int {
|
||||
for i < len(msg) {
|
||||
if msg[i] == 0 {
|
||||
// end of name
|
||||
i++
|
||||
i += 1
|
||||
break
|
||||
}
|
||||
if msg[i] >= 0xc0 {
|
||||
@ -333,18 +310,10 @@ func getUint32(s string) int {
|
||||
|
||||
func cachingRoundTrip(cache *cache, network, address string) roundTripper {
|
||||
return func(ctx context.Context, req string) (res string, err error) {
|
||||
cache.opts.Meter.Counter(semconv.CacheRequestInflight, "type", "dns").Inc()
|
||||
defer cache.opts.Meter.Counter(semconv.CacheRequestInflight, "type", "dns").Dec()
|
||||
// check cache
|
||||
if res = cache.get(req); res != "" {
|
||||
if res := cache.get(req); res != "" {
|
||||
return res, nil
|
||||
}
|
||||
cache.opts.Meter.Counter(semconv.CacheRequestTotal, "type", "dns", "method", "get", "status", "miss").Inc()
|
||||
ts := time.Now()
|
||||
defer func() {
|
||||
cache.opts.Meter.Summary(semconv.CacheRequestLatencyMicroseconds, "type", "dns", "method", "get").UpdateDuration(ts)
|
||||
cache.opts.Meter.Histogram(semconv.CacheRequestDurationSeconds, "type", "dns", "method", "get").UpdateDuration(ts)
|
||||
}()
|
||||
|
||||
switch {
|
||||
case cache.opts.PreferIPV4 && cache.opts.PreferIPV6:
|
||||
@ -371,7 +340,6 @@ func cachingRoundTrip(cache *cache, network, address string) roundTripper {
|
||||
var d net.Dialer
|
||||
conn, err = d.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -8,13 +8,9 @@ import (
|
||||
func TestCache(t *testing.T) {
|
||||
net.DefaultResolver = NewNetResolver(PreferIPV4(true))
|
||||
|
||||
_, err := net.LookupHost("unistack.org")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = net.LookupHost("unistack.org")
|
||||
addrs, err := net.LookupHost("unistack.org")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("addrs %v", addrs)
|
||||
}
|
||||
|
@ -11,16 +11,15 @@ import (
|
||||
)
|
||||
|
||||
type dnsConn struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
roundTrip roundTripper
|
||||
|
||||
deadline time.Time
|
||||
sync.Mutex
|
||||
|
||||
ibuf bytes.Buffer
|
||||
obuf bytes.Buffer
|
||||
|
||||
sync.Mutex
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
deadline time.Time
|
||||
roundTrip roundTripper
|
||||
}
|
||||
|
||||
type roundTripper func(ctx context.Context, req string) (res string, err error)
|
||||
@ -67,24 +66,19 @@ func (c *dnsConn) RemoteAddr() net.Addr {
|
||||
}
|
||||
|
||||
func (c *dnsConn) SetDeadline(t time.Time) error {
|
||||
var err error
|
||||
if err = c.SetReadDeadline(t); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = c.SetWriteDeadline(t); err != nil {
|
||||
return err
|
||||
}
|
||||
c.SetReadDeadline(t)
|
||||
c.SetWriteDeadline(t)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dnsConn) SetReadDeadline(t time.Time) error {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
c.deadline = t
|
||||
c.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *dnsConn) SetWriteDeadline(_ time.Time) error {
|
||||
func (c *dnsConn) SetWriteDeadline(t time.Time) error {
|
||||
// writes do not timeout
|
||||
return nil
|
||||
}
|
||||
@ -162,7 +156,7 @@ func readMessage(c net.Conn) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
return string(b[:n]), nil
|
||||
}
|
||||
} else {
|
||||
var sz [2]byte
|
||||
_, err := io.ReadFull(c, sz[:])
|
||||
if err != nil {
|
||||
@ -181,3 +175,4 @@ func readMessage(c net.Conn) (string, error) {
|
||||
}
|
||||
return str.String(), nil
|
||||
}
|
||||
}
|
||||
|
@ -71,7 +71,7 @@ func (h *serverHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
|
||||
}
|
||||
|
||||
// TagConn can attach some information to the given context.
|
||||
func (h *serverHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
|
||||
func (h *serverHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
|
||||
if span, ok := tracer.SpanFromContext(ctx); ok {
|
||||
attrs := peerAttr(peerFromCtx(ctx))
|
||||
span.AddLabels(attrs...)
|
||||
@ -80,7 +80,7 @@ func (h *serverHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) conte
|
||||
}
|
||||
|
||||
// HandleConn processes the Conn stats.
|
||||
func (h *serverHandler) HandleConn(_ context.Context, _ stats.ConnStats) {
|
||||
func (h *serverHandler) HandleConn(ctx context.Context, info stats.ConnStats) {
|
||||
}
|
||||
|
||||
type clientHandler struct {
|
||||
|
@ -665,12 +665,12 @@ func patParamKeys(pattern string) ([]string, error) {
|
||||
// longestPrefix finds the length of the shared prefix
|
||||
// of two strings
|
||||
func longestPrefix(k1, k2 string) int {
|
||||
maxLen := len(k1)
|
||||
if l := len(k2); l < maxLen {
|
||||
maxLen = l
|
||||
max := len(k1)
|
||||
if l := len(k2); l < max {
|
||||
max = l
|
||||
}
|
||||
var i int
|
||||
for i = 0; i < maxLen; i++ {
|
||||
for i = 0; i < max; i++ {
|
||||
if k1[i] != k2[i] {
|
||||
break
|
||||
}
|
||||
|
22
util/id/LICENSE
Normal file
22
util/id/LICENSE
Normal file
@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018-2021 Matous Dzivjak <matousdzivjak@gmail.com>
|
||||
Copyright (c) 2021 Unistack LLC <v.tolstov@unistack.org>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
157
util/id/id.go
157
util/id/id.go
@ -1,159 +1,112 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
"math"
|
||||
|
||||
uuidv8 "github.com/ash3in/uuidv8"
|
||||
"github.com/google/uuid"
|
||||
nanoid "github.com/matoous/go-nanoid"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
)
|
||||
|
||||
var generatedNode [6]byte
|
||||
// DefaultAlphabet is the alphabet used for ID characters by default
|
||||
var DefaultAlphabet = []rune("6789BCDFGHJKLMNPQRTWbcdfghjkmnpqrtwz")
|
||||
|
||||
func init() {
|
||||
if _, err := rand.Read(generatedNode[:]); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Type int
|
||||
|
||||
const (
|
||||
TypeUnspecified Type = iota
|
||||
TypeNanoid
|
||||
TypeUUIDv8
|
||||
)
|
||||
|
||||
// DefaultNanoidAlphabet is the alphabet used for ID characters by default
|
||||
var DefaultNanoidAlphabet = "6789BCDFGHJKLMNPQRTWbcdfghjkmnpqrtwz"
|
||||
|
||||
// DefaultNanoidSize is the size used for ID by default
|
||||
// DefaultSize is the size used for ID by default
|
||||
// To get uuid like collision specify 21
|
||||
var DefaultNanoidSize = 16
|
||||
var DefaultSize = 16
|
||||
|
||||
type Generator struct {
|
||||
opts Options
|
||||
// getMask generates bit mask used to obtain bits from the random bytes that are used to get index of random character
|
||||
// from the alphabet. Example: if the alphabet has 6 = (110)_2 characters it is sufficient to use mask 7 = (111)_2
|
||||
func getMask(alphabetSize int) int {
|
||||
for i := 1; i <= 8; i++ {
|
||||
mask := (2 << uint(i)) - 1
|
||||
if mask >= alphabetSize-1 {
|
||||
return mask
|
||||
}
|
||||
|
||||
func (g *Generator) MustNew() string {
|
||||
id, err := g.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func (g *Generator) New() (string, error) {
|
||||
switch g.opts.Type {
|
||||
case TypeNanoid:
|
||||
if len(g.opts.NanoidAlphabet) == 0 || len(g.opts.NanoidAlphabet) > 255 {
|
||||
return "", errors.New("invalid option, NanoidAlphabet must not be empty and contain no more than 255 chars")
|
||||
}
|
||||
if g.opts.NanoidSize <= 0 {
|
||||
return "", errors.New("invalid option, NanoidSize must be positive integer")
|
||||
}
|
||||
|
||||
return nanoid.Generate(g.opts.NanoidAlphabet, g.opts.NanoidSize)
|
||||
case TypeUUIDv8:
|
||||
timestamp := uint64(time.Now().UnixNano())
|
||||
clockSeq := make([]byte, 2)
|
||||
if _, err := rand.Read(clockSeq); err != nil {
|
||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
||||
}
|
||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, g.opts.UUIDNode[:], uuidv8.TimestampBits48)
|
||||
}
|
||||
return "", errors.New("invalid option, Type unspecified")
|
||||
return 0
|
||||
}
|
||||
|
||||
// New returns new id or error
|
||||
func New(opts ...Option) (string, error) {
|
||||
options := NewOptions(opts...)
|
||||
|
||||
switch options.Type {
|
||||
case TypeNanoid:
|
||||
if len(options.NanoidAlphabet) == 0 || len(options.NanoidAlphabet) > 255 {
|
||||
return "", errors.New("invalid option, NanoidAlphabet must not be empty and contain no more than 255 chars")
|
||||
if len(options.Alphabet) == 0 || len(options.Alphabet) > 255 {
|
||||
return "", errors.New("alphabet must not be empty and contain no more than 255 chars")
|
||||
}
|
||||
if options.NanoidSize <= 0 {
|
||||
return "", errors.New("invalid option, NanoidSize must be positive integer")
|
||||
if options.Size <= 0 {
|
||||
return "", errors.New("size must be positive integer")
|
||||
}
|
||||
|
||||
return nanoid.Generate(options.NanoidAlphabet, options.NanoidSize)
|
||||
case TypeUUIDv8:
|
||||
timestamp := uint64(time.Now().UnixNano())
|
||||
clockSeq := make([]byte, 2)
|
||||
if _, err := rand.Read(clockSeq); err != nil {
|
||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
||||
}
|
||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, options.UUIDNode[:], uuidv8.TimestampBits48)
|
||||
}
|
||||
chars := options.Alphabet
|
||||
|
||||
return "", errors.New("invalid option, Type unspecified")
|
||||
}
|
||||
mask := getMask(len(chars))
|
||||
// estimate how many random bytes we will need for the ID, we might actually need more but this is tradeoff
|
||||
// between average case and worst case
|
||||
ceilArg := 1.6 * float64(mask*options.Size) / float64(len(options.Alphabet))
|
||||
step := int(math.Ceil(ceilArg))
|
||||
|
||||
func ToUUID(s string) uuid.UUID {
|
||||
return uuid.MustParse(s)
|
||||
id := make([]rune, options.Size)
|
||||
bytes := make([]byte, step)
|
||||
for j := 0; ; {
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for i := 0; i < step; i++ {
|
||||
currByte := bytes[i] & byte(mask)
|
||||
if currByte < byte(len(chars)) {
|
||||
id[j] = chars[currByte]
|
||||
j++
|
||||
if j == options.Size {
|
||||
return string(id[:options.Size]), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Must is the same as New but fatals on error
|
||||
func MustNew(opts ...Option) string {
|
||||
func Must(opts ...Option) string {
|
||||
id, err := New(opts...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
logger.DefaultLogger.Fatal(context.TODO(), "Must call is failed", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// Options contains id deneration options
|
||||
type Options struct {
|
||||
Type Type
|
||||
NanoidAlphabet string
|
||||
NanoidSize int
|
||||
UUIDNode [6]byte
|
||||
Alphabet []rune
|
||||
Size int
|
||||
}
|
||||
|
||||
// Option func signature
|
||||
type Option func(*Options)
|
||||
|
||||
// WithNanoidAlphabet specifies alphabet to use
|
||||
func WithNanoidAlphabet(alphabet string) Option {
|
||||
// Alphabet specifies alphabet to use
|
||||
func Alphabet(alphabet string) Option {
|
||||
return func(o *Options) {
|
||||
o.NanoidAlphabet = alphabet
|
||||
o.Alphabet = []rune(alphabet)
|
||||
}
|
||||
}
|
||||
|
||||
// WithNanoidSize specifies generated id size
|
||||
func WithNanoidSize(size int) Option {
|
||||
// Size specifies id size
|
||||
func Size(size int) Option {
|
||||
return func(o *Options) {
|
||||
o.NanoidSize = size
|
||||
}
|
||||
}
|
||||
|
||||
// WithUUIDNode specifies node component for UUIDv8
|
||||
func WithUUIDNode(node [6]byte) Option {
|
||||
return func(o *Options) {
|
||||
o.UUIDNode = node
|
||||
o.Size = size
|
||||
}
|
||||
}
|
||||
|
||||
// NewOptions returns new Options struct filled by opts
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
Type: TypeUUIDv8,
|
||||
NanoidAlphabet: DefaultNanoidAlphabet,
|
||||
NanoidSize: DefaultNanoidSize,
|
||||
UUIDNode: generatedNode,
|
||||
Alphabet: DefaultAlphabet,
|
||||
Size: DefaultSize,
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package id
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUUIDv8(t *testing.T) {
|
||||
id, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = id
|
||||
}
|
||||
|
||||
func TestToUUID(t *testing.T) {
|
||||
id, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u := ToUUID(id)
|
||||
_ = u
|
||||
}
|
@ -14,7 +14,7 @@ func Random(d time.Duration) time.Duration {
|
||||
return time.Duration(v)
|
||||
}
|
||||
|
||||
func RandomInterval(minTime, maxTime time.Duration) time.Duration {
|
||||
func RandomInterval(min, max time.Duration) time.Duration {
|
||||
var rng rand.Rand
|
||||
return time.Duration(rng.Int63n(maxTime.Nanoseconds()-minTime.Nanoseconds())+minTime.Nanoseconds()) * time.Nanosecond
|
||||
return time.Duration(rng.Int63n(max.Nanoseconds()-min.Nanoseconds())+min.Nanoseconds()) * time.Nanosecond
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user