-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add maxAllowedPacket DSN Parameter #489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
50b3c01
e78ecc8
1793086
873fcb6
b3f09b7
9e928e1
84d597b
9ab50a7
4c1b46f
b77594f
c1f7d07
a049a66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,15 @@ Default: 0 | |
I/O write timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. | ||
|
||
|
||
##### `maxPacketAllowed` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
``` | ||
Type: decimal number | ||
Default: 0 | ||
``` | ||
|
||
Max MySQL packet size allowed in bytes. Use `maxPacketAllowed` == 0 to fetch the `max_allowed_packet` variable from server. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "MySQL" and replace " There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also add "automatically" before "fetch" |
||
|
||
|
||
##### System Variables | ||
|
||
All other parameters are interpreted as system variables: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,13 +109,17 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) { | |
return nil, err | ||
} | ||
|
||
// Get max allowed packet size | ||
maxap, err := mc.getSystemVar("max_allowed_packet") | ||
if err != nil { | ||
mc.Close() | ||
return nil, err | ||
if mc.cfg.MaxPacketAllowed > 0 { | ||
mc.maxPacketAllowed = mc.cfg.MaxPacketAllowed | ||
} else { | ||
// Get max allowed packet size | ||
maxap, err := mc.getSystemVar("max_allowed_packet") | ||
if err != nil { | ||
mc.Close() | ||
return nil, err | ||
} | ||
mc.maxPacketAllowed = stringToInt(maxap) - 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
} | ||
mc.maxPacketAllowed = stringToInt(maxap) - 1 | ||
if mc.maxPacketAllowed < maxPacketSize { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
mc.maxWriteSize = mc.maxPacketAllowed | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"net/url" | ||
"strings" | ||
"time" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
|
@@ -51,6 +52,7 @@ type Config struct { | |
MultiStatements bool // Allow multiple statements in one query | ||
ParseTime bool // Parse time values to time.Time | ||
Strict bool // Return warnings as errors | ||
MaxPacketAllowed int // Max packet size allowed | ||
} | ||
|
||
// FormatDSN formats the given Config into a DSN string which can be passed to | ||
|
@@ -222,6 +224,17 @@ func (cfg *Config) FormatDSN() string { | |
buf.WriteString(cfg.WriteTimeout.String()) | ||
} | ||
|
||
if cfg.MaxPacketAllowed > 0 { | ||
if hasParam { | ||
buf.WriteString("&maxPacketAllowed=") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
} else { | ||
hasParam = true | ||
buf.WriteString("?maxPacketAllowed=") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
} | ||
buf.WriteString(strconv.Itoa(cfg.MaxPacketAllowed)) | ||
|
||
} | ||
|
||
// other params | ||
if cfg.Params != nil { | ||
for param, value := range cfg.Params { | ||
|
@@ -496,7 +509,11 @@ func parseDSNParams(cfg *Config, params string) (err error) { | |
if err != nil { | ||
return | ||
} | ||
|
||
case "maxPacketAllowed": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here |
||
cfg.MaxPacketAllowed, err = strconv.Atoi(value) | ||
if err != nil { | ||
return | ||
} | ||
default: | ||
// lazy init | ||
if cfg.Params == nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,8 +39,8 @@ var testDSNs = []struct { | |
"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify", | ||
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8mb4,utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, TLSConfig: "skip-verify"}, | ||
}, { | ||
"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci", | ||
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true}, | ||
"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxPacketAllowed=16777216", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here |
||
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxPacketAllowed: 16777216}, | ||
}, { | ||
"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local", | ||
&Config{User: "user", Passwd: "p@ss(word)", Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:80", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.Local}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move your entry to the end (alphabetical order)