Skip to content

Commit 0413ff8

Browse files
committed
replication: check hostname length
1 parent 81ff5fd commit 0413ff8

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

canal/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ type Config struct {
105105
// Set Dialer
106106
Dialer client.Dialer
107107

108-
// Set Localhost
108+
// Set the hostname that is used when registering as replica. This is similar to `report_host` in MySQL.
109+
// This will be truncated if it is longer than 255 characters.
109110
Localhost string
110111

111112
// EventCacheCount is the capacity of the BinlogStreamer internal event channel.

replication/binlogsyncer.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,17 @@ func (b *BinlogSyncer) writeBinlogDumpMariadbGTIDCommand(gset mysql.GTIDSet) err
573573
return b.writeBinlogDumpCommand(mysql.Position{Name: "", Pos: 0})
574574
}
575575

576-
// localHostname returns the hostname that register slave would register as.
576+
// localHostname returns the hostname that register replica would register as.
577+
// this gets truncated to 255 bytes.
577578
func (b *BinlogSyncer) localHostname() string {
578-
if len(b.cfg.Localhost) == 0 {
579-
h, _ := os.Hostname()
579+
h := b.cfg.Localhost
580+
if len(h) == 0 {
581+
h, _ = os.Hostname()
582+
}
583+
if len(h) <= 255 {
580584
return h
581585
}
582-
return b.cfg.Localhost
586+
return h[:255]
583587
}
584588

585589
func (b *BinlogSyncer) writeRegisterSlaveCommand() error {

replication/binlogsyncer_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package replication
2+
3+
import (
4+
"os"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestLocalHostname(t *testing.T) {
12+
b := BinlogSyncer{
13+
cfg: BinlogSyncerConfig{
14+
Localhost: "foobar",
15+
},
16+
}
17+
18+
require.Equal(t, "foobar", b.localHostname())
19+
}
20+
21+
func TestLocalHostname_long(t *testing.T) {
22+
b := BinlogSyncer{
23+
cfg: BinlogSyncerConfig{
24+
Localhost: strings.Repeat("x", 255),
25+
},
26+
}
27+
28+
require.Equal(t, 255, len(b.localHostname()))
29+
}
30+
31+
func TestLocalHostname_toolong(t *testing.T) {
32+
b := BinlogSyncer{
33+
cfg: BinlogSyncerConfig{
34+
Localhost: strings.Repeat("x", 300),
35+
},
36+
}
37+
38+
require.Equal(t, 255, len(b.localHostname()))
39+
}
40+
41+
func TestLocalHostname_os(t *testing.T) {
42+
b := BinlogSyncer{
43+
cfg: BinlogSyncerConfig{
44+
Localhost: "",
45+
},
46+
}
47+
48+
h, _ := os.Hostname()
49+
require.Equal(t, h, b.localHostname())
50+
}

0 commit comments

Comments
 (0)