Repository: go-kratos/kratos
Pull request #2236 (2022-07-25 18:26:21)

build(deps): bump github.com/go-zookeeper/zk from 1.0.2 to 1.0.3 in /contrib/registry/zookeeper
Bumps [github.com/go-zookeeper/zk](https://github.com/go-zookeeper/zk) from 1.0.2 to 1.0.3.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/go-zookeeper/zk/releases">github.com/go-zookeeper/zk's releases</a>.</em></p>
<blockquote>
<h2>v1.0.3</h2>
<h2>What's Changed</h2>
<ul>
<li>Global EventNotWatching events for session lost by <a href="https://github.com/dragonsinth"><code>@​dragonsinth</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/34">go-zookeeper/zk#34</a></li>
<li>Optimize comparison by <a href="https://github.com/moredure"><code>@​moredure</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/55">go-zookeeper/zk#55</a></li>
<li>[doc] add some go doc in conn.go by <a href="https://github.com/linsite"><code>@​linsite</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/60">go-zookeeper/zk#60</a></li>
<li>[doc] add more method docs by <a href="https://github.com/linsite"><code>@​linsite</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/61">go-zookeeper/zk#61</a></li>
<li>fix some typos and add some comments to constants.go by <a href="https://github.com/linsite"><code>@​linsite</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/68">go-zookeeper/zk#68</a></li>
<li>fix link error by <a href="https://github.com/horizonzy"><code>@​horizonzy</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/70">go-zookeeper/zk#70</a></li>
<li>fix the set of events received by ChildrenW by <a href="https://github.com/sakateka"><code>@​sakateka</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/66">go-zookeeper/zk#66</a></li>
<li>Add Server var to ServerStats struct in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/73">go-zookeeper/zk#73</a></li>
<li>Fix parseSeq incorrectly parsing negative sequence number by <a href="https://github.com/colinmcintosh"><code>@​colinmcintosh</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/65">go-zookeeper/zk#65</a></li>
<li>Fix deadlock caused by non-responsive ZK servers by <a href="https://github.com/tobio"><code>@​tobio</code></a> in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/80">go-zookeeper/zk#80</a></li>
</ul>
<h2>New Contributors</h2>
<ul>
<li><a href="https://github.com/dragonsinth"><code>@​dragonsinth</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/34">go-zookeeper/zk#34</a></li>
<li><a href="https://github.com/moredure"><code>@​moredure</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/55">go-zookeeper/zk#55</a></li>
<li><a href="https://github.com/linsite"><code>@​linsite</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/60">go-zookeeper/zk#60</a></li>
<li><a href="https://github.com/horizonzy"><code>@​horizonzy</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/70">go-zookeeper/zk#70</a></li>
<li><a href="https://github.com/sakateka"><code>@​sakateka</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/66">go-zookeeper/zk#66</a></li>
<li><a href="https://github.com/colinmcintosh"><code>@​colinmcintosh</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/65">go-zookeeper/zk#65</a></li>
<li><a href="https://github.com/tobio"><code>@​tobio</code></a> made their first contribution in <a href="https://github-redirect.dependabot.com/go-zookeeper/zk/pull/80">go-zookeeper/zk#80</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a href="https://github.com/go-zookeeper/zk/compare/v1.0.2...v1.0.3">https://github.com/go-zookeeper/zk/compare/v1.0.2...v1.0.3</a></p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/go-zookeeper/zk/commit/abd6db4a30359a595ed2d94db8a2ee329228c755"><code>abd6db4</code></a> Close the listener after a single request</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/463724b40a5d37dadeaf68214bb4484b217ec2f3"><code>463724b</code></a> Fix deadlock caused by non-responsive ZK servers</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/21b4929e1a6ec56e790268b7c4cf5acd67ea2121"><code>21b4929</code></a> Fixed parseSeq so it doesn't strip the negative symbol off sequence numbers.</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/c9bbb5d9b632e7a00845e89fcd3245b0c39db172"><code>c9bbb5d</code></a> Added unit test for parsing negative sequence numbers.</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/d32f05d6916e12ba0126f1a9093f3ff80439fe48"><code>d32f05d</code></a> append Server even when error connecting</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/92883354ac93161a62ba68fd71b48e22b7e6674e"><code>9288335</code></a> Add Server var to ServerStats struct</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/566b763f33d32dffac7daa6d5fdd3a5a54fb5791"><code>566b763</code></a> add test TestNotifyWatches</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/90e39fa3f56e2b8e8eac0e9a8ca78dfc8a009e8e"><code>90e39fa</code></a> fix the set of events received by ChildrenW</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/a87d7eb5874ea2a7363b0ae181c4c9991f261c79"><code>a87d7eb</code></a> fix link error</li>
<li><a href="https://github.com/go-zookeeper/zk/commit/a930334f2cfd404016dcdb890d06130029fd6ec3"><code>a930334</code></a> fix some typos and add some comments to constants.go</li>
<li>Additional commits viewable in <a href="https://github.com/go-zookeeper/zk/compare/v1.0.2...v1.0.3">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=github.com/go-zookeeper/zk&package-manager=go_modules&previous-version=1.0.2&new-version=1.0.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>


Patch:
diff --git a/cmd/kratos/internal/proto/server/server.go b/cmd/kratos/internal/proto/server/server.go
index dcc19790..3b8a8196 100644
--- a/cmd/kratos/internal/proto/server/server.go
+++ b/cmd/kratos/internal/proto/server/server.go
@@ -9,6 +9,8 @@ import (
 
 	"github.com/emicklei/proto"
 	"github.com/spf13/cobra"
+	"golang.org/x/text/cases"
+	"golang.org/x/text/language"
 )
 
 // CmdServer the service command.
@@ -54,7 +56,7 @@ func run(cmd *cobra.Command, args []string) {
 		proto.WithService(func(s *proto.Service) {
 			cs := &Service{
 				Package: pkg,
-				Service: s.Name,
+				Service: serviceName(s.Name),
 			}
 			for _, e := range s.Elements {
 				r, ok := e.(*proto.RPC)
@@ -62,7 +64,7 @@ func run(cmd *cobra.Command, args []string) {
 					continue
 				}
 				cs.Methods = append(cs.Methods, &Method{
-					Service: s.Name, Name: ucFirst(r.Name), Request: r.RequestType,
+					Service: serviceName(s.Name), Name: serviceName(r.Name), Request: r.RequestType,
 					Reply: r.ReturnsType, Type: getMethodType(r.StreamsRequest, r.StreamsReturns),
 				})
 			}
@@ -103,10 +105,12 @@ func getMethodType(streamsRequest, streamsReturns bool) MethodType {
 	return unaryType
 }
 
-func ucFirst(str string) string {
-	if str == "" {
-		return ""
-	}
+func serviceName(name string) string {
+	return toUpperCamelCase(strings.Split(name, ".")[0])
+}
 
-	return strings.ToUpper(str[:1]) + str[1:]
+func toUpperCamelCase(s string) string {
+	s = strings.ReplaceAll(s, "_", " ")
+	s = cases.Title(language.Und, cases.NoLower).String(s)
+	return strings.ReplaceAll(s, " ", "")
 }
diff --git a/cmd/kratos/internal/run/run.go b/cmd/kratos/internal/run/run.go
index a84d45ba..ac229bab 100644
--- a/cmd/kratos/internal/run/run.go
+++ b/cmd/kratos/internal/run/run.go
@@ -23,8 +23,9 @@ var CmdRun = &cobra.Command{
 // Run run project.
 func Run(cmd *cobra.Command, args []string) {
 	var dir string
-	if len(args) > 0 {
-		dir = args[0]
+	cmdArgs, programArgs := splitArgs(cmd, args)
+	if len(cmdArgs) > 0 {
+		dir = cmdArgs[0]
 	}
 	base, err := os.Getwd()
 	if err != nil {
@@ -63,7 +64,7 @@ func Run(cmd *cobra.Command, args []string) {
 			dir = cmdPath[dir]
 		}
 	}
-	fd := exec.Command("go", "run", ".")
+	fd := exec.Command("go", append([]string{"run", "."}, programArgs...)...)
 	fd.Stdout = os.Stdout
 	fd.Stderr = os.Stderr
 	fd.Dir = dir
@@ -73,6 +74,14 @@ func Run(cmd *cobra.Command, args []string) {
 	}
 }
 
+func splitArgs(cmd *cobra.Command, args []string) (cmdArgs, programArgs []string) {
+	dashAt := cmd.ArgsLenAtDash()
+	if dashAt >= 0 {
+		return args[:dashAt], args[dashAt:]
+	}
+	return args, []string{}
+}
+
 func findCMD(base string) (map[string]string, error) {
 	wd, err := os.Getwd()
 	if err != nil {
diff --git a/contrib/config/polaris/config.go b/contrib/config/polaris/config.go
index f5d52d18..c3c39ece 100644
--- a/contrib/config/polaris/config.go
+++ b/contrib/config/polaris/config.go
@@ -43,11 +43,11 @@ func WithFileName(fileName string) Option {
 }
 
 type source struct {
-	client  *polaris.ConfigAPI
+	client  polaris.ConfigAPI
 	options *options
 }
 
-func New(client *polaris.ConfigAPI, opts ...Option) (config.Source, error) {
+func New(client polaris.ConfigAPI, opts ...Option) (config.Source, error) {
 	options := &options{
 		namespace: "default",
 		fileGroup: "",
@@ -74,12 +74,7 @@ func New(client *polaris.ConfigAPI, opts ...Option) (config.Source, error) {
 
 // Load return the config values
 func (s *source) Load() ([]*config.KeyValue, error) {
-	configAPI, err := polaris.NewConfigAPI()
-	if err != nil {
-		return nil, err
-	}
-
-	configFile, err := configAPI.GetConfigFile(s.options.namespace, s.options.fileGroup, s.options.fileName)
+	configFile, err := s.client.GetConfigFile(s.options.namespace, s.options.fileGroup, s.options.fileName)
 	if err != nil {
 		fmt.Println("fail to get config.", err)
 		return nil, err
diff --git a/contrib/registry/zookeeper/go.mod b/contrib/registry/zookeeper/go.mod
index 33d5697f..e33c1d93 100644
--- a/contrib/registry/zookeeper/go.mod
+++ b/contrib/registry/zookeeper/go.mod
@@ -4,7 +4,7 @@ go 1.16
 
 require (
 	github.com/go-kratos/kratos/v2 v2.4.0
-	github.com/go-zookeeper/zk v1.0.2
+	github.com/go-zookeeper/zk v1.0.3
 	golang.org/x/sync v0.0.0-20220513210516-0976fa681c29
 )
 
diff --git a/contrib/registry/zookeeper/go.sum b/contrib/registry/zookeeper/go.sum
index c2c80c1a..f5cce467 100644
--- a/contrib/registry/zookeeper/go.sum
+++ b/contrib/registry/zookeeper/go.sum
@@ -28,8 +28,8 @@ github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre
 github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
 github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
 github.com/go-playground/form/v4 v4.2.0/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=
-github.com/go-zookeeper/zk v1.0.2 h1:4mx0EYENAdX/B/rbunjlt5+4RTA/a9SMHBRuSKdGxPM=
-github.com/go-zookeeper/zk v1.0.2/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
+github.com/go-zookeeper/zk v1.0.3 h1:7M2kwOsc//9VeeFiPtf+uSJlVpU66x9Ba5+8XK7/TDg=
+github.com/go-zookeeper/zk v1.0.3/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
 github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
 github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
diff --git a/contrib/registry/zookeeper/register.go b/contrib/registry/zookeeper/register.go
index 2aedc198..a9de43c8 100644
--- a/contrib/registry/zookeeper/register.go
+++ b/contrib/registry/zookeeper/register.go
@@ -3,6 +3,7 @@ package zookeeper
 import (
 	"context"
 	"path"
+	"time"
 
 	"github.com/go-zookeeper/zk"
 	"golang.org/x/sync/singleflight"
@@ -77,6 +78,7 @@ func (r *Registry) Register(ctx context.Context, service *registry.ServiceInstan
 	if err = r.ensureName(servicePath, data, zk.FlagEphemeral); err != nil {
 		return err
 	}
+	go r.reRegister(servicePath, data)
 	return nil
 }
 
@@ -150,3 +152,21 @@ func (r *Registry) ensureName(path string, data []byte, flags int32) error {
 	}
 	return nil
 }
+
+// reRegister re-register data node info when bad connection recovered
+func (r *Registry) reRegister(path string, data []byte) {
+	sessionID := r.conn.SessionID()
+	ticker := time.NewTicker(time.Second)
+	defer ticker.Stop()
+	for range ticker.C {
+		cur := r.conn.SessionID()
+		// sessionID changed
+		if cur > 0 && sessionID != cur {
+			// re-ensureName
+			if err := r.ensureName(path, data, zk.FlagEphemeral); err != nil {
+				return
+			}
+			sessionID = cur
+		}
+	}
+}


Test patch:
diff --git a/cmd/kratos/internal/proto/server/server_test.go b/cmd/kratos/internal/proto/server/server_test.go
index a2cbeee6..fa90f692 100644
--- a/cmd/kratos/internal/proto/server/server_test.go
+++ b/cmd/kratos/internal/proto/server/server_test.go
@@ -2,7 +2,7 @@ package server
 
 import "testing"
 
-func Test_ucFirst(t *testing.T) {
+func Test_serviceName(t *testing.T) {
 	type args struct {
 		str string
 	}
@@ -12,40 +12,50 @@ func Test_ucFirst(t *testing.T) {
 		want string
 	}{
 		{
-			name: "ucFirst on lowercase words",
+			name: "serviceName on lowercase words",
 			args: args{str: "helloworld"},
 			want: "Helloworld",
 		},
 		{
-			name: "ucFirst on uppercase words",
+			name: "serviceName on uppercase words",
 			args: args{str: "HELLOWORLD"},
 			want: "HELLOWORLD",
 		},
 		{
-			name: "ucFirst on lowercase words with spaces",
+			name: "serviceName on lowercase words with spaces",
 			args: args{str: "hello world"},
-			want: "Hello world",
+			want: "HelloWorld",
 		},
 		{
-			name: "ucFirst on uppercase words with spaces",
+			name: "serviceName on uppercase words with spaces",
 			args: args{str: "HELLO WORLD"},
-			want: "HELLO WORLD",
+			want: "HELLOWORLD",
 		},
 		{
-			name: "ucFirst on Lower Camel Case words",
+			name: "serviceName on Lower Camel Case words",
 			args: args{str: "helloWorld"},
 			want: "HelloWorld",
 		},
 		{
-			name: "ucFirst on Upper Camel Case words",
+			name: "serviceName on Lower Camel Case words",
+			args: args{str: "helloWorld"},
+			want: "HelloWorld",
+		},
+		{
+			name: "serviceName on Upper Camel Case words",
 			args: args{str: "HelloWorld"},
 			want: "HelloWorld",
 		},
+		{
+			name: "serviceName on Upper Camel Case words",
+			args: args{str: "hello_world"},
+			want: "HelloWorld",
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			if got := ucFirst(tt.args.str); got != tt.want {
-				t.Errorf("ucFirst() = %v, want %v", got, tt.want)
+			if got := serviceName(tt.args.str); got != tt.want {
+				t.Errorf("serviceName() = %v, want %v", got, tt.want)
 			}
 		})
 	}
diff --git a/contrib/config/polaris/config_test.go b/contrib/config/polaris/config_test.go
index 6dfaeb3f..fc864526 100644
--- a/contrib/config/polaris/config_test.go
+++ b/contrib/config/polaris/config_test.go
@@ -213,7 +213,7 @@ func TestConfig(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	config, err := New(&configAPI, WithNamespace(namespace), WithFileGroup(fileGroup), WithFileName(name))
+	config, err := New(configAPI, WithNamespace(namespace), WithFileGroup(fileGroup), WithFileName(name))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -285,7 +285,7 @@ func TestExtToFormat(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	config, err := New(&configAPI, WithNamespace(namespace), WithFileGroup(fileGroup), WithFileName(name))
+	config, err := New(configAPI, WithNamespace(namespace), WithFileGroup(fileGroup), WithFileName(name))
 	if err != nil {
 		t.Fatal(err)
 	}