File tree Expand file tree Collapse file tree 3 files changed +60
-5
lines changed Expand file tree Collapse file tree 3 files changed +60
-5
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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.
577578func (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
585589func (b * BinlogSyncer ) writeRegisterSlaveCommand () error {
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments