Commit 2a8056e6 authored by gejun's avatar gejun

polish code related to redis-auth

parent 035cce37
...@@ -116,7 +116,7 @@ friend void policy::ProcessMongoRequest(InputMessageBase*); ...@@ -116,7 +116,7 @@ friend void policy::ProcessMongoRequest(InputMessageBase*);
static const uint32_t FLAGS_PB_BYTES_TO_BASE64 = (1 << 11); static const uint32_t FLAGS_PB_BYTES_TO_BASE64 = (1 << 11);
static const uint32_t FLAGS_ALLOW_DONE_TO_RUN_IN_PLACE = (1 << 12); static const uint32_t FLAGS_ALLOW_DONE_TO_RUN_IN_PLACE = (1 << 12);
static const uint32_t FLAGS_USED_BY_RPC = (1 << 13); static const uint32_t FLAGS_USED_BY_RPC = (1 << 13);
static const uint32_t FLAGS_REQUEST_WITH_AUTH = (1 << 14); static const uint32_t FLAGS_REQUEST_WITH_AUTH = (1 << 15);
public: public:
Controller(); Controller();
......
...@@ -125,11 +125,9 @@ public: ...@@ -125,11 +125,9 @@ public:
void set_readable_progressive_attachment(ReadableProgressiveAttachment* s) void set_readable_progressive_attachment(ReadableProgressiveAttachment* s)
{ _cntl->_rpa.reset(s); } { _cntl->_rpa.reset(s); }
ControllerPrivateAccessor &set_with_auth(bool with_auth) { void add_with_auth() {
_cntl->set_flag(Controller::FLAGS_REQUEST_WITH_AUTH, with_auth); _cntl->add_flag(Controller::FLAGS_REQUEST_WITH_AUTH);
return *this;
} }
bool with_auth() const { return _cntl->has_flag(Controller::FLAGS_REQUEST_WITH_AUTH); }
private: private:
Controller* _cntl; Controller* _cntl;
}; };
......
...@@ -24,7 +24,7 @@ namespace policy { ...@@ -24,7 +24,7 @@ namespace policy {
// Request to redis for authentication. // Request to redis for authentication.
class RedisAuthenticator : public Authenticator { class RedisAuthenticator : public Authenticator {
public: public:
RedisAuthenticator(const std::string& passwd) RedisAuthenticator(const std::string& passwd)
: passwd_(passwd) {} : passwd_(passwd) {}
...@@ -35,7 +35,7 @@ class RedisAuthenticator : public Authenticator { ...@@ -35,7 +35,7 @@ class RedisAuthenticator : public Authenticator {
return 0; return 0;
} }
private: private:
const std::string passwd_; const std::string passwd_;
}; };
......
...@@ -188,9 +188,7 @@ void PackRedisRequest(butil::IOBuf* buf, ...@@ -188,9 +188,7 @@ void PackRedisRequest(butil::IOBuf* buf,
return cntl->SetFailed(EREQUEST, "Fail to generate credential"); return cntl->SetFailed(EREQUEST, "Fail to generate credential");
} }
buf->append(auth_str); buf->append(auth_str);
ControllerPrivateAccessor(cntl).set_with_auth(true); ControllerPrivateAccessor(cntl).add_with_auth();
} else {
ControllerPrivateAccessor(cntl).set_with_auth(false);
} }
buf->append(request); buf->append(request);
......
...@@ -86,7 +86,6 @@ BRPC_VALIDATE_GFLAG(connect_timeout_as_unreachable, ...@@ -86,7 +86,6 @@ BRPC_VALIDATE_GFLAG(connect_timeout_as_unreachable,
validate_connect_timeout_as_unreachable); validate_connect_timeout_as_unreachable);
const int WAIT_EPOLLOUT_TIMEOUT_MS = 50; const int WAIT_EPOLLOUT_TIMEOUT_MS = 50;
static const uint32_t REDIS_AUTH_FLAG = (1ul << 15);
#ifdef BAIDU_INTERNAL #ifdef BAIDU_INTERNAL
#define BRPC_AUXTHREAD_ATTR \ #define BRPC_AUXTHREAD_ATTR \
...@@ -305,9 +304,12 @@ struct BAIDU_CACHELINE_ALIGNMENT Socket::WriteRequest { ...@@ -305,9 +304,12 @@ struct BAIDU_CACHELINE_ALIGNMENT Socket::WriteRequest {
Socket* socket; Socket* socket;
uint32_t pipelined_count() const { uint32_t pipelined_count() const {
return (_pc_and_udmsg >> 48) & 0xFFFF; return (_pc_and_udmsg >> 48) & 0x7FFF;
} }
void clear_pipelined_count() { bool is_with_auth() const {
return _pc_and_udmsg & 0x8000000000000000ULL;
}
void clear_pipelined_count_and_with_auth() {
_pc_and_udmsg &= 0xFFFFFFFFFFFFULL; _pc_and_udmsg &= 0xFFFFFFFFFFFFULL;
} }
SocketMessage* user_message() const { SocketMessage* user_message() const {
...@@ -318,8 +320,8 @@ struct BAIDU_CACHELINE_ALIGNMENT Socket::WriteRequest { ...@@ -318,8 +320,8 @@ struct BAIDU_CACHELINE_ALIGNMENT Socket::WriteRequest {
} }
void set_pipelined_count_and_user_message( void set_pipelined_count_and_user_message(
uint32_t pc, SocketMessage* msg, bool with_auth) { uint32_t pc, SocketMessage* msg, bool with_auth) {
if (pc != 0 && with_auth) { if (with_auth) {
pc |= REDIS_AUTH_FLAG; pc |= (1 << 15);
} }
_pc_and_udmsg = ((uint64_t)pc << 48) | (uint64_t)(uintptr_t)msg; _pc_and_udmsg = ((uint64_t)pc << 48) | (uint64_t)(uintptr_t)msg;
} }
...@@ -371,10 +373,10 @@ void Socket::WriteRequest::Setup(Socket* s) { ...@@ -371,10 +373,10 @@ void Socket::WriteRequest::Setup(Socket* s) {
// which is common in cache servers: memcache, redis... // which is common in cache servers: memcache, redis...
// The struct will be popped when reading a message from the socket. // The struct will be popped when reading a message from the socket.
PipelinedInfo pi; PipelinedInfo pi;
pi.count = pc & (~REDIS_AUTH_FLAG); pi.count = pc;
pi.with_auth = pc & REDIS_AUTH_FLAG; pi.with_auth = is_with_auth();
pi.id_wait = id_wait; pi.id_wait = id_wait;
clear_pipelined_count(); // avoid being pushed again clear_pipelined_count_and_with_auth(); // avoid being pushed again
s->PushPipelinedInfo(pi); s->PushPipelinedInfo(pi);
} }
} }
......
...@@ -215,15 +215,20 @@ public: ...@@ -215,15 +215,20 @@ public:
// Default: 0 // Default: 0
uint32_t pipelined_count; uint32_t pipelined_count;
// [Only effective when pipelined_count is non-zero]
// The request contains authenticating information which will be
// responded by the server and processed specially when dealing
// with the response.
bool with_auth;
// Do not return EOVERCROWDED // Do not return EOVERCROWDED
// Default: false // Default: false
bool ignore_eovercrowded; bool ignore_eovercrowded;
bool with_auth;
WriteOptions() WriteOptions()
: id_wait(INVALID_BTHREAD_ID), abstime(NULL) : id_wait(INVALID_BTHREAD_ID), abstime(NULL)
, pipelined_count(0), ignore_eovercrowded(false), with_auth(false) {} , pipelined_count(0), with_auth(false)
, ignore_eovercrowded(false) {}
}; };
int Write(butil::IOBuf *msg, const WriteOptions* options = NULL); int Write(butil::IOBuf *msg, const WriteOptions* options = NULL);
......
// Copyright (c) 2014 Baidu, Inc. // Copyright (c) 2014 Baidu, Inc.
// Date: Thu Jun 11 14:30:07 CST 2015 // Date: Thu Jun 11 14:30:07 CST 2015
#ifdef BAIDU_INTERNAL #if defined(BAIDU_INTERNAL)
#include <iostream> #include <iostream>
#include "butil/time.h" #include "butil/time.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment