Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RoundRobinCodisPool可能导致访问错误的指针 #7

Open
typhoonzero opened this issue Aug 29, 2016 · 3 comments
Open

RoundRobinCodisPool可能导致访问错误的指针 #7

typhoonzero opened this issue Aug 29, 2016 · 3 comments
Assignees

Comments

@typhoonzero
Copy link
Contributor

在BfdCodis.cpp中:

BfdCodis::BfdCodis(const string& zookeeperAddr, const string& proxyPath, const string& businessID)
{
    m_Pool = new RoundRobinCodisPool(zookeeperAddr, proxyPath, businessID);
}

BfdCodis::~BfdCodis()
{
    if (m_Pool = NULL)
    {
        delete m_Pool;
        m_Pool = NULL;
    }
}

bool BfdCodis::exists(string key, int tt)
{
    return m_Pool->GetProxy()->exists(key, tt);
}

int BfdCodis::del(string key, int tt)
{
    return m_Pool->GetProxy()->del(key, tt);
}

这段代码的m_Pool->GetProxy()得到的CodisClient实例,有可能会在RoundRobinCodisPool中被析构。

建议的更新方法:

  • 仿照RedisClientPool.cpp的方法,GetProxy()返回的指针要从vector中pop出来
  • 减少调用的层数,在RoundRobinCodisPool中维持一个Map, key是proxy地址,value是一个vector,包含这个proxy的链接池。每次轮转这个Map从中获取一个proxy,并从链接池中pop一个连接出来使用,并在使用完成后归还。
  • RoundRobinCodisPool的析构要在连接池内容都被归还之后才能开始
@typhoonzero
Copy link
Contributor Author

typhoonzero commented Aug 30, 2016

一个高效的写法:

    {
    ScopedLock lock(m_Mutex);
    int idx = ++proxyIndex % m_Proxys.size()
    }
    return m_Proxys[idx];

@weim0000
Copy link
Contributor

有一天proxyIndex会溢出

@weim0000
Copy link
Contributor

可以这样:int idx = crc32(key)%m_Proxys.size()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants