Compare commits
11 Commits
3eebfb5b11
...
v3.10.86
| Author | SHA1 | Date | |
|---|---|---|---|
| eb1482d789 | |||
| a305f7553f | |||
|
|
d9b2f2a45d | ||
| 3ace7657dc | |||
| 53b40617e2 | |||
| 1a9236caad | |||
| 6c68d39081 | |||
| 35e62fbeb0 | |||
| 00b3ceb468 | |||
| 7dc8f088c9 | |||
| c65afcea1b |
@@ -373,6 +373,10 @@ func (m *memoryEvent) SetError(err error) {
|
|||||||
m.err = err
|
m.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *memoryEvent) Context() context.Context {
|
||||||
|
return m.opts.Context
|
||||||
|
}
|
||||||
|
|
||||||
func (m *memorySubscriber) Options() broker.SubscribeOptions {
|
func (m *memorySubscriber) Options() broker.SubscribeOptions {
|
||||||
return m.opts
|
return m.opts
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,6 @@ package codec // import "go.unistack.org/micro/v3/codec"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
|
||||||
|
|
||||||
"go.unistack.org/micro/v3/metadata"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Message types
|
|
||||||
const (
|
|
||||||
Error MessageType = iota
|
|
||||||
Request
|
|
||||||
Response
|
|
||||||
Event
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -32,42 +21,13 @@ var (
|
|||||||
DefaultTagName = "codec"
|
DefaultTagName = "codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MessageType specifies message type for codec
|
// Codec encodes/decodes various types of messages.
|
||||||
type MessageType int
|
|
||||||
|
|
||||||
// Codec encodes/decodes various types of messages used within micro.
|
|
||||||
// ReadHeader and ReadBody are called in pairs to read requests/responses
|
|
||||||
// from the connection. Close is called when finished with the
|
|
||||||
// connection. ReadBody may be called with a nil argument to force the
|
|
||||||
// body to be read and discarded.
|
|
||||||
type Codec interface {
|
type Codec interface {
|
||||||
ReadHeader(r io.Reader, m *Message, mt MessageType) error
|
|
||||||
ReadBody(r io.Reader, v interface{}) error
|
|
||||||
Write(w io.Writer, m *Message, v interface{}) error
|
|
||||||
Marshal(v interface{}, opts ...Option) ([]byte, error)
|
Marshal(v interface{}, opts ...Option) ([]byte, error)
|
||||||
Unmarshal(b []byte, v interface{}, opts ...Option) error
|
Unmarshal(b []byte, v interface{}, opts ...Option) error
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message represents detailed information about
|
|
||||||
// the communication, likely followed by the body.
|
|
||||||
// In the case of an error, body may be nil.
|
|
||||||
type Message struct {
|
|
||||||
Header metadata.Metadata
|
|
||||||
Target string
|
|
||||||
Method string
|
|
||||||
Endpoint string
|
|
||||||
Error string
|
|
||||||
ID string
|
|
||||||
Body []byte
|
|
||||||
Type MessageType
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMessage creates new codec message
|
|
||||||
func NewMessage(t MessageType) *Message {
|
|
||||||
return &Message{Type: t, Header: metadata.New(0)}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalAppend calls codec.Marshal(v) and returns the data appended to buf.
|
// MarshalAppend calls codec.Marshal(v) and returns the data appended to buf.
|
||||||
// If codec implements MarshalAppend, that is called instead.
|
// If codec implements MarshalAppend, that is called instead.
|
||||||
func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, error) {
|
func MarshalAppend(buf []byte, c Codec, v interface{}, opts ...Option) ([]byte, error) {
|
||||||
@@ -93,6 +53,8 @@ type RawMessage []byte
|
|||||||
func (m *RawMessage) MarshalJSON() ([]byte, error) {
|
func (m *RawMessage) MarshalJSON() ([]byte, error) {
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return []byte("null"), nil
|
return []byte("null"), nil
|
||||||
|
} else if len(*m) == 0 {
|
||||||
|
return []byte("null"), nil
|
||||||
}
|
}
|
||||||
return *m, nil
|
return *m, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,70 +2,14 @@ package codec
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
|
codecpb "go.unistack.org/micro-proto/v3/codec"
|
||||||
)
|
)
|
||||||
|
|
||||||
type noopCodec struct {
|
type noopCodec struct {
|
||||||
opts Options
|
opts Options
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *noopCodec) ReadHeader(conn io.Reader, m *Message, t MessageType) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *noopCodec) ReadBody(conn io.Reader, b interface{}) error {
|
|
||||||
// read bytes
|
|
||||||
buf, err := io.ReadAll(conn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if b == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch v := b.(type) {
|
|
||||||
case *string:
|
|
||||||
*v = string(buf)
|
|
||||||
case *[]byte:
|
|
||||||
*v = buf
|
|
||||||
case *Frame:
|
|
||||||
v.Data = buf
|
|
||||||
default:
|
|
||||||
return json.Unmarshal(buf, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *noopCodec) Write(conn io.Writer, m *Message, b interface{}) error {
|
|
||||||
if b == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var v []byte
|
|
||||||
switch vb := b.(type) {
|
|
||||||
case *Frame:
|
|
||||||
v = vb.Data
|
|
||||||
case string:
|
|
||||||
v = []byte(vb)
|
|
||||||
case *string:
|
|
||||||
v = []byte(*vb)
|
|
||||||
case *[]byte:
|
|
||||||
v = *vb
|
|
||||||
case []byte:
|
|
||||||
v = vb
|
|
||||||
default:
|
|
||||||
var err error
|
|
||||||
v, err = json.Marshal(vb)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_, err := conn.Write(v)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *noopCodec) String() string {
|
func (c *noopCodec) String() string {
|
||||||
return "noop"
|
return "noop"
|
||||||
}
|
}
|
||||||
@@ -91,8 +35,8 @@ func (c *noopCodec) Marshal(v interface{}, opts ...Option) ([]byte, error) {
|
|||||||
return ve, nil
|
return ve, nil
|
||||||
case *Frame:
|
case *Frame:
|
||||||
return ve.Data, nil
|
return ve.Data, nil
|
||||||
case *Message:
|
case *codecpb.Frame:
|
||||||
return ve.Body, nil
|
return ve.Data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.Marshal(v)
|
return json.Marshal(v)
|
||||||
@@ -115,8 +59,8 @@ func (c *noopCodec) Unmarshal(d []byte, v interface{}, opts ...Option) error {
|
|||||||
case *Frame:
|
case *Frame:
|
||||||
ve.Data = d
|
ve.Data = d
|
||||||
return nil
|
return nil
|
||||||
case *Message:
|
case *codecpb.Frame:
|
||||||
ve.Body = d
|
ve.Data = d
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,20 @@ var (
|
|||||||
ErrGatewayTimeout = &Error{Code: 504}
|
ErrGatewayTimeout = &Error{Code: 504}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const ProblemContentType = "application/problem+json"
|
||||||
|
|
||||||
|
type Problem struct {
|
||||||
|
Type string `json:"type,omitempty"`
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
Detail string `json:"detail,omitempty"`
|
||||||
|
Instance string `json:"instance,omitempty"`
|
||||||
|
Errors []struct {
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
Detail string `json:"detail,omitempty"`
|
||||||
|
} `json:"errors,omitempty"`
|
||||||
|
Status int `json:"status,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// Error type
|
// Error type
|
||||||
type Error struct {
|
type Error struct {
|
||||||
// ID holds error id or service, usually someting like my_service or id
|
// ID holds error id or service, usually someting like my_service or id
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Package logger provides a log interface
|
// Package logger provides a log interface
|
||||||
package logger // import "go.unistack.org/micro/v3/logger"
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func NewOptions(opts ...Option) Options {
|
|||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithContextAttrFuncs appends default funcs for the context arrts filler
|
// WithContextAttrFuncs appends default funcs for the context attrs filler
|
||||||
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
func WithContextAttrFuncs(fncs ...ContextAttrFunc) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...)
|
o.ContextAttrFuncs = append(o.ContextAttrFuncs, fncs...)
|
||||||
@@ -137,6 +137,13 @@ func WithName(n string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithMeter sets the meter
|
||||||
|
func WithMeter(m meter.Meter) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.Meter = m
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithTimeFunc sets the func to obtain current time
|
// WithTimeFunc sets the func to obtain current time
|
||||||
func WithTimeFunc(fn func() time.Time) Option {
|
func WithTimeFunc(fn func() time.Time) Option {
|
||||||
return func(o *Options) {
|
return func(o *Options) {
|
||||||
|
|||||||
@@ -305,6 +305,10 @@ func (t *tunEvent) SetError(err error) {
|
|||||||
t.err = err
|
t.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tunEvent) Context() context.Context {
|
||||||
|
return context.TODO()
|
||||||
|
}
|
||||||
|
|
||||||
// NewBroker returns new tunnel broker
|
// NewBroker returns new tunnel broker
|
||||||
func NewBroker(opts ...broker.Option) (broker.Broker, error) {
|
func NewBroker(opts ...broker.Option) (broker.Broker, error) {
|
||||||
options := broker.NewOptions(opts...)
|
options := broker.NewOptions(opts...)
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package semconv
|
|
||||||
|
|
||||||
var (
|
|
||||||
// CacheRequestDurationSeconds specifies meter metric name
|
|
||||||
CacheRequestDurationSeconds = "cache_request_duration_seconds"
|
|
||||||
// ClientRequestLatencyMicroseconds specifies meter metric name
|
|
||||||
CacheRequestLatencyMicroseconds = "cache_request_latency_microseconds"
|
|
||||||
// CacheRequestTotal specifies meter metric name
|
|
||||||
CacheRequestTotal = "cache_request_total"
|
|
||||||
// CacheRequestInflight specifies meter metric name
|
|
||||||
CacheRequestInflight = "cache_request_inflight"
|
|
||||||
)
|
|
||||||
12
semconv/store.go
Normal file
12
semconv/store.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package semconv
|
||||||
|
|
||||||
|
var (
|
||||||
|
// StoreRequestDurationSeconds specifies meter metric name
|
||||||
|
StoreRequestDurationSeconds = "store_request_duration_seconds"
|
||||||
|
// ClientRequestLatencyMicroseconds specifies meter metric name
|
||||||
|
StoreRequestLatencyMicroseconds = "store_request_latency_microseconds"
|
||||||
|
// StoreRequestTotal specifies meter metric name
|
||||||
|
StoreRequestTotal = "store_request_total"
|
||||||
|
// StoreRequestInflight specifies meter metric name
|
||||||
|
StoreRequestInflight = "store_request_inflight"
|
||||||
|
)
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -691,7 +690,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
|
|||||||
req = req.Elem()
|
req = req.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cf.ReadBody(bytes.NewBuffer(msg.Body), req.Interface()); err != nil {
|
if err = cf.Unmarshal(msg.Body, req.Interface()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -140,6 +140,8 @@ type Options struct {
|
|||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
// Name of the tracer
|
// Name of the tracer
|
||||||
Name string
|
Name string
|
||||||
|
// ContextAttrFuncs contains funcs that provides tracing
|
||||||
|
ContextAttrFuncs []ContextAttrFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option func signature
|
// Option func signature
|
||||||
@@ -176,8 +178,9 @@ func NewSpanOptions(opts ...SpanOption) SpanOptions {
|
|||||||
// NewOptions returns default options
|
// NewOptions returns default options
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
Logger: logger.DefaultLogger,
|
Logger: logger.DefaultLogger,
|
||||||
Context: context.Background(),
|
Context: context.Background(),
|
||||||
|
ContextAttrFuncs: DefaultContextAttrFuncs,
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
o(&options)
|
||||||
|
|||||||
@@ -21,8 +21,11 @@ var (
|
|||||||
"HealthService.Ready",
|
"HealthService.Ready",
|
||||||
"HealthService.Version",
|
"HealthService.Version",
|
||||||
}
|
}
|
||||||
|
DefaultContextAttrFuncs []ContextAttrFunc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ContextAttrFunc func(ctx context.Context) []interface{}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs,
|
logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs,
|
||||||
func(ctx context.Context) []interface{} {
|
func(ctx context.Context) []interface{} {
|
||||||
|
|||||||
60
util/sort/sort_test.go
Normal file
60
util/sort/sort_test.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package sort
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUniq(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
labels []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test#1",
|
||||||
|
args: args{
|
||||||
|
labels: append(make([]interface{}, 0), "test-1", 1, "test-2", 2),
|
||||||
|
},
|
||||||
|
want: append(make([]interface{}, 0), "test-1", 1, "test-2", 2),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test#2",
|
||||||
|
args: args{
|
||||||
|
labels: append(make([]interface{}, 0), "test-1", 1, "test-2", 2, "test-2", 2),
|
||||||
|
},
|
||||||
|
want: append(make([]interface{}, 0), "test-1", 1, "test-2", 2),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test#3",
|
||||||
|
args: args{
|
||||||
|
labels: append(make([]interface{}, 0), "test-1", 1, "test-2", 2, "test-2", 3),
|
||||||
|
},
|
||||||
|
want: append(make([]interface{}, 0), "test-1", 1, "test-2", 3),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test#4",
|
||||||
|
args: args{
|
||||||
|
labels: append(make([]interface{}, 0),
|
||||||
|
"test-1", 1, "test-1", 2,
|
||||||
|
"test-2", 3, "test-2", 2,
|
||||||
|
"test-3", 5, "test-3", 3,
|
||||||
|
"test-1", 4, "test-1", 1),
|
||||||
|
},
|
||||||
|
want: append(make([]interface{}, 0), "test-1", 1, "test-2", 2, "test-3", 3),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var got []interface{}
|
||||||
|
if got = Uniq(tt.args.labels); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
t.Errorf("Uniq() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
t.Logf("got-%#v", got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
package pool
|
package pool
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Pool[T any] struct {
|
type Pool[T any] struct {
|
||||||
p *sync.Pool
|
p *sync.Pool
|
||||||
@@ -23,3 +26,11 @@ func (p Pool[T]) Get() T {
|
|||||||
func (p Pool[T]) Put(t T) {
|
func (p Pool[T]) Put(t T) {
|
||||||
p.p.Put(t)
|
p.p.Put(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewBytePool(size int) Pool[[]byte] {
|
||||||
|
return NewPool(func() []byte { return make([]byte, size) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBytesPool() Pool[*bytes.Buffer] {
|
||||||
|
return NewPool(func() *bytes.Buffer { return bytes.NewBuffer(nil) })
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user