Skip to content

Commit eb92a8d

Browse files
do not query max_allowed_packet by default (go-sql-driver#680)
1 parent ee359f9 commit eb92a8d

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ Please keep in mind, that param values must be [url.QueryEscape](https://golang.
232232
##### `maxAllowedPacket`
233233
```
234234
Type: decimal number
235-
Default: 0
235+
Default: 4194304
236236
```
237237

238-
Max packet size allowed in bytes. Use `maxAllowedPacket=0` to automatically fetch the `max_allowed_packet` variable from server.
238+
Max packet size allowed in bytes. The default value is 4 MiB and should be adjusted to match the server settings. `maxAllowedPacket=0` can be used to automatically fetch the `max_allowed_packet` variable from server *on every connection*.
239239

240240
##### `multiStatements`
241241

const.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
package mysql
1010

1111
const (
12-
minProtocolVersion byte = 10
12+
defaultMaxAllowedPacket = 4 << 20 // 4 MiB
13+
minProtocolVersion = 10
1314
maxPacketSize = 1<<24 - 1
1415
timeFormat = "2006-01-02 15:04:05.999999"
1516
)

driver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ func TestUint64(t *testing.T) {
964964
}
965965

966966
func TestLongData(t *testing.T) {
967-
runTests(t, dsn, func(dbt *DBTest) {
967+
runTests(t, dsn+"&maxAllowedPacket=0", func(dbt *DBTest) {
968968
var maxAllowedPacketSize int
969969
err := dbt.db.QueryRow("select @@max_allowed_packet").Scan(&maxAllowedPacketSize)
970970
if err != nil {

dsn.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func NewConfig() *Config {
6464
return &Config{
6565
Collation: defaultCollation,
6666
Loc: time.UTC,
67+
MaxAllowedPacket: defaultMaxAllowedPacket,
6768
AllowNativePasswords: true,
6869
}
6970
}
@@ -274,7 +275,7 @@ func (cfg *Config) FormatDSN() string {
274275
buf.WriteString(cfg.WriteTimeout.String())
275276
}
276277

277-
if cfg.MaxAllowedPacket > 0 {
278+
if cfg.MaxAllowedPacket != defaultMaxAllowedPacket {
278279
if hasParam {
279280
buf.WriteString("&maxAllowedPacket=")
280281
} else {

dsn_test.go

+24-26
Original file line numberDiff line numberDiff line change
@@ -22,55 +22,55 @@ var testDSNs = []struct {
2222
out *Config
2323
}{{
2424
"username:password@protocol(address)/dbname?param=value",
25-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
25+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
2626
}, {
2727
"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true",
28-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, ColumnsWithAlias: true},
28+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, ColumnsWithAlias: true},
2929
}, {
3030
"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true",
31-
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, ColumnsWithAlias: true, MultiStatements: true},
31+
&Config{User: "username", Passwd: "password", Net: "protocol", Addr: "address", DBName: "dbname", Params: map[string]string{"param": "value"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, ColumnsWithAlias: true, MultiStatements: true},
3232
}, {
3333
"user@unix(/path/to/socket)/dbname?charset=utf8",
34-
&Config{User: "user", Net: "unix", Addr: "/path/to/socket", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
34+
&Config{User: "user", Net: "unix", Addr: "/path/to/socket", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
3535
}, {
3636
"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true",
37-
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true, TLSConfig: "true"},
37+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "localhost:5555", DBName: "dbname", Params: map[string]string{"charset": "utf8"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, TLSConfig: "true"},
3838
}, {
3939
"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify",
40-
&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, AllowNativePasswords: true, TLSConfig: "skip-verify"},
40+
&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, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true, TLSConfig: "skip-verify"},
4141
}, {
4242
"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci&maxAllowedPacket=16777216",
4343
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8mb4_unicode_ci", Loc: time.UTC, AllowNativePasswords: true, Timeout: 30 * time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second, AllowAllFiles: true, AllowOldPasswords: true, ClientFoundRows: true, MaxAllowedPacket: 16777216},
4444
}, {
45-
"user:password@/dbname?allowNativePasswords=false",
46-
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: false},
45+
"user:password@/dbname?allowNativePasswords=false&maxAllowedPacket=0",
46+
&Config{User: "user", Passwd: "password", Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: 0, AllowNativePasswords: false},
4747
}, {
4848
"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local",
49-
&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, AllowNativePasswords: true},
49+
&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, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
5050
}, {
5151
"/dbname",
52-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
52+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
5353
}, {
5454
"@/",
55-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
55+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
5656
}, {
5757
"/",
58-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
58+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
5959
}, {
6060
"",
61-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
61+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
6262
}, {
6363
"user:p@/ssword@/",
64-
&Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
64+
&Config{User: "user", Passwd: "p@/ssword", Net: "tcp", Addr: "127.0.0.1:3306", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
6565
}, {
6666
"unix/?arg=%2Fsome%2Fpath.ext",
67-
&Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
67+
&Config{Net: "unix", Addr: "/tmp/mysql.sock", Params: map[string]string{"arg": "/some/path.ext"}, Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
6868
}, {
6969
"tcp(127.0.0.1)/dbname",
70-
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
70+
&Config{Net: "tcp", Addr: "127.0.0.1:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
7171
}, {
7272
"tcp(de:ad:be:ef::ca:fe)/dbname",
73-
&Config{Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, AllowNativePasswords: true},
73+
&Config{Net: "tcp", Addr: "[de:ad:be:ef::ca:fe]:3306", DBName: "dbname", Collation: "utf8_general_ci", Loc: time.UTC, MaxAllowedPacket: defaultMaxAllowedPacket, AllowNativePasswords: true},
7474
},
7575
}
7676

@@ -233,16 +233,14 @@ func TestDSNUnsafeCollation(t *testing.T) {
233233

234234
func TestParamsAreSorted(t *testing.T) {
235235
expected := "/dbname?interpolateParams=true&foobar=baz&quux=loo"
236-
dsn := &Config{
237-
DBName: "dbname",
238-
InterpolateParams: true,
239-
AllowNativePasswords: true,
240-
Params: map[string]string{
241-
"quux": "loo",
242-
"foobar": "baz",
243-
},
236+
cfg := NewConfig()
237+
cfg.DBName = "dbname"
238+
cfg.InterpolateParams = true
239+
cfg.Params = map[string]string{
240+
"quux": "loo",
241+
"foobar": "baz",
244242
}
245-
actual := dsn.FormatDSN()
243+
actual := cfg.FormatDSN()
246244
if actual != expected {
247245
t.Errorf("generic Config.Params were not sorted: want %#v, got %#v", expected, actual)
248246
}

0 commit comments

Comments
 (0)