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

有概率无法获取连接吗? #308

Open
ye-xiaowei opened this issue Sep 25, 2022 · 1 comment
Open

有概率无法获取连接吗? #308

ye-xiaowei opened this issue Sep 25, 2022 · 1 comment

Comments

@ye-xiaowei
Copy link

我看到源码中 RandomSelectStrategy#randomGet (在这)

private Connection randomGet(List<Connection> connections) {
        if (null == connections || connections.isEmpty()) {
            return null;
        }

        int size = connections.size();
        int tries = 0;
        Connection result = null;
        while ((result == null || !result.isFine()) && tries++ < MAX_TIMES) {
            result = connections.get(this.random.nextInt(size));
        }

        if (result != null && !result.isFine()) {
            result = null;
        }
        return result;
    }

如果 connections 有部分 !conn.isFine() 而在 MAX_TIMES 此后仍然没有找到 可用的连接就会返回null 吗。
为什么不是在筛选出 fine 的连接再 random 出来呢?

@chuailiwu
Copy link
Collaborator

考虑!conn.isFine() 为小概率事件,每次都要塞选一遍复杂度变成o(n)了

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

2 participants