@@ -133,7 +133,7 @@ RedisConn::RedisConn(const int fd,
133
133
const std::string &ip_port,
134
134
ServerThread *thread)
135
135
: PinkConn(fd, ip_port, thread),
136
- wbuf_size_ (REDIS_MAX_MESSAGE ),
136
+ wbuf_size_ (DEFAULT_WBUF_SIZE ),
137
137
wbuf_len_(0 ),
138
138
last_read_pos_(-1 ),
139
139
next_parse_pos_(0 ),
@@ -144,7 +144,7 @@ RedisConn::RedisConn(const int fd,
144
144
is_overtake_(false ),
145
145
wbuf_pos_(0 ) {
146
146
rbuf_ = reinterpret_cast <char *>(malloc (sizeof (char ) * REDIS_MAX_MESSAGE));
147
- wbuf_ = reinterpret_cast <char *>(malloc (sizeof (char ) * REDIS_MAX_MESSAGE ));
147
+ wbuf_ = reinterpret_cast <char *>(malloc (sizeof (char ) * DEFAULT_WBUF_SIZE ));
148
148
}
149
149
150
150
RedisConn::~RedisConn () {
@@ -282,14 +282,19 @@ void RedisConn::ResetClient() {
282
282
bulk_len_ = -1 ;
283
283
}
284
284
285
- bool RedisConn::ExpandWbuf () {
286
- if (wbuf_size_ >= (uint32_t )(REDIS_MAX_MESSAGE * (uint32_t )32 )) {
285
+ bool RedisConn::ExpandWbufTo (uint32_t new_size) {
286
+ if (new_size <= wbuf_size_) {
287
+ return true ;
288
+ }
289
+ void * new_wbuf = realloc (wbuf_, new_size);
290
+ if (new_wbuf != nullptr ) {
291
+ wbuf_ = reinterpret_cast <char *>(new_wbuf);
292
+ wbuf_size_ = new_size;
293
+ return true ;
294
+ } else {
287
295
wbuf_pos_ = 0 ;
288
296
return false ;
289
297
}
290
- wbuf_size_ *= 2 ;
291
- wbuf_ = reinterpret_cast <char *>(realloc (wbuf_, wbuf_size_));
292
- return true ;
293
298
}
294
299
295
300
ReadStatus RedisConn::ProcessInputBuffer () {
@@ -379,6 +384,12 @@ WriteStatus RedisConn::SendReply() {
379
384
if (wbuf_pos_ == wbuf_len_) {
380
385
wbuf_len_ = 0 ;
381
386
wbuf_pos_ = 0 ;
387
+ if (wbuf_size_ > DEFAULT_WBUF_SIZE) {
388
+ free (wbuf_);
389
+ wbuf_ = reinterpret_cast <char *>(malloc (sizeof (char ) *
390
+ DEFAULT_WBUF_SIZE));
391
+ wbuf_size_ = DEFAULT_WBUF_SIZE;
392
+ }
382
393
}
383
394
}
384
395
if (nwritten == -1 ) {
0 commit comments