Compare commits

..

10 Commits

Author SHA1 Message Date
d0f9d44fe4 Coverage default.go (#330)
Some checks failed
Go / test (push) Failing after 11s
/ autoupdate (push) Failing after 1s
Coverage default.go

Added tests for default.go. The tests are successful

Co-authored-by: Aleksandr Tolstikhin <atolstikhin@mtsbank.ru>
Co-authored-by: Василий Толстов <v.tolstov@unistack.org>
Reviewed-on: #330
Reviewed-by: Василий Толстов <v.tolstov@unistack.org>
Co-authored-by: Александр Толстихин <tolstihin1996@mail.ru>
Co-committed-by: Александр Толстихин <tolstihin1996@mail.ru>
2024-09-20 17:55:24 +03:00
725ed992cc #335 caller skip count. (#338)
Some checks failed
Go / test (push) Has been cancelled
/ autoupdate (push) Failing after 5s
Co-authored-by: Gorbunov Kirill Andreevich <kgorbunov@mtsbank.ru>
Reviewed-on: #338
Co-authored-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
Co-committed-by: Кирилл Горбунов <kirya_gorbunov_2015@mail.ru>
2024-04-15 13:31:14 +03:00
b8928d3da9 semconv: add cache metric names
Some checks failed
Go / test (push) Failing after 6m10s
/ autoupdate (push) Successful in 1m20s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-14 16:46:05 +03:00
76090f7569 util/xpool: package pool
Some checks failed
Go / test (push) Failing after 6m9s
/ autoupdate (push) Successful in 1m18s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-14 00:19:08 +03:00
f8c68a81f7 semconv: add broker group lag
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-13 02:11:05 +03:00
5d997f7654 Merge pull request 'add options in broker' (#333) from devstigneev/micro:master into master
Some checks failed
Go / test (push) Failing after 6m14s
/ autoupdate (push) Successful in 1m20s
Reviewed-on: #333
2024-04-08 23:17:49 +03:00
56d33ae823 rename path to sync
Some checks failed
Go / test (pull_request) Has been cancelled
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled
2024-04-07 21:17:42 +03:00
c3cabc1fe5 add options in broker
Some checks failed
Go / test (pull_request) Has been cancelled
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled
2024-04-07 20:49:07 +03:00
47497b49b3 util/reflect: detect json.Unmarshaler
Some checks failed
Go / test (push) Failing after 6m45s
/ autoupdate (push) Successful in 1m21s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-04-02 08:54:12 +03:00
e89cfdc80d move sort.Uniq to dedicated package
Some checks failed
/ autoupdate (push) Has been cancelled
Go / test (push) Has been cancelled
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-03-27 11:18:16 +03:00
13 changed files with 211 additions and 51 deletions

3
.gitignore vendored
View File

@ -1,6 +1,8 @@
# Develop tools
/.vscode/
/.idea/
.idea
.vscode
# Binaries for programs and plugins
*.exe
@ -13,6 +15,7 @@
_obj
_test
_build
.DS_Store
# Architecture specific extensions/prefixes
*.[568vq]

View File

@ -4,6 +4,7 @@ package broker // import "go.unistack.org/micro/v4/broker"
import (
"context"
"errors"
"time"
"go.unistack.org/micro/v4/metadata"
"go.unistack.org/micro/v4/options"
@ -19,6 +20,8 @@ var (
ErrDisconnected = errors.New("broker disconnected")
// ErrInvalidMessage returns when message has nvalid format
ErrInvalidMessage = errors.New("broker message has invalid format")
// DefaultGracefulTimeout
DefaultGracefulTimeout = 5 * time.Second
)
// Broker is an interface used for asynchronous messaging.

View File

@ -11,6 +11,7 @@ import (
"go.unistack.org/micro/v4/meter"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/register"
"go.unistack.org/micro/v4/sync"
"go.unistack.org/micro/v4/tracer"
)
@ -36,22 +37,27 @@ type Options struct {
Name string
// Address holds the broker address
Address []string
Wait *sync.WaitGroup
GracefulTimeout time.Duration
}
// NewOptions create new Options
func NewOptions(opts ...options.Option) Options {
options := Options{
newOpts := Options{
Register: register.DefaultRegister,
Logger: logger.DefaultLogger,
Context: context.Background(),
Meter: meter.DefaultMeter,
Codecs: make(map[string]codec.Codec),
Tracer: tracer.DefaultTracer,
GracefulTimeout: DefaultGracefulTimeout,
}
for _, o := range opts {
o(&options)
o(&newOpts)
}
return options
return newOpts
}
// PublishOptions struct

View File

@ -31,6 +31,9 @@ func (c *cfg) Validate() error {
if c.IntValue != 10 {
return fmt.Errorf("invalid IntValue %d != %d", 10, c.IntValue)
}
if c.MapValue["key1"] != true {
return fmt.Errorf("invalid MapValue %t != %t", true, c.MapValue["key1"])
}
return nil
}
@ -105,3 +108,19 @@ func TestValidate(t *testing.T) {
t.Fatal(err)
}
}
func TestString(t *testing.T) {
cfg := config.NewConfig()
res := cfg.String()
if res != "default" {
t.Fatalf("string value invalid: %s", res)
}
}
func TestName(t *testing.T) {
cfg := config.NewConfig()
res := cfg.Name()
if res != "" {
t.Fatal("name value not empty")
}
}

36
flow/flow_test.go Normal file
View File

@ -0,0 +1,36 @@
package flow
import (
"reflect"
"testing"
)
func FuzzMarshall(f *testing.F) {
f.Fuzz(func(t *testing.T, ref []byte) {
rm := RawMessage(ref)
b, err := rm.MarshalJSON()
if err != nil {
t.Errorf("Error MarshalJSON: %s", err)
}
if !reflect.DeepEqual(ref, b) {
t.Errorf("Error. Expected '%s', was '%s'", ref, b)
}
})
}
func FuzzUnmarshall(f *testing.F) {
f.Fuzz(func(t *testing.T, ref string) {
b := []byte(ref)
rm := RawMessage(b)
if err := rm.UnmarshalJSON(b); err != nil {
t.Errorf("Error UnmarshalJSON: %s", err)
}
if ref != string(rm) {
t.Errorf("Error. Expected '%s', was '%s'", ref, rm)
}
})
}

View File

@ -214,6 +214,20 @@ func WithMicroKeys() options.Option {
}
}
// WithAddCallerSkipCount add skip count for copy logger
func WithAddCallerSkipCount(n int) options.Option {
return func(src interface{}) error {
c, err := options.Get(src, ".CallerSkipCount")
if err != nil {
return err
}
if err = options.Set(src, c.(int)+n, ".CallerSkipCount"); err != nil {
return err
}
return nil
}
}
// WithAddStacktrace controls writing stacktrace on error
func WithAddStacktrace(v bool) options.Option {
return func(src interface{}) error {

View File

@ -17,4 +17,6 @@ var (
SubscribeMessageTotal = "subscribe_message_total"
// SubscribeMessageInflight specifies meter metric name
SubscribeMessageInflight = "subscribe_message_inflight"
// BrokerGroupLag specifies broker lag
BrokerGroupLag = "broker_lag"
)

12
semconv/cache.go Normal file
View File

@ -0,0 +1,12 @@
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"
)

View File

@ -1,10 +1,8 @@
// Package tracer provides an interface for distributed tracing
package tracer // import "go.unistack.org/micro/v4/tracer"
package tracer
import (
"context"
"fmt"
"sort"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/options"
@ -71,37 +69,3 @@ type Span interface {
// SpanID returns span id
SpanID() string
}
// sort labels alphabeticaly by label name
type byKey []interface{}
func (k byKey) Len() int { return len(k) / 2 }
func (k byKey) Less(i, j int) bool { return fmt.Sprintf("%s", k[i*2]) < fmt.Sprintf("%s", k[j*2]) }
func (k byKey) Swap(i, j int) {
k[i*2], k[j*2] = k[j*2], k[i*2]
k[i*2+1], k[j*2+1] = k[j*2+1], k[i*2+1]
}
func UniqLabels(labels []interface{}) []interface{} {
if len(labels)%2 == 1 {
labels = labels[:len(labels)-1]
}
if len(labels) > 2 {
sort.Sort(byKey(labels))
idx := 0
for {
if labels[idx] == labels[idx+2] {
copy(labels[idx:], labels[idx+2:])
labels = labels[:len(labels)-2]
} else {
idx += 2
}
if idx+2 >= len(labels) {
break
}
}
}
return labels
}

View File

@ -1,6 +1,7 @@
package reflect // import "go.unistack.org/micro/v4/util/reflect"
package reflect
import (
"encoding/json"
"errors"
"fmt"
"reflect"
@ -45,15 +46,23 @@ func SliceAppend(b bool) Option {
// Merge merges map[string]interface{} to destination struct
func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error {
var err error
var sval reflect.Value
var fname string
options := Options{}
for _, o := range opts {
o(&options)
}
if unmarshaler, ok := dst.(json.Unmarshaler); ok {
buf, err := json.Marshal(mp)
if err == nil {
err = unmarshaler.UnmarshalJSON(buf)
}
return err
}
var err error
var sval reflect.Value
var fname string
dviface := reflect.ValueOf(dst)
if dviface.Kind() == reflect.Ptr {
dviface = dviface.Elem()

40
util/sort/sort.go Normal file
View File

@ -0,0 +1,40 @@
package sort
import (
"fmt"
"sort"
)
// sort labels alphabeticaly by label name
type byKey []interface{}
func (k byKey) Len() int { return len(k) / 2 }
func (k byKey) Less(i, j int) bool { return fmt.Sprintf("%s", k[i*2]) < fmt.Sprintf("%s", k[j*2]) }
func (k byKey) Swap(i, j int) {
k[i*2], k[j*2] = k[j*2], k[i*2]
k[i*2+1], k[j*2+1] = k[j*2+1], k[i*2+1]
}
func Uniq(labels []interface{}) []interface{} {
if len(labels)%2 == 1 {
labels = labels[:len(labels)-1]
}
if len(labels) > 2 {
sort.Sort(byKey(labels))
idx := 0
for {
if labels[idx] == labels[idx+2] {
copy(labels[idx:], labels[idx+2:])
labels = labels[:len(labels)-2]
} else {
idx += 2
}
if idx+2 >= len(labels) {
break
}
}
}
return labels
}

25
util/xpool/pool.go Normal file
View File

@ -0,0 +1,25 @@
package pool
import "sync"
type Pool[T any] struct {
p *sync.Pool
}
func NewPool[T any](fn func() T) Pool[T] {
return Pool[T]{
p: &sync.Pool{
New: func() interface{} {
return fn()
},
},
}
}
func (p Pool[T]) Get() T {
return p.p.Get().(T)
}
func (p Pool[T]) Put(t T) {
p.p.Put(t)
}

27
util/xpool/pool_test.go Normal file
View File

@ -0,0 +1,27 @@
package pool
import (
"bytes"
"strings"
"testing"
)
func TestBytes(t *testing.T) {
p := NewPool(func() *bytes.Buffer { return bytes.NewBuffer(nil) })
b := p.Get()
b.Write([]byte(`test`))
if b.String() != "test" {
t.Fatal("pool not works")
}
p.Put(b)
}
func TestStrings(t *testing.T) {
p := NewPool(func() *strings.Builder { return &strings.Builder{} })
b := p.Get()
b.Write([]byte(`test`))
if b.String() != "test" {
t.Fatal("pool not works")
}
p.Put(b)
}