Repository: go-kratos/kratos
Pull request #3113 (2023-12-10 07:14:10)

chore:chore update
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need to mark it as a WIP(Work In Progress) PR or Draft PR
4. Please use a semantic commits format title, such as `<type>[optional scope]: <description>`, see: https://go-kratos.dev/docs/community/contribution#type
5. at the same time, please note that similar work should be submitted in one PR as far as possible to reduce the workload of reviewers. Do not split a work into multiple PR unless it should.
-->

<!--
🎉 感谢您向 Kratos 发送 PR！以下是一些提示：
如果这是你第一次为 Kratos 贡献，请阅读我们的贡献指南：https://go-kratos.dev/en/docs/community/contribution/
2、确保您已经为您的 PR 添加或运行了适当的测试和lint，请在提交PR之前使用“make lint”和“make test”，使用“make clean”整理您的 go.mod。
3、如果 PR 未完成，您可能需要将其标记为 WIP（Work In Progress）PR 或 Draft PR
4、请使用语义提交格式标题，如“<类型>[可选范围]：<说明>`，请参阅：https://go-kratos.dev/docs/community/contribution#type
5. 同时请注意，同类的工作请尽量在一个PR中提交，以减轻 review 者的工作负担，不要把一项工作拆分成很多个PR，除非它应该这样做。
-->


#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->
fix some words 
format
add a test

#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If your PR is not fully resolved the issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->


#### Other special notes for the reviewers:
<!--
* Somethings that need extra attention for the reviewers
* Some additional notes, TODO list, etc.
-->


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/go-kratos/kratos/pull/3113?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`7e2edec`)](https://app.codecov.io/gh/go-kratos/kratos/commit/7e2edecabf320cfebeb8c24b95263b7395daf3c3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) 84.40% compared to head [(`65676de`)](https://app.codecov.io/gh/go-kratos/kratos/pull/3113?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) 84.43%.

> :exclamation: Current head 65676de differs from pull request most recent head 4d8e109. Consider uploading reports for the commit 4d8e109 to get more accurate results


:exclamation: Your organization needs to install the [Codecov GitHub app](https://github.com/apps/codecov/installations/select_target) to enable full functionality.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #3113      +/-   ##
==========================================
+ Coverage   84.40%   84.43%   +0.02%     
==========================================
  Files          88       88              
  Lines        3983     3983              
==========================================
+ Hits         3362     3363       +1     
+ Misses        446      443       -3     
- Partials      175      177       +2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/go-kratos/kratos/pull/3113?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos).


Patch:
diff --git a/app.go b/app.go
index 88532891..2fa52ec9 100644
--- a/app.go
+++ b/app.go
@@ -30,7 +30,7 @@ type AppInfo interface {
 type App struct {
 	opts     options
 	ctx      context.Context
-	cancel   func()
+	cancel   context.CancelFunc
 	mu       sync.Mutex
 	instance *registry.ServiceInstance
 }
diff --git a/contrib/config/consul/config.go b/contrib/config/consul/config.go
index 0404801b..108f0082 100644
--- a/contrib/config/consul/config.go
+++ b/contrib/config/consul/config.go
@@ -11,7 +11,7 @@ import (
 	"github.com/go-kratos/kratos/v2/config"
 )
 
-// Option is etcd config option.
+// Option is consul config option.
 type Option func(o *options)
 
 type options struct {
diff --git a/contrib/polaris/go.mod b/contrib/polaris/go.mod
index 7dd7c735..46225dca 100644
--- a/contrib/polaris/go.mod
+++ b/contrib/polaris/go.mod
@@ -16,7 +16,7 @@ require (
 	github.com/dlclark/regexp2 v1.7.0 // indirect
 	github.com/go-playground/form/v4 v4.2.0 // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
-	github.com/gorilla/mux v1.8.0 // indirect
+	github.com/gorilla/mux v1.8.1 // indirect
 	github.com/hashicorp/errwrap v1.0.0 // indirect
 	github.com/hashicorp/go-multierror v1.1.1 // indirect
 	github.com/imdario/mergo v0.3.16 // indirect
diff --git a/contrib/polaris/go.sum b/contrib/polaris/go.sum
index c457f9a0..69b19318 100644
--- a/contrib/polaris/go.sum
+++ b/contrib/polaris/go.sum
@@ -159,8 +159,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
 github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
-github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
-github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
+github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
+github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
 github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
 github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
 github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
diff --git a/contrib/registry/eureka/eureka.go b/contrib/registry/eureka/eureka.go
index a7759dee..530b638c 100644
--- a/contrib/registry/eureka/eureka.go
+++ b/contrib/registry/eureka/eureka.go
@@ -58,9 +58,9 @@ func (e *API) broadcast() {
 	for _, subscriber := range e.subscribers {
 		go subscriber.callBack()
 	}
-	defer e.lock.Unlock()
 	e.lock.Lock()
 	e.allInstances = instances
+	e.lock.Unlock()
 }
 
 func (e *API) cacheAllInstances() map[string][]Instance {
@@ -106,12 +106,12 @@ func (e *API) Deregister(ctx context.Context, endpoints []Endpoint) error {
 
 func (e *API) Subscribe(serverName string, fn func()) error {
 	e.lock.Lock()
-	defer e.lock.Unlock()
 	appID := e.ToAppID(serverName)
 	e.subscribers[appID] = &subscriber{
 		appID:    appID,
 		callBack: fn,
 	}
+	e.lock.Unlock()
 	go e.broadcast()
 	return nil
 }
@@ -128,8 +128,8 @@ func (e *API) GetService(ctx context.Context, serverName string) []Instance {
 
 func (e *API) Unsubscribe(serverName string) {
 	e.lock.Lock()
-	defer e.lock.Unlock()
 	delete(e.subscribers, e.ToAppID(serverName))
+	e.lock.Unlock()
 }
 
 func (e *API) ToAppID(serverName string) string {
diff --git a/contrib/registry/eureka/register.go b/contrib/registry/eureka/register.go
index 242ec910..f7226ce2 100644
--- a/contrib/registry/eureka/register.go
+++ b/contrib/registry/eureka/register.go
@@ -54,7 +54,11 @@ func New(eurekaUrls []string, opts ...Option) (*Registry, error) {
 		o(r)
 	}
 
-	client := NewClient(eurekaUrls, WithHeartbeatInterval(r.heartbeatInterval), WithClientContext(r.ctx), WithNamespace(r.eurekaPath))
+	client := NewClient(eurekaUrls,
+		WithHeartbeatInterval(r.heartbeatInterval),
+		WithClientContext(r.ctx),
+		WithNamespace(r.eurekaPath),
+	)
 	r.api = NewAPI(r.ctx, client, r.refreshInterval)
 	return r, nil
 }
diff --git a/contrib/registry/nacos/go.mod b/contrib/registry/nacos/go.mod
index c6e5e1c9..19e95ce0 100644
--- a/contrib/registry/nacos/go.mod
+++ b/contrib/registry/nacos/go.mod
@@ -9,7 +9,7 @@ require (
 
 require (
 	github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
-	github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 // indirect
+	github.com/buger/jsonparser v1.1.1 // indirect
 	github.com/go-errors/errors v1.0.1 // indirect
 	github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
 	github.com/json-iterator/go v1.1.6 // indirect
diff --git a/contrib/registry/polaris/registry.go b/contrib/registry/polaris/registry.go
index d9b87150..e968fe38 100644
--- a/contrib/registry/polaris/registry.go
+++ b/contrib/registry/polaris/registry.go
@@ -376,7 +376,7 @@ func (w *Watcher) Next() ([]*registry.ServiceInstance, error) {
 							if serviceInstance.ID == instance.GetId() {
 								// remove equal
 								if len(w.ServiceInstances) <= 1 {
-									w.ServiceInstances = w.ServiceInstances[0:0]
+									w.ServiceInstances = w.ServiceInstances[:0]
 									continue
 								}
 								w.ServiceInstances = append(w.ServiceInstances[:i], w.ServiceInstances[i+1:]...)
diff --git a/go.mod b/go.mod
index 4280a65a..33874fec 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
 	github.com/go-playground/form/v4 v4.2.0
 	github.com/golang-jwt/jwt/v5 v5.1.0
 	github.com/google/uuid v1.3.0
-	github.com/gorilla/mux v1.8.0
+	github.com/gorilla/mux v1.8.1
 	github.com/imdario/mergo v0.3.16
 	go.opentelemetry.io/otel v1.16.0
 	go.opentelemetry.io/otel/sdk v1.16.0
diff --git a/go.sum b/go.sum
index fee7466d..340d04fb 100644
--- a/go.sum
+++ b/go.sum
@@ -33,8 +33,8 @@ 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/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
-github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
+github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
+github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
 github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
 github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
 github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
diff --git a/selector/filter/version.go b/selector/filter/version.go
index 21f8518a..a5374470 100644
--- a/selector/filter/version.go
+++ b/selector/filter/version.go
@@ -9,7 +9,7 @@ import (
 // Version is version filter.
 func Version(version string) selector.NodeFilter {
 	return func(_ context.Context, nodes []selector.Node) []selector.Node {
-		newNodes := make([]selector.Node, 0)
+		newNodes := make([]selector.Node, 0, len(nodes))
 		for _, n := range nodes {
 			if n.Version() == version {
 				newNodes = append(newNodes, n)
diff --git a/transport/grpc/client.go b/transport/grpc/client.go
index d3c24617..edc57d3e 100644
--- a/transport/grpc/client.go
+++ b/transport/grpc/client.go
@@ -39,7 +39,7 @@ func WithEndpoint(endpoint string) ClientOption {
 	}
 }
 
-// WithSubset with client disocvery subset size.
+// WithSubset with client discovery subset size.
 // zero value means subset filter disabled
 func WithSubset(size int) ClientOption {
 	return func(o *clientOptions) {
diff --git a/transport/grpc/resolver/direct/builder.go b/transport/grpc/resolver/direct/builder.go
index ebbd22d7..a81b77af 100644
--- a/transport/grpc/resolver/direct/builder.go
+++ b/transport/grpc/resolver/direct/builder.go
@@ -6,6 +6,8 @@ import (
 	"google.golang.org/grpc/resolver"
 )
 
+const name = "direct"
+
 func init() {
 	resolver.Register(NewBuilder())
 }
@@ -35,5 +37,5 @@ func (d *directBuilder) Build(target resolver.Target, cc resolver.ClientConn, _
 }
 
 func (d *directBuilder) Scheme() string {
-	return "direct"
+	return name
 }
diff --git a/transport/http/client.go b/transport/http/client.go
index 7784a1c2..2d013ffa 100644
--- a/transport/http/client.go
+++ b/transport/http/client.go
@@ -56,7 +56,7 @@ type clientOptions struct {
 	subsetSize   int
 }
 
-// WithSubset with client disocvery subset size.
+// WithSubset with client discovery subset size.
 // zero value means subset filter disabled
 func WithSubset(size int) ClientOption {
 	return func(o *clientOptions) {
diff --git a/transport/http/resolver.go b/transport/http/resolver.go
index bcbf8f3b..e0db0cb6 100644
--- a/transport/http/resolver.go
+++ b/transport/http/resolver.go
@@ -56,7 +56,7 @@ type resolver struct {
 func newResolver(ctx context.Context, discovery registry.Discovery, target *Target,
 	rebalancer selector.Rebalancer, block, insecure bool, subsetSize int,
 ) (*resolver, error) {
-	// this is new resovler
+	// this is new resolver
 	watcher, err := discovery.Watch(ctx, target.Endpoint)
 	if err != nil {
 		return nil, err
diff --git a/transport/http/server.go b/transport/http/server.go
index 9958a08a..c967e4a1 100644
--- a/transport/http/server.go
+++ b/transport/http/server.go
@@ -134,7 +134,7 @@ func Listener(lis net.Listener) ServerOption {
 	}
 }
 
-// PathPrefix with mux's PathPrefix, router will replaced by a subrouter that start with prefix.
+// PathPrefix with mux's PathPrefix, router will be replaced by a subrouter that start with prefix.
 func PathPrefix(prefix string) ServerOption {
 	return func(s *Server) {
 		s.router = s.router.PathPrefix(prefix).Subrouter()


Test patch:
diff --git a/transport/grpc/resolver/discovery/builder_test.go b/transport/grpc/resolver/discovery/builder_test.go
index e216a544..64051051 100644
--- a/transport/grpc/resolver/discovery/builder_test.go
+++ b/transport/grpc/resolver/discovery/builder_test.go
@@ -38,6 +38,14 @@ func TestDisableDebugLog(t *testing.T) {
 	}
 }
 
+func TestPrintDebugLog(t *testing.T) {
+	o := &builder{}
+	PrintDebugLog(true)(o)
+	if !o.debugLog {
+		t.Errorf("expected PrintdebugLog true, got %v", o.debugLog)
+	}
+}
+
 type mockDiscovery struct{}
 
 func (m *mockDiscovery) GetService(_ context.Context, _ string) ([]*registry.ServiceInstance, error) {