Skip to content

Commit 2d0ffd1

Browse files
FIX-245: Add TLS config for canal (#496)
* FIX-245: Add TLS config for canal
1 parent 7107693 commit 2d0ffd1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

canal/canal.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ func (c *Canal) prepareSyncer() error {
425425
SemiSyncEnabled: c.cfg.SemiSyncEnabled,
426426
MaxReconnectAttempts: c.cfg.MaxReconnectAttempts,
427427
TimestampStringLocation: c.cfg.TimestampStringLocation,
428+
TLSConfig: c.cfg.TLSConfig,
428429
}
429430

430431
if strings.Contains(c.cfg.Addr, "/") {
@@ -453,11 +454,16 @@ func (c *Canal) prepareSyncer() error {
453454
func (c *Canal) Execute(cmd string, args ...interface{}) (rr *mysql.Result, err error) {
454455
c.connLock.Lock()
455456
defer c.connLock.Unlock()
456-
457+
argF := make([]func(*client.Conn), 0)
458+
if c.cfg.TLSConfig != nil {
459+
argF = append(argF, func(conn *client.Conn) {
460+
conn.SetTLSConfig(c.cfg.TLSConfig)
461+
})
462+
}
457463
retryNum := 3
458464
for i := 0; i < retryNum; i++ {
459465
if c.conn == nil {
460-
c.conn, err = client.Connect(c.cfg.Addr, c.cfg.User, c.cfg.Password, "")
466+
c.conn, err = client.Connect(c.cfg.Addr, c.cfg.User, c.cfg.Password, "", argF...)
461467
if err != nil {
462468
return nil, errors.Trace(err)
463469
}

canal/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package canal
22

33
import (
4+
"crypto/tls"
45
"io/ioutil"
56
"math/rand"
67
"time"
@@ -79,6 +80,9 @@ type Config struct {
7980
// Set to change the maximum number of attempts to re-establish a broken
8081
// connection
8182
MaxReconnectAttempts int `toml:"max_reconnect_attempts"`
83+
84+
// Set TLS config
85+
TLSConfig *tls.Config
8286
}
8387

8488
func NewConfigWithFile(name string) (*Config, error) {

0 commit comments

Comments
 (0)