@@ -54,6 +54,21 @@ type Config struct {
54
54
MultiStatements bool // Allow multiple statements in one query
55
55
ParseTime bool // Parse time values to time.Time
56
56
Strict bool // Return warnings as errors
57
+
58
+ // RejectreadOnly causes mysql driver to reject read-only connections. This
59
+ // is specifically for AWS Aurora: During a failover, there seems to be a
60
+ // race condition on Aurora, where we get connected to the [old master
61
+ // before failover], i.e. the [new read-only slave after failover].
62
+ //
63
+ // Note that this should be a fairly rare case, as automatic failover
64
+ // normally happens when master is down, and the race condition shouldn't
65
+ // happen unless it comes back up online as soon as the failover is kicked
66
+ // off. But it's pretty easy to reproduce using a manual failover. In case
67
+ // this happens, we should reconnect to the Aurora cluster by returning a
68
+ // driver.ErrBadConnection.
69
+ //
70
+ // tl;dr: Set this if you are using Aurora.
71
+ RejectReadOnly bool
57
72
}
58
73
59
74
// FormatDSN formats the given Config into a DSN string which can be passed to
@@ -195,6 +210,15 @@ func (cfg *Config) FormatDSN() string {
195
210
buf .WriteString (cfg .ReadTimeout .String ())
196
211
}
197
212
213
+ if cfg .RejectReadOnly {
214
+ if hasParam {
215
+ buf .WriteString ("&rejectReadOnly=true" )
216
+ } else {
217
+ hasParam = true
218
+ buf .WriteString ("?rejectReadOnly=true" )
219
+ }
220
+ }
221
+
198
222
if cfg .Strict {
199
223
if hasParam {
200
224
buf .WriteString ("&strict=true" )
@@ -472,6 +496,14 @@ func parseDSNParams(cfg *Config, params string) (err error) {
472
496
return
473
497
}
474
498
499
+ // Reject read-only connections
500
+ case "rejectReadOnly" :
501
+ var isBool bool
502
+ cfg .RejectReadOnly , isBool = readBool (value )
503
+ if ! isBool {
504
+ return errors .New ("invalid bool value: " + value )
505
+ }
506
+
475
507
// Strict mode
476
508
case "strict" :
477
509
var isBool bool
0 commit comments