1111
1212#include < glog/logging.h>
1313
14- static time_t kCheckDiff = 1 ;
14+ static time_t kCheckDiff = 5 ;
1515
1616RedisSender::RedisSender (int id, std::string ip, int64_t port, std::string password):
1717 id_(id),
@@ -24,82 +24,75 @@ RedisSender::RedisSender(int id, std::string ip, int64_t port, std::string passw
2424 elements_(0 ) {
2525
2626 last_write_time_ = ::time (NULL );
27+ cli_ = std::shared_ptr<net::NetCli>(net::NewRedisCli ());
28+ cli_->set_connect_timeout (1000 );
29+ cli_->set_recv_timeout (10000 );
30+ cli_->set_send_timeout (10000 );
2731}
2832
2933RedisSender::~RedisSender () {
30- LOG (INFO) << " RedisSender thread " << id_ << " exit!!!" ;
3134}
3235
3336void RedisSender::ConnectRedis () {
34- while (cli_ == NULL ) {
35- // Connect to redis
36- cli_ = std::shared_ptr<net::NetCli>(net::NewRedisCli ());
37- cli_->set_connect_timeout (1000 );
38- cli_->set_recv_timeout (10000 );
39- cli_->set_send_timeout (10000 );
37+ while (true ) {
4038 pstd::Status s = cli_->Connect (ip_, port_);
4139 if (!s.ok ()) {
4240 LOG (WARNING) << " Can not connect to " << ip_ << " :" << port_ << " , status: " << s.ToString ();
43- cli_ = NULL ;
44- sleep (3 );
4541 continue ;
4642 } else {
4743 // Connect success
4844 LOG (INFO) << " RedisSender thread " << id_ << " Connect to redis(" << ip_ << " :" << port_ << " ) success" ;
49- // Authentication
50- if (!password_.empty ()) {
51- net::RedisCmdArgsType argv, resp;
52- std::string cmd;
53-
54- argv.push_back (" AUTH" );
55- argv.push_back (password_);
56- net::SerializeRedisCommand (argv, &cmd);
57- pstd::Status s = cli_->Send (&cmd);
58-
59- if (s.ok ()) {
60- s = cli_->Recv (&resp);
61- if (resp[0 ] == " OK" ) {
62- } else {
63- LOG (FATAL) << " Connect to redis(" << ip_ << " :" << port_ << " ) Invalid password" ;
64- cli_->Close ();
65- cli_ = NULL ;
66- should_exit_ = true ;
67- return ;
68- }
69- } else {
70- LOG (WARNING) << " send auth failed: " << s.ToString ();
71- cli_->Close ();
72- cli_ = NULL ;
73- continue ;
74- }
45+ if (!Authenticate ()) {
46+ cli_->Close ();
47+ continue ;
48+ }
49+ break ;
50+ }
51+ }
52+ }
53+
54+ bool RedisSender::Authenticate () {
55+ if (!password_.empty ()) {
56+ net::RedisCmdArgsType argv, resp;
57+ std::string cmd;
58+
59+ argv.push_back (" AUTH" );
60+ argv.push_back (password_);
61+ net::SerializeRedisCommand (argv, &cmd);
62+ pstd::Status s = cli_->Send (&cmd);
63+
64+ if (s.ok ()) {
65+ s = cli_->Recv (&resp);
66+ if (resp[0 ] == " OK" ) {
67+ return true ;
7568 } else {
76- // If forget to input password
77- net::RedisCmdArgsType argv, resp;
78- std::string cmd;
79-
80- argv.push_back (" PING" );
81- net::SerializeRedisCommand (argv, &cmd);
82- pstd::Status s = cli_->Send (&cmd);
83-
84- if (s.ok ()) {
85- s = cli_->Recv (&resp);
86- if (s.ok ()) {
87- if (resp[0 ] == " NOAUTH Authentication required." ) {
88- LOG (FATAL) << " Ping redis(" << ip_ << " :" << port_ << " ) NOAUTH Authentication required" ;
89- cli_->Close ();
90- cli_ = NULL ;
91- should_exit_ = true ;
92- return ;
93- }
94- } else {
95- LOG (WARNING) << s.ToString ();
96- cli_->Close ();
97- cli_ = NULL ;
98- }
99- }
69+ LOG (ERROR) << " Connect to redis(" << ip_ << " :" << port_ << " ) Invalid password" ;
70+ return false ;
10071 }
72+ } else {
73+ LOG (WARNING) << " send auth failed: " << s.ToString ();
74+ return false ;
75+ }
76+ } else {
77+ net::RedisCmdArgsType argv, resp;
78+ std::string cmd;
79+
80+ argv.push_back (" PING" );
81+ net::SerializeRedisCommand (argv, &cmd);
82+ pstd::Status s = cli_->Send (&cmd);
83+
84+ if (s.ok ()) {
85+ s = cli_->Recv (&resp);
86+ if (s.ok () && resp[0 ] == " NOAUTH Authentication required." ) {
87+ LOG (ERROR) << " Ping redis(" << ip_ << " :" << port_ << " ) NOAUTH Authentication required" ;
88+ return false ;
89+ }
90+ } else {
91+ LOG (WARNING) << s.ToString ();
92+ return false ;
10193 }
10294 }
95+ return true ;
10396}
10497
10598void RedisSender::Stop () {
@@ -122,11 +115,8 @@ void RedisSender::SendRedisCommand(const std::string &command) {
122115int RedisSender::SendCommand (std::string &command) {
123116 time_t now = ::time (NULL );
124117 if (kCheckDiff < now - last_write_time_) {
125- int ret = cli_->CheckAliveness ();
126- if (ret < 0 ) {
127- cli_ = NULL ;
118+ cli_->Close ();
128119 ConnectRedis ();
129- }
130120 last_write_time_ = now;
131121 }
132122
@@ -140,10 +130,9 @@ int RedisSender::SendCommand(std::string &command) {
140130 }
141131
142132 cli_->Close ();
143- cli_ = NULL ;
144133 ConnectRedis ();
145134 } while (++idx < 3 );
146- LOG (WARNING ) << " RedisSender " << id_ << " fails to send redis command " << command << " , times: " << idx << " , error: " << " send command failed" ;
135+ LOG (ERROR ) << " RedisSender " << id_ << " fails to send redis command " << command << " , times: " << idx << " , error: " << " send command failed" ;
147136 return -1 ;
148137}
149138
@@ -182,7 +171,7 @@ void *RedisSender::ThreadMain() {
182171 }
183172
184173 LOG (INFO) << " RedisSender thread " << id_ << " complete" ;
185- cli_ = NULL ;
174+ cli_-> Close () ;
186175 return NULL ;
187176}
188177
0 commit comments