From c422dd11b87bf2cc78db4a1747afd0aeee68f657 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 31 Oct 2023 03:41:15 +0800 Subject: [PATCH] Add default connection attribute '_server_host' --- connector.go | 9 ++++++--- const.go | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/connector.go b/connector.go index c4ea5bc25..6e8d65f6d 100644 --- a/connector.go +++ b/connector.go @@ -22,7 +22,7 @@ type connector struct { encodedAttributes string // Encoded connection attributes. } -func encodeConnectionAttributes(textAttributes string) string { +func encodeConnectionAttributes(cfg *Config) string { connAttrsBuf := make([]byte, 0) // default connection attributes @@ -34,9 +34,12 @@ func encodeConnectionAttributes(textAttributes string) string { connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrPlatformValue) connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrPid) connAttrsBuf = appendLengthEncodedString(connAttrsBuf, strconv.Itoa(os.Getpid())) + connAttrsBuf = appendLengthEncodedString(connAttrsBuf, connAttrServerHost) + serverHost, _, _ := net.SplitHostPort(cfg.Addr) + connAttrsBuf = appendLengthEncodedString(connAttrsBuf, serverHost) // user-defined connection attributes - for _, connAttr := range strings.Split(textAttributes, ",") { + for _, connAttr := range strings.Split(cfg.ConnectionAttributes, ",") { k, v, found := strings.Cut(connAttr, ":") if !found { continue @@ -49,7 +52,7 @@ func encodeConnectionAttributes(textAttributes string) string { } func newConnector(cfg *Config) *connector { - encodedAttributes := encodeConnectionAttributes(cfg.ConnectionAttributes) + encodedAttributes := encodeConnectionAttributes(cfg) return &connector{ cfg: cfg, encodedAttributes: encodedAttributes, diff --git a/const.go b/const.go index 0f2621a6f..22526e031 100644 --- a/const.go +++ b/const.go @@ -26,6 +26,7 @@ const ( connAttrPlatform = "_platform" connAttrPlatformValue = runtime.GOARCH connAttrPid = "_pid" + connAttrServerHost = "_server_host" ) // MySQL constants documentation: