@@ -57,7 +57,7 @@ func SaveTwemproxyConfig() {
5757
5858func RestartTwemproxy () error {
5959 Debug ("Restarting Twemproxy." )
60- args := strings .Split (Setting .RestartArgs , string (' ' ))
60+ args := strings .Split (Settings .RestartArgs , string (' ' ))
6161 out , err := exec .Command (Settings .RestartCommand , args ... ).Output ()
6262
6363 if err != nil {
@@ -67,9 +67,19 @@ func RestartTwemproxy() error {
6767 return err
6868}
6969
70- func GetSentinel () (sentinel string ) {
71- address := ComposeRedisAddress (Settings .SentinelIp , Settings .SentinelPort )
72- return address
70+ func GetSentinel () (redis.Conn , error ) {
71+ var c redis.Conn
72+ var err error
73+ for i := range Settings .Sentinels {
74+ c , err = redis .Dial ("tcp" , Settings .Sentinels [i ])
75+ if err == nil {
76+ Debug (fmt .Sprintf ("Connected to sentinel %s" , Settings .Sentinels [i ]))
77+ break ;
78+ }
79+ Debug (fmt .Sprintf ("Sentinel %s is not reachable" , Settings .Sentinels [i ]))
80+ }
81+
82+ return c , err
7383}
7484
7585func SwitchMaster (master_name string , ip string , port string ) error {
@@ -84,7 +94,7 @@ func SwitchMaster(master_name string, ip string, port string) error {
8494}
8595
8696func ValidateCurrentMaster () error {
87- c , err := redis . Dial ( "tcp" , GetSentinel () )
97+ c , err := GetSentinel ()
8898 if err != nil {
8999 return err
90100 }
@@ -111,10 +121,9 @@ func ValidateCurrentMaster() error {
111121}
112122
113123func SubscribeToSentinel () {
114- sentinel := GetSentinel ()
115- c , err := redis .Dial ("tcp" , sentinel )
124+ c , err := GetSentinel ()
116125 if err != nil {
117- Fatal ("Cannot connect to redis sentinel:" , sentinel )
126+ Fatal ("Cannot connect to any sentinel." )
118127 }
119128
120129 err = ValidateCurrentMaster ()
@@ -133,7 +142,15 @@ func SubscribeToSentinel() {
133142 case redis.Subscription :
134143 Debug (fmt .Sprintf ("%s: %s %d" , v .Channel , v .Kind , v .Count ))
135144 case error :
136- Fatal ("Error with redis connection:" , psc )
145+ Debug ("Subscription error, trying to fallback" )
146+ c , err = GetSentinel ()
147+ if err != nil {
148+ Fatal ("Error with redis connection:" , psc )
149+ }
150+
151+ psc = redis.PubSubConn {c }
152+ Debug ("Subscribing to sentinel (+switch-master)." )
153+ psc .Subscribe ("+switch-master" )
137154 }
138155 }
139156}
0 commit comments