Skip to content

Commit 54778b6

Browse files
authored
cleanup init functions (#977)
1 parent d00dff7 commit 54778b6

File tree

6 files changed

+27
-47
lines changed

6 files changed

+27
-47
lines changed

compress/zlib.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,11 @@ import (
1111
const DefaultCompressionLevel = 6
1212

1313
var (
14-
zlibReaderPool *sync.Pool
15-
zlibWriterPool sync.Pool
16-
)
17-
18-
func init() {
19-
zlibReaderPool = &sync.Pool{
14+
zlibReaderPool = sync.Pool{
2015
New: func() interface{} {
2116
return nil
2217
},
2318
}
24-
2519
zlibWriterPool = sync.Pool{
2620
New: func() interface{} {
2721
w, err := zlib.NewWriterLevel(new(bytes.Buffer), DefaultCompressionLevel)
@@ -31,7 +25,7 @@ func init() {
3125
return w
3226
},
3327
}
34-
}
28+
)
3529

3630
var _ io.WriteCloser = zlibWriter{}
3731
var _ io.ReadCloser = zlibReader{}

driver/driver.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ var customTLSMutex sync.Mutex
2727
var (
2828
dsnRegex = regexp.MustCompile("@[^@]+/[^@/]+")
2929
customTLSConfigMap = make(map[string]*tls.Config)
30-
options = make(map[string]DriverOption)
30+
options = map[string]DriverOption{
31+
"compress": CompressOption,
32+
"collation": CollationOption,
33+
"readTimeout": ReadTimeoutOption,
34+
"writeTimeout": WriteTimeoutOption,
35+
}
3136

3237
// can be provided by clients to allow more control in handling Go and database
3338
// types beyond the default Value types allowed
@@ -392,11 +397,6 @@ func (r *rows) Next(dest []sqldriver.Value) error {
392397
var driverName = "mysql"
393398

394399
func init() {
395-
options["compress"] = CompressOption
396-
options["collation"] = CollationOption
397-
options["readTimeout"] = ReadTimeoutOption
398-
options["writeTimeout"] = WriteTimeoutOption
399-
400400
sql.Register(driverName, driver{})
401401
}
402402

dump/parser.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,16 @@ type ParseHandler interface {
2323
Data(schema string, table string, values []string) error
2424
}
2525

26-
var binlogExp *regexp.Regexp
27-
var useExp *regexp.Regexp
28-
var valuesExp *regexp.Regexp
29-
var gtidExp *regexp.Regexp
30-
31-
func init() {
26+
var (
3227
binlogExp = regexp.MustCompile(`^CHANGE (MASTER|REPLICATION SOURCE) TO (MASTER_LOG_FILE|SOURCE_LOG_FILE)='(.+)', (MASTER_LOG_POS|SOURCE_LOG_POS)=(\d+);`)
33-
useExp = regexp.MustCompile("^USE `(.+)`;")
28+
useExp = regexp.MustCompile("^USE `(.+)`;")
3429
valuesExp = regexp.MustCompile("^INSERT INTO `(.+?)` VALUES \\((.+)\\);$")
30+
3531
// The pattern will only match MySQL GTID, as you know SET GLOBAL gtid_slave_pos='0-1-4' is used for MariaDB.
3632
// SET @@GLOBAL.GTID_PURGED='1638041a-0457-11e9-bb9f-00505690b730:1-429405150';
3733
// https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-concepts.html
3834
gtidExp = regexp.MustCompile(`(\w{8}(-\w{4}){3}-\w{12}(:\d+(-\d+)?)+)`)
39-
}
35+
)
4036

4137
// Parse the dump data with Dumper generate.
4238
// It can not parse all the data formats with mysqldump outputs

mysql/util.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,7 @@ func init() {
443443
for i := range EncodeMap {
444444
EncodeMap[i] = DONTESCAPE
445445
}
446-
for i := range EncodeMap {
447-
if to, ok := encodeRef[byte(i)]; ok {
448-
EncodeMap[byte(i)] = to
449-
}
446+
for k, v := range encodeRef {
447+
EncodeMap[k] = v
450448
}
451449
}

replication/time.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ package replication
22

33
import (
44
"fmt"
5-
"strings"
65
"time"
76
)
87

98
var (
10-
fracTimeFormat []string
9+
fracTimeFormat = [7]string{
10+
"2006-01-02 15:04:05",
11+
"2006-01-02 15:04:05.0",
12+
"2006-01-02 15:04:05.00",
13+
"2006-01-02 15:04:05.000",
14+
"2006-01-02 15:04:05.0000",
15+
"2006-01-02 15:04:05.00000",
16+
"2006-01-02 15:04:05.000000",
17+
}
1118
)
1219

1320
// fracTime is a help structure wrapping Golang Time.
@@ -56,12 +63,3 @@ func microSecTimestampToTime(ts uint64) time.Time {
5663
}
5764
return time.Unix(int64(ts/1000000), int64(ts%1000000)*1000)
5865
}
59-
60-
func init() {
61-
fracTimeFormat = make([]string, 7)
62-
fracTimeFormat[0] = "2006-01-02 15:04:05"
63-
64-
for i := 1; i <= 6; i++ {
65-
fracTimeFormat[i] = fmt.Sprintf("2006-01-02 15:04:05.%s", strings.Repeat("0", i))
66-
}
67-
}

server/stmt.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@ import (
1010
"github.com/pingcap/errors"
1111
)
1212

13-
var paramFieldData []byte
14-
var columnFieldData []byte
15-
16-
func init() {
17-
var p = &Field{Name: []byte("?")}
18-
var c = &Field{}
19-
20-
paramFieldData = p.Dump()
21-
columnFieldData = c.Dump()
22-
}
13+
var (
14+
paramFieldData = (&Field{Name: []byte("?")}).Dump()
15+
columnFieldData = (&Field{}).Dump()
16+
)
2317

2418
type Stmt struct {
2519
ID uint32

0 commit comments

Comments
 (0)