Alanxtl commented on code in PR #3404:
URL: https://github.com/apache/dubbo-go/pull/3404#discussion_r3407486568
##########
common/url.go:
##########
@@ -395,21 +395,51 @@ func isMatchCategory(category1 string, category2 string)
bool {
}
func (c *URL) String() string {
- c.paramsLock.Lock()
- defer c.paramsLock.Unlock()
+ c.paramsLock.RLock()
+ defer c.paramsLock.RUnlock()
+
+ encodedParams := c.params.Encode()
var buf strings.Builder
- if len(c.Username) == 0 && len(c.Password) == 0 {
- buf.WriteString(fmt.Sprintf("%s://%s:%s%s", c.Protocol, c.Ip,
c.Port, c.Path))
- } else {
- buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s", c.Protocol,
c.Username, c.Password, c.Ip, c.Port, c.Path))
+ size := c.stringPrefixLen()
+ if encodedParams != "" {
+ size += len("?") + len(encodedParams)
}
- if encoded := c.params.Encode(); encoded != "" {
+ buf.Grow(size)
+ c.writeStringPrefix(&buf)
+ if encodedParams != "" {
buf.WriteByte('?')
- buf.WriteString(encoded)
+ buf.WriteString(encodedParams)
}
return buf.String()
}
+func (c *URL) stringPrefixLen() int {
+ size := len(c.Protocol) + len("://") + len(c.Ip) + len(":") +
len(c.Port) + len(c.Path)
+ if c.hasAuth() {
+ size += len(c.Username) + len(":") + len(c.Password) + len("@")
+ }
+ return size
+}
+
+func (c *URL) writeStringPrefix(buf *strings.Builder) {
+ buf.WriteString(c.Protocol)
+ buf.WriteString("://")
+ if c.hasAuth() {
+ buf.WriteString(c.Username)
+ buf.WriteString(":")
+ buf.WriteString(c.Password)
+ buf.WriteString("@")
+ }
+ buf.WriteString(c.Ip)
+ buf.WriteString(":")
+ buf.WriteString(c.Port)
+ buf.WriteString(c.Path)
+}
+
+func (c *URL) hasAuth() bool {
+ return len(c.Username) != 0 || len(c.Password) != 0
+}
Review Comment:
这个就不用单独抽象一个函数了
##########
common/url.go:
##########
@@ -395,21 +395,51 @@ func isMatchCategory(category1 string, category2 string)
bool {
}
func (c *URL) String() string {
- c.paramsLock.Lock()
- defer c.paramsLock.Unlock()
+ c.paramsLock.RLock()
+ defer c.paramsLock.RUnlock()
+
+ encodedParams := c.params.Encode()
var buf strings.Builder
- if len(c.Username) == 0 && len(c.Password) == 0 {
- buf.WriteString(fmt.Sprintf("%s://%s:%s%s", c.Protocol, c.Ip,
c.Port, c.Path))
- } else {
- buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s", c.Protocol,
c.Username, c.Password, c.Ip, c.Port, c.Path))
+ size := c.stringPrefixLen()
+ if encodedParams != "" {
+ size += len("?") + len(encodedParams)
}
- if encoded := c.params.Encode(); encoded != "" {
+ buf.Grow(size)
+ c.writeStringPrefix(&buf)
+ if encodedParams != "" {
buf.WriteByte('?')
- buf.WriteString(encoded)
+ buf.WriteString(encodedParams)
}
return buf.String()
}
+func (c *URL) stringPrefixLen() int {
+ size := len(c.Protocol) + len("://") + len(c.Ip) + len(":") +
len(c.Port) + len(c.Path)
+ if c.hasAuth() {
+ size += len(c.Username) + len(":") + len(c.Password) + len("@")
+ }
+ return size
+}
+
+func (c *URL) writeStringPrefix(buf *strings.Builder) {
+ buf.WriteString(c.Protocol)
+ buf.WriteString("://")
+ if c.hasAuth() {
+ buf.WriteString(c.Username)
+ buf.WriteString(":")
+ buf.WriteString(c.Password)
+ buf.WriteString("@")
+ }
+ buf.WriteString(c.Ip)
+ buf.WriteString(":")
+ buf.WriteString(c.Port)
+ buf.WriteString(c.Path)
+}
Review Comment:
这个也不用单独抽象函数了 又没有别的地方用
##########
common/url.go:
##########
@@ -395,21 +395,51 @@ func isMatchCategory(category1 string, category2 string)
bool {
}
func (c *URL) String() string {
- c.paramsLock.Lock()
- defer c.paramsLock.Unlock()
+ c.paramsLock.RLock()
+ defer c.paramsLock.RUnlock()
+
+ encodedParams := c.params.Encode()
var buf strings.Builder
- if len(c.Username) == 0 && len(c.Password) == 0 {
- buf.WriteString(fmt.Sprintf("%s://%s:%s%s", c.Protocol, c.Ip,
c.Port, c.Path))
- } else {
- buf.WriteString(fmt.Sprintf("%s://%s:%s@%s:%s%s", c.Protocol,
c.Username, c.Password, c.Ip, c.Port, c.Path))
+ size := c.stringPrefixLen()
+ if encodedParams != "" {
+ size += len("?") + len(encodedParams)
}
- if encoded := c.params.Encode(); encoded != "" {
+ buf.Grow(size)
+ c.writeStringPrefix(&buf)
+ if encodedParams != "" {
buf.WriteByte('?')
- buf.WriteString(encoded)
+ buf.WriteString(encodedParams)
}
return buf.String()
}
+func (c *URL) stringPrefixLen() int {
+ size := len(c.Protocol) + len("://") + len(c.Ip) + len(":") +
len(c.Port) + len(c.Path)
+ if c.hasAuth() {
+ size += len(c.Username) + len(":") + len(c.Password) + len("@")
+ }
+ return size
+}
Review Comment:
这个也不用
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]