Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Sep 11, 2024
1 parent 1383aba commit 0eccb48
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 264 deletions.
2 changes: 0 additions & 2 deletions config/apollo/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,6 @@ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/config-apollo v0.0.1 h1:9RWRy3n/bXdlk/J8faO4iPjhSjSowaI9N4YY340TkI0=
github.com/kitex-contrib/config-apollo v0.0.1/go.mod h1:u9a9W/WHtQpJrfttzS9WllRErQU6nsLfM9mWh4vChpA=
github.com/kitex-contrib/monitor-prometheus v0.1.0/go.mod h1:ZHWQOKRHnN1Bw+PgVYeOXmB9l4+k8dlOJ9wx2xz76NU=
github.com/kitex-contrib/obs-opentelemetry v0.2.3/go.mod h1:pTXgnFrhyEGwSSYF4kpbELPgjoz0XBmhl54PaPjCH/w=
github.com/kitex-contrib/obs-opentelemetry/logging/logrus v0.0.0-20230819133448-76093321aa8e/go.mod h1:Kf0zvMUYs1/xlqrqthhJCw4RVyWZoAsfeAWUDN6en6U=
Expand Down
2 changes: 2 additions & 0 deletions config/file/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/cloudwego-contrib/cwgo-pkg/config/file

go 1.21

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

require (
github.com/bytedance/sonic v1.11.2
github.com/cloudwego/kitex v0.9.1
Expand Down
42 changes: 42 additions & 0 deletions config/file/parser/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import (
"github.com/cloudwego/kitex/pkg/circuitbreak"
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpctimeout"
)

// ClientFileConfig is config of a client/service pair
type ClientFileConfig struct {
Timeout map[string]*rpctimeout.RPCTimeout `mapstructure:"timeout"` // key: method, "*" for default
Retry map[string]*retry.Policy `mapstructure:"retry"` // key: method, "*" for default
Circuitbreaker map[string]*circuitbreak.CBConfig `mapstructure:"circuitbreaker"` // key: method
}

// ClientFileManager is a map of client/service pairs to ClientFileConfig
type ClientFileManager map[string]*ClientFileConfig

// GetConfig returns the config from Manager by key
func (s *ClientFileManager) GetConfig(key string) interface{} {
config, exist := (*s)[key]

if !exist {
return nil
}

return config
}
68 changes: 68 additions & 0 deletions config/file/parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import (
"fmt"

"github.com/bytedance/sonic"
"sigs.k8s.io/yaml"
)

type ConfigParam struct {
Type ConfigType
}

type ConfigType string

const (
JSON ConfigType = "json"
YAML ConfigType = "yaml"
)

// ConfigParser the parser for config file.
type ConfigParser interface {
Decode(kind ConfigType, data []byte, config interface{}) error
}

type Parser struct{}

type ConfigManager interface {
GetConfig(key string) interface{}
}

var _ ConfigParser = &Parser{}

// Decode decodes the data to struct in specified format.
func (p *Parser) Decode(kind ConfigType, data []byte, config interface{}) error {
switch kind {
case JSON:
return sonic.Unmarshal(data, config)
case YAML:
return yaml.Unmarshal(data, config)
default:
return fmt.Errorf("unsupported config data type %s", kind)
}
}

func DefaultConfigParser() ConfigParser {
return &Parser{}
}

func DefaultConfigParam() *ConfigParam {
return &ConfigParam{
Type: JSON,
}
}
36 changes: 36 additions & 0 deletions config/file/parser/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package parser

import "github.com/cloudwego/kitex/pkg/limiter"

// ServerFileConfig is config of a service
type ServerFileConfig struct {
Limit limiter.LimiterConfig `mapstructure:"limit"`
}

// ServerFileManager is a map of service names to ServerFileConfig
type ServerFileManager map[string]*ServerFileConfig

// GetConfig returns the config from Manager by key
func (s *ServerFileManager) GetConfig(key string) interface{} {
config, exist := (*s)[key]

if !exist {
return nil
}

return config
}
12 changes: 7 additions & 5 deletions config/nacos/v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module github.com/cloudwego-contrib/cwgo-pkg/config/nacos/v2

go 1.19
go 1.21

toolchain go1.21.11

require (
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b
github.com/cloudwego/kitex v0.8.0
github.com/cloudwego/kitex-examples v0.2.4
github.com/cloudwego/thriftgo v0.3.3
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
go.uber.org/atomic v1.11.0
sigs.k8s.io/yaml v1.4.0
Expand All @@ -21,7 +26,6 @@ require (
github.com/apache/thrift v0.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
Expand All @@ -33,7 +37,6 @@ require (
github.com/cloudwego/frugal v0.1.12 // indirect
github.com/cloudwego/localsession v0.0.2 // indirect
github.com/cloudwego/netpoll v0.5.1 // indirect
github.com/cloudwego/thriftgo v0.3.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/golang/mock v1.6.0 // indirect
Expand All @@ -49,7 +52,6 @@ require (
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oleiade/lane v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand Down Expand Up @@ -77,4 +79,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
4 changes: 3 additions & 1 deletion config/nacos/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
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/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -396,6 +397,7 @@ go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
Expand Down Expand Up @@ -788,4 +790,4 @@ rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
6 changes: 3 additions & 3 deletions registry/consul/consulhertz/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestConsulRegister(t *testing.T) {
}

var (
testSvcName = "etcdhertz.test.demo1"
testSvcName = "hertz.test.demo1"
testSvcPort = fmt.Sprintf("%d", 8581)
testSvcAddr = net.JoinHostPort(localIpAddr, testSvcPort)
testSvcWeight = 777
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestConsulDiscovery(t *testing.T) {
}

var (
testSvcName = "etcdhertz.test.demo2"
testSvcName = "hertz.test.demo2"
testSvcPort = fmt.Sprintf("%d", 8582)
testSvcAddr = net.JoinHostPort(localIpAddr, testSvcPort)
testSvcWeight = 777
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestConsulDeregister(t *testing.T) {
}

var (
testSvcName = "etcdhertz.test.demo3"
testSvcName = "hertz.test.demo3"
testSvcPort = fmt.Sprintf("%d", 8583)
testSvcAddr = net.JoinHostPort(localIpAddr, testSvcPort)
testSvcWeight = 777
Expand Down
3 changes: 1 addition & 2 deletions registry/etcd/etcdhertz/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ func TestResolver(t *testing.T) {
}
}

// TestEtcdRegistryWithHertz Test etcd registry complete workflow(service registry|service de-registry|service resolver)with hertz.
func TestEtcdRegistryWithHertz(t *testing.T) {
address := "127.0.0.1:1234"
r, _ := NewEtcdRegistry([]string{"127.0.0.1:2379"})
Expand Down Expand Up @@ -267,7 +266,7 @@ func TestEtcdRegistryWithHertz(t *testing.T) {
assert.Nil(t, opt.RegistryInfo.Tags)

if err := h.Shutdown(context.Background()); err != nil {
t.Errorf("HERTZ: Shutdown cwerror=%v", err)
t.Errorf("HERTZ: Shutdown error=%v", err)
}
time.Sleep(5 * time.Second)

Expand Down
2 changes: 2 additions & 0 deletions registry/etcd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (
go.etcd.io/etcd/server/v3 v3.5.12
)

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0

require (
github.com/apache/thrift v0.20.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
5 changes: 1 addition & 4 deletions registry/etcd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/thrift v0.13.0 h1:5hryIiq9gtn+MiLVn0wP37kb/uTeRZgN08WoCsAhIhI=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.20.0 h1:631+KvYbsBZxmuJjYwhezVsrfc/TbqtZV4QcxOX1fOI=
github.com/apache/thrift v0.20.0/go.mod h1:hOk1BQqcp2OLzGsyVXdfMk7YFlMxK3aoEVhjD06QhB8=
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/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
Expand Down Expand Up @@ -199,8 +198,6 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/registry-etcd v0.2.4 h1:4C8vPNoiZuzBSCurtjystQj2aG7pJzHXzGg2sSogeWg=
github.com/kitex-contrib/registry-etcd v0.2.4/go.mod h1:Imgvy+EkqExHuJs4+VFK0g1bFWHaCJ0KuK1sofWYZf4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
Expand Down
6 changes: 3 additions & 3 deletions registry/etcd/internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ type InstanceInfo struct {
}

func ServiceKeyPrefix(prefix string, serviceName string) string {
prefix = prefix + "/%v/"
return fmt.Sprintf(prefix, serviceName)
prefix = prefix + "/" + serviceName
return prefix
}

// ServiceKey generates the key used to stored in etcd.
func ServiceKey(prefix string, serviceName, addr string) string {
return ServiceKeyPrefix(prefix, serviceName) + addr
return ServiceKeyPrefix(prefix, serviceName) + "/" + addr
}

func GetLocalIPv4Host() (string, error) {
Expand Down
Loading

0 comments on commit 0eccb48

Please sign in to comment.