Skip to content

Commit

Permalink
feat(xmsgbus): 添加消息总线支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Ccheers committed Jan 22, 2024
1 parent 3a7cb26 commit 239804e
Show file tree
Hide file tree
Showing 29 changed files with 1,776 additions and 13 deletions.
5 changes: 2 additions & 3 deletions etcdx/lease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
clientv3 "go.etcd.io/etcd/client/v3"
)

type emptyLog struct {
}
type emptyLog struct{}

func (x *emptyLog) Logf(level LogLevel, template string, args ...interface{}) {
log.Printf(fmt.Sprintf("[%s] %s\n", level, template), args...)
}

func (x *emptyLog) Logw(level LogLevel, keyPairs ...interface{}) {
var args = []interface{}{"level", level}
args := []interface{}{"level", level}
args = append(args, keyPairs...)
log.Println(args...)
}
Expand Down
1 change: 0 additions & 1 deletion etcdx/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,4 @@ func TestClientX_BatchWatch(t *testing.T) {

x.Close()
time.Sleep(time.Second)

}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ require (
github.com/aliyun/aliyun-oss-go-sdk v2.1.10+incompatible
github.com/fsnotify/fsnotify v1.4.9
github.com/go-playground/validator/v10 v10.8.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/mock v1.6.0
github.com/google/uuid v1.1.2
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.1+incompatible
github.com/philchia/agollo/v4 v4.1.3
github.com/pkg/errors v0.9.1
Expand All @@ -20,23 +22,26 @@ require (
go.etcd.io/etcd/client/v3 v3.5.7
go.opentelemetry.io/otel v1.20.0
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.20.0
go.opentelemetry.io/otel/sdk v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/mod v0.6.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.4.0
golang.org/x/tools v0.2.0
google.golang.org/grpc v1.41.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
Expand All @@ -56,7 +61,6 @@ require (
github.com/tklauser/numcpus v0.3.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.7 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/sdk v1.20.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
Expand All @@ -65,7 +69,6 @@ require (
golang.org/x/sys v0.14.0 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
12 changes: 11 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand All @@ -34,6 +35,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -65,6 +68,8 @@ github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD87
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k=
github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
Expand Down Expand Up @@ -100,6 +105,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.1+incompatible h1:EFjtiulITiEktaZrr0OPlymTmrlpvSAa/xvv08kTQEU=
Expand Down Expand Up @@ -132,6 +138,9 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/philchia/agollo/v4 v4.1.3 h1:UH4P9Zhm6hONGx1Ce9OjWfqPbstcQtfO0Nkicw7M5d4=
github.com/philchia/agollo/v4 v4.1.3/go.mod h1:SBdQmfqqu/XCWJ1MDzYcCL3X+p3VJ+uQBy0nxxqjexg=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -341,6 +350,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
7 changes: 2 additions & 5 deletions sync/graceful/graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
"github.com/ccheers/xpkg/sync/routinepool"
)

var (
ErrGracefulExitTimeout = fmt.Errorf("graceful exit timeout")
)
var ErrGracefulExitTimeout = fmt.Errorf("graceful exit timeout")

type ILogger interface {
Warn(ctx context.Context, message string)
Expand All @@ -32,8 +30,7 @@ type IExiting interface {
Stop(ctx context.Context) error
}

type stdLogger struct {
}
type stdLogger struct{}

func (x *stdLogger) Warn(ctx context.Context, message string) {
fmt.Printf("[%s][WARN]: %s\n", time.Now().String(), message)
Expand Down
60 changes: 60 additions & 0 deletions xlogger/level.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package xlogger

import "strings"

// Level is a logger level.
type Level int8

// LevelKey is logger level key.
const LevelKey = "level"

const (
// LevelDebug is logger debug level.
LevelDebug Level = iota - 1
// LevelInfo is logger info level.
LevelInfo
// LevelWarn is logger warn level.
LevelWarn
// LevelError is logger error level.
LevelError
// LevelFatal is logger fatal level
LevelFatal
)

func (l Level) Key() string {
return LevelKey
}

func (l Level) String() string {
switch l {
case LevelDebug:
return "DEBUG"
case LevelInfo:
return "INFO"
case LevelWarn:
return "WARN"
case LevelError:
return "ERROR"
case LevelFatal:
return "FATAL"
default:
return ""
}
}

// ParseLevel parses a level string into a logger Level value.
func ParseLevel(s string) Level {
switch strings.ToUpper(s) {
case "DEBUG":
return LevelDebug
case "INFO":
return LevelInfo
case "WARN":
return LevelWarn
case "ERROR":
return LevelError
case "FATAL":
return LevelFatal
}
return LevelInfo
}
101 changes: 101 additions & 0 deletions xlogger/level_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package xlogger

import "testing"

func TestLevel_Key(t *testing.T) {
if LevelInfo.Key() != LevelKey {
t.Errorf("want: %s, got: %s", LevelKey, LevelInfo.Key())
}
}

func TestLevel_String(t *testing.T) {
tests := []struct {
name string
l Level
want string
}{
{
name: "DEBUG",
l: LevelDebug,
want: "DEBUG",
},
{
name: "INFO",
l: LevelInfo,
want: "INFO",
},
{
name: "WARN",
l: LevelWarn,
want: "WARN",
},
{
name: "ERROR",
l: LevelError,
want: "ERROR",
},
{
name: "FATAL",
l: LevelFatal,
want: "FATAL",
},
{
name: "other",
l: 10,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.l.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestParseLevel(t *testing.T) {
tests := []struct {
name string
s string
want Level
}{
{
name: "DEBUG",
want: LevelDebug,
s: "DEBUG",
},
{
name: "INFO",
want: LevelInfo,
s: "INFO",
},
{
name: "WARN",
want: LevelWarn,
s: "WARN",
},
{
name: "ERROR",
want: LevelError,
s: "ERROR",
},
{
name: "FATAL",
want: LevelFatal,
s: "FATAL",
},
{
name: "other",
want: LevelInfo,
s: "other",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseLevel(tt.s); got != tt.want {
t.Errorf("ParseLevel() = %v, want %v", got, tt.want)
}
})
}
}
63 changes: 63 additions & 0 deletions xlogger/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package xlogger

import (
"context"
"log"
)

// DefaultLogger is default logger.
var DefaultLogger = NewStdLogger(log.Writer())

// Logger is a logger interface.
type Logger interface {
Log(level Level, keyvals ...interface{}) error
}

type logger struct {
logger Logger
prefix []interface{}
hasValuer bool
ctx context.Context
}

func (c *logger) Log(level Level, keyvals ...interface{}) error {
kvs := make([]interface{}, 0, len(c.prefix)+len(keyvals))
kvs = append(kvs, c.prefix...)
if c.hasValuer {
bindValues(c.ctx, kvs)
}
kvs = append(kvs, keyvals...)
return c.logger.Log(level, kvs...)
}

// With with logger fields.
func With(l Logger, kv ...interface{}) Logger {
c, ok := l.(*logger)
if !ok {
return &logger{logger: l, prefix: kv, hasValuer: containsValuer(kv), ctx: context.Background()}
}
kvs := make([]interface{}, 0, len(c.prefix)+len(kv))
kvs = append(kvs, c.prefix...)
kvs = append(kvs, kv...)
return &logger{
logger: c.logger,
prefix: kvs,
hasValuer: containsValuer(kvs),
ctx: c.ctx,
}
}

// WithContext returns a shallow copy of l with its context changed
// to ctx. The provided ctx must be non-nil.
func WithContext(ctx context.Context, l Logger) Logger {
c, ok := l.(*logger)
if !ok {
return &logger{logger: l, ctx: ctx}
}
return &logger{
logger: c.logger,
prefix: c.prefix,
hasValuer: c.hasValuer,
ctx: ctx,
}
}
17 changes: 17 additions & 0 deletions xlogger/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package xlogger

import (
"context"
"testing"
)

func TestInfo(_ *testing.T) {
logger := DefaultLogger
logger = With(logger, "ts", DefaultTimestamp)
logger = With(logger, "caller", DefaultCaller)
_ = logger.Log(LevelInfo, "key1", "value1")
}

func TestWithContext(_ *testing.T) {
WithContext(context.Background(), nil)
}
Loading

0 comments on commit 239804e

Please sign in to comment.