suggestion: leaderPath + "/" + ExpectedPrimary
116 // write a flag to indicate the current primary has exited
117 resp, err := kv.NewSlowLogTxn(client).
118 Then(
119 clientv3.OpPut(strings.Join([]string{leaderPath, ExpectedPrimary}, "/"), string(leaderRaw), clientv3.WithLease(grantResp.ID)),120 // indicate the current primary has exited
121 clientv3.OpDelete(leaderPath)).
122 Commit()
suggestion: leaderPath + "/" + ExpectedPrimary
92 log.Info("remove expected primary key", zap.String("leader-path", leaderPath))
93 // remove expected leader key
94 resp, err := kv.NewSlowLogTxn(client).
95 Then(clientv3.OpDelete(strings.Join([]string{leaderPath, ExpectedPrimary}, "/"))). 96 Commit()
97 if err != nil || !resp.Succeeded {
98 log.Error("change expected primary error", errs.ZapError(err))
suggestion: leaderPath + "/" + ExpectedPrimary
76
77// GetExpectedPrimary indicates API has changed the primary, ONLY SET VALUE BY API.
78func GetExpectedPrimary(client *clientv3.Client, leaderPath string) string {
79 primary, err := etcdutil.GetValue(client, strings.Join([]string{leaderPath, ExpectedPrimary}, "/")) 80 if err != nil {
81 log.Error("get expected primary key error", errs.ZapError(err))
82 return ""
Description
Some string concatenation operations such as strings.Join
called with a single
or a two element slice can be simplified using binary operators.
This also improves performance as it doesn't need to allocate a new slice and iterate through it.
Bad practice
func foo(x, y string) string {
return strings.Join([]string{x, y}, "_")
}
Recommended
func foo(x, y string) string {
return x + "_" + y
}