Repository: zeromicro/go-zero
Pull request #3008 (2023-03-10 11:36:50)

Added zrpc server custom serverID for custom registration Key when the service is registered on ETCD.
When registering the zrpc server, if you choose the ETCD method to register, the ID in the Publisher used during registration does not support modification in NewRpcPubServer, resulting in the final registration of zrpc to the server, which can only be key+leaseID. Therefore, if you want to add the ServerID field to the configuration file of ETCD, you can **customize the key+id registered in ETCD key.**

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3008](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (116aafb) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/3a493cd6a6898c0efd9c481557d9d2d661210770?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3a493cd) will **decrease** coverage by `0.03%`.
> The diff coverage is `0.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/discov/config.go](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9kaXNjb3YvY29uZmlnLmdv) | `71.42% <0.00%> (-11.91%)` | :arrow_down: |
| [.../internal/clientinterceptors/tracinginterceptor.go](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9pbnRlcm5hbC9jbGllbnRpbnRlcmNlcHRvcnMvdHJhY2luZ2ludGVyY2VwdG9yLmdv) | `96.80% <ø> (ø)` | |
| [zrpc/internal/rpcpubserver.go](https://codecov.io/gh/zeromicro/go-zero/pull/3008?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-enJwYy9pbnRlcm5hbC9ycGNwdWJzZXJ2ZXIuZ28=) | `68.18% <0.00%> (-4.99%)` | :arrow_down: |

ShyunnY: > unit test please, thanks for your PR!

Okay！I have added a test method for the newly added verification ServerID of ETCD. The newly added ServerID verification branch etcd.HasServerID() in the zrpc/internal/rpcpubserver.go file cannot be tested directly.
ShyunnY: Hello, can my pr be adopted, because in most cases, it is still necessary to be able to customize the ServerID. @kevwan 
kevwan: Unit tests didn't cover all the code. But thanks for your PR, I'll do it.

Patch:
diff --git a/core/discov/config.go b/core/discov/config.go
index 1557b4f8..66c088b6 100644
--- a/core/discov/config.go
+++ b/core/discov/config.go
@@ -13,6 +13,7 @@ var (
 type EtcdConf struct {
 	Hosts              []string
 	Key                string
+	ServerID           int64  `json:",optional"`
 	User               string `json:",optional"`
 	Pass               string `json:",optional"`
 	CertFile           string `json:",optional"`
@@ -26,6 +27,11 @@ func (c EtcdConf) HasAccount() bool {
 	return len(c.User) > 0 && len(c.Pass) > 0
 }
 
+// HasServerID returns if ServerID provided.
+func (c EtcdConf) HasServerID() bool {
+	return c.ServerID > 0
+}
+
 // HasTLS returns if TLS CertFile/CertKeyFile/CACertFile are provided.
 func (c EtcdConf) HasTLS() bool {
 	return len(c.CertFile) > 0 && len(c.CertKeyFile) > 0 && len(c.CACertFile) > 0
diff --git a/zrpc/internal/clientinterceptors/tracinginterceptor.go b/zrpc/internal/clientinterceptors/tracinginterceptor.go
index 006c4691..adf814b3 100644
--- a/zrpc/internal/clientinterceptors/tracinginterceptor.go
+++ b/zrpc/internal/clientinterceptors/tracinginterceptor.go
@@ -2,6 +2,7 @@ package clientinterceptors
 
 import (
 	"context"
+	"github.com/zeromicro/go-zero/core/lang"
 	"io"
 
 	ztrace "github.com/zeromicro/go-zero/core/trace"
@@ -94,7 +95,7 @@ type (
 		Finished          chan error
 		desc              *grpc.StreamDesc
 		events            chan streamEvent
-		eventsDone        chan struct{}
+		eventsDone        chan lang.PlaceholderType
 		receivedMessageID int
 		sentMessageID     int
 	}
diff --git a/zrpc/internal/rpcpubserver.go b/zrpc/internal/rpcpubserver.go
index 61b79a7d..58f6a9b3 100644
--- a/zrpc/internal/rpcpubserver.go
+++ b/zrpc/internal/rpcpubserver.go
@@ -26,6 +26,9 @@ func NewRpcPubServer(etcd discov.EtcdConf, listenOn string, middlewares ServerMi
 			pubOpts = append(pubOpts, discov.WithPubEtcdTLS(etcd.CertFile, etcd.CertKeyFile,
 				etcd.CACertFile, etcd.InsecureSkipVerify))
 		}
+		if etcd.HasServerID() {
+			pubOpts = append(pubOpts, discov.WithId(etcd.ServerID))
+		}
 		pubClient := discov.NewPublisher(etcd.Hosts, etcd.Key, pubListenOn, pubOpts...)
 		return pubClient.KeepAlive()
 	}


Test patch:
diff --git a/core/discov/config_test.go b/core/discov/config_test.go
index 63502713..732f7b55 100644
--- a/core/discov/config_test.go
+++ b/core/discov/config_test.go
@@ -80,3 +80,36 @@ func TestEtcdConf_HasAccount(t *testing.T) {
 		assert.Equal(t, test.hasAccount, test.EtcdConf.HasAccount())
 	}
 }
+
+func TestEtcdConf_HasServerID(t *testing.T) {
+	tests := []struct {
+		EtcdConf
+		hasServerID bool
+	}{
+		{
+			EtcdConf: EtcdConf{
+				Hosts:    []string{"any"},
+				ServerID: -1,
+			},
+			hasServerID: false,
+		},
+		{
+			EtcdConf: EtcdConf{
+				Hosts:    []string{"any"},
+				ServerID: 0,
+			},
+			hasServerID: false,
+		},
+		{
+			EtcdConf: EtcdConf{
+				Hosts:    []string{"any"},
+				ServerID: 10000,
+			},
+			hasServerID: true,
+		},
+	}
+
+	for _, test := range tests {
+		assert.Equal(t, test.hasServerID, test.EtcdConf.HasServerID())
+	}
+}