Skip to content

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

Merged
merged 12 commits into from
Oct 26, 2016

Conversation

xiezhenye
Copy link
Contributor

@xiezhenye xiezhenye commented Oct 20, 2016

Description

Add a maxPacketAllowed DSN param and a config field to set it directly instead of fetch from server.

Some mysql proxy implements or other services using mysql protocol may not supports 'select @@max_allowed_packet'. maxPacketAllowed param can be used to walk around these problems.

It can also be used to limit the max row size returned from server.

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

@methane
Copy link
Member

methane commented Oct 20, 2016

Some proxy implements not supports 'select @@max_allowed_packet' query.

I feel this sentence is worth enough to include in README.
(Many options doesn't have enough description about what is motivation to use the option.)

@julienschmidt
Copy link
Member

julienschmidt commented Oct 20, 2016

What we currently do is indeed a hack we should maybe avoid in the future. It might be a good idea to just use MySQL's default value by default, allow modification via the maxPacketAllowed DSN parameter and use the current behavior only when explicitly set, e.g. via maxPacketAllowed=auto.

Anyway, @xiezhenye please use the PR template (edit you post) and make the necessary additions.

@arnehormann
Copy link
Member

I prefer maxAllowedPacket to maxPacketAllowed so naming is consistent with the variable in MySQL. But if the name was intentional: for what the reason?

@@ -43,6 +43,7 @@ Soroush Pour <me at soroushjp.com>
Stan Putrya <root.vagner at gmail.com>
Stanley Gunawan <gunawan.stanley at gmail.com>
Xiaobing Jiang <s7v7nislands at gmail.com>
Zhenye Xie <xiezhenye at gmail.com>
Copy link
Member

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)

Default: 0
```

Max MySQL packet size allowed in bytes. Use `maxPacketAllowed` == 0 to fetch the `max_allowed_packet` variable from server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "MySQL" and replace "maxPacketAllowed == 0" with "Use maxPacketAllowed = 0" (single =).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add "automatically" before "fetch"

@julienschmidt
Copy link
Member

@arnehormann's comment is still valid. The parameter name should be consistent with the variable name (max_allowed_packet). Therefore it should be maxAllowedPacket.
Please also change that, other than that, the PR looks fine to me.

@julienschmidt julienschmidt changed the title add a maxPacketAllowed dsn param Add maxAllowedPacket DSN Parameter Oct 25, 2016
@@ -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`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxAllowedPacket also here and in DSN

@arnehormann arnehormann mentioned this pull request Oct 26, 2016
5 tasks
Default: 0
```

Max packet size allowed in bytes. Use `maxAllowedPacket` = 0 to automatically fetch the `max_allowed_packet` variable from server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write it as maxAllowedPacket=0 to make it consistent with other variables.

mc.Close()
return nil, err
if mc.cfg.MaxAllowedPacket > 0 {
mc.maxPacketAllowed = mc.cfg.MaxAllowedPacket
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also change the name here

mc.Close()
return nil, err
}
mc.maxPacketAllowed = stringToInt(maxap) - 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

}
mc.maxPacketAllowed = stringToInt(maxap) - 1
if mc.maxPacketAllowed < maxPacketSize {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

@@ -222,6 +224,17 @@ func (cfg *Config) FormatDSN() string {
buf.WriteString(cfg.WriteTimeout.String())
}

if cfg.MaxAllowedPacket > 0 {
if hasParam {
buf.WriteString("&maxPacketAllowed=")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

buf.WriteString("&maxPacketAllowed=")
} else {
hasParam = true
buf.WriteString("?maxPacketAllowed=")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

@@ -496,7 +509,11 @@ func parseDSNParams(cfg *Config, params string) (err error) {
if err != nil {
return
}

case "maxPacketAllowed":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here

@@ -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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

@@ -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
MaxAllowedPacket int // Max packet size allowed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move it after Loc *time.Location // Location for time.Time values.
We just put all the bool vars at the end for more efficient struct packing.

Default: 0
```

Max packet size allowed in bytes. Use `maxAllowedPacket`=0 to automatically fetch the `max_allowed_packet` variable from server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`maxAllowedPacket=0`

@julienschmidt julienschmidt merged commit b81e73c into go-sql-driver:master Oct 26, 2016
@julienschmidt
Copy link
Member

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants