今天面試某后臺c++職位

今天碰到個很棘手的問題,因為這邊我們服務器架構 ,網路是多線程 但是邏輯是單線程的,就出現了個問題,假如我此時一個操作去訪問 redis 或者mysql? 數據量過大,線程阻塞了,那么邏輯線程 其他消息將會延遲,如何優化?

問題如下:假如1000個玩家 都要去查詢 1000名排行榜 必須在mysql 查找。

我的回答如下:

1.當時我就說設計不會出現這種情況,排序的名次,肯定是在內存實時動態更新的。(那賤逼,說我就要去mysql查,不考慮其他內存情況)

2.?做連接池,或者mysql開辟多線程可以解決一定性能問題(但是問題始終無法解決出現線程阻塞問題,無論架構設計,只要去mysql 查詢數據)

3 模擬hiredis 使用異步回調。(那賤逼說我們不管redis提供的,如果寫如何回調)

我回到家里,看了下hiredis 果然思路和我閑的一模一樣,就是說最終我們要做成 ?a->b ? 假如b此時阻塞,但是a還是繼續跑,b 阻塞結束,在通信a ? ? a在接受消息 -> client

hiredis 同步接口問題

int redisGetReply(redisContext *c, void **reply) {

int wdone = 0;

void *aux = NULL;

/* Try to read pending replies */

if (redisGetReplyFromReader(c,&aux) == REDIS_ERR)

return REDIS_ERR;

/* For the blocking context, flush output buffer and read reply */

if (aux == NULL && c->flags & REDIS_BLOCK) {

/* Write until done */

do {

if (redisBufferWrite(c,&wdone) == REDIS_ERR)// write ?假如此時是select * from player

return REDIS_ERR;

} while (!wdone);

/* Read until there is a reply */

do {

if (redisBufferRead(c) == REDIS_ERR)//然后read mysql 獲取的player 信息 問題在這里,這里是阻塞的,玩意數據過大,就呵呵了。

return REDIS_ERR;

if (redisGetReplyFromReader(c,&aux) == REDIS_ERR)

return REDIS_ERR;

} while (aux == NULL);

}

/* Set reply object */

if (reply != NULL) *reply = aux;

return REDIS_OK;}hiredis 異步接口問題

/* Helper function for the redisAsyncCommand* family of functions. Writes a

* formatted command to the output buffer and registers the provided callback

* function with the context. */

static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *cmd, size_t len) {

redisContext *c = &(ac->c);

redisCallback cb;//就是說我先把我需要的回調函數注冊到一個list當中。然后在下一次read 出來在回調這個cb 這樣就是異步回調模式了。這樣變解決了,單邏輯線程出現的阻塞問題。

int pvariant, hasnext;

const char *cstr, *astr;

size_t clen, alen;

const char *p;

sds sname;

int ret;

/* Don't accept new commands when the connection is about to be closed. */

if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;

/* Setup callback */

cb.fn = fn;

cb.privdata = privdata;

/* Find out which command will be appended. */

p = nextArgument(cmd,&cstr,&clen);

assert(p != NULL);

hasnext = (p[0] == '$');

pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;

cstr += pvariant;

clen -= pvariant;

if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) {

c->flags |= REDIS_SUBSCRIBED;

/* Add every channel/pattern to the list of subscription callbacks. */

while ((p = nextArgument(p,&astr,&alen)) != NULL) {

sname = sdsnewlen(astr,alen);

if (pvariant)

ret = dictReplace(ac->sub.patterns,sname,&cb);

else

ret = dictReplace(ac->sub.channels,sname,&cb);

if (ret == 0) sdsfree(sname);

}

} else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) {

/* It is only useful to call (P)UNSUBSCRIBE when the context is

* subscribed to one or more channels or patterns. */

if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;

/* (P)UNSUBSCRIBE does not have its own response: every channel or

* pattern that is unsubscribed will receive a message. This means we

* should not append a callback function for this command. */

} else if(strncasecmp(cstr,"monitor\r\n",9) == 0) {

/* Set monitor flag and push callback */

c->flags |= REDIS_MONITORING;

__redisPushCallback(&ac->replies,&cb);

} else {

if (c->flags & REDIS_SUBSCRIBED)

/* This will likely result in an error reply, but it needs to be

* received and passed to the callback. */

__redisPushCallback(&ac->sub.invalid,&cb);

else

__redisPushCallback(&ac->replies,&cb);

}

__redisAppendCommand(c,cmd,len);

/* Always schedule a write when the write buffer is non-empty */

_EL_ADD_WRITE(ac);

return REDIS_OK;

}

int redisvAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, const char *format, va_list ap) {

char *cmd;

int len;

int status;

len = redisvFormatCommand(&cmd,format,ap);

/* We don't want to pass -1 or -2 to future functions as a length. */

if (len < 0)

return REDIS_ERR;

status = __redisAsyncCommand(ac,fn,privdata,cmd,len);

free(cmd);

return status;

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容