znone
2017-02-15 76949c0bf4e34f15a5f9b1c2b870477c7dbeb6b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef _QTL_DATABASE_POOL_H_
#define _QTL_DATABASE_POOL_H_
 
#include <memory>
#include <vector>
#include <atomic>
#include <thread>
#include <mutex>
#include <chrono>
#include <algorithm>
 
namespace qtl
{
 
template<typename Database>
class database_pool
{
public:
    typedef Database value_type;
    typedef std::shared_ptr<Database> pointer;
 
    database_pool()
        : m_trying_connection(false), m_stop_thread(false)
    {
    }
 
    virtual ~database_pool()
    {
        if(m_background_thread.joinable())
        {
            m_stop_thread=true;
            m_background_thread.join();
        }
        clear();
    }
 
    pointer get()
    {
        Database* db=popup();
        if(db==NULL && m_trying_connection==false)
            db=create_database();
        return pointer(db, [this](Database* db) {
            recovery(db);
        });
    }
 
    bool test_alive()
    {
        if(m_databases.empty()) 
            return false;
        std::unique_lock<std::mutex> lock(m_pool_mutex);
        auto it=m_databases.begin();
        while(it!=m_databases.end())
        {
            Database* db=*it;
            if(!db->is_alive())
            {
                delete db;
                it=m_databases.erase(it);
            }
            else
            {
                ++it;
            }
        }
        if(m_databases.empty())
        {
            lock.unlock();
            try_connect();
            return false;
        }
        else return true;
    }
 
private:
    std::vector<Database*> m_databases;
    std::mutex m_pool_mutex;
    std::atomic<bool> m_trying_connection;
    std::thread m_background_thread;
    bool m_stop_thread;
 
    virtual bool open_database(Database& db)=0;
    void recovery(Database* db)
    {
        if(db==NULL) return;
        if(db->is_alive())
        {
            std::lock_guard<std::mutex> lock(m_pool_mutex);
            m_databases.push_back(db);
        }
        else
        {
            delete db;
            {
                std::lock_guard<std::mutex> lock(m_pool_mutex);
                clear();
            }
            try_connect();
        }
    }
 
    Database* create_database()
    {
        Database* db=new Database;
        if(open_database(*db))
            return db;
 
        {
            std::lock_guard<std::mutex> lock(m_pool_mutex);
            clear();
        }
        try_connect();
        return NULL;
    }
 
    Database* popup()
    {
        Database* db=NULL;
        std::lock_guard<std::mutex> lock(m_pool_mutex);
        if(!m_databases.empty())
        {
            db=m_databases.back();
            m_databases.pop_back();
        }
        return db;
    }
 
    void try_connect()
    {
        if(m_trying_connection)
            return;
 
        m_trying_connection=true;
        try
        {
            m_background_thread=std::thread(&database_pool<Database>::background_connect, this);
        }
        catch (std::system_error&)
        {
            m_trying_connection=false;
        }
    }
 
    void background_connect()
    {
        Database* db=NULL;
        int interval=1;
        while(db==NULL && m_stop_thread==false)
        {
            db=create_database();
            if(db==NULL)
            {
                std::this_thread::sleep_for(std::chrono::seconds(interval));
                if(interval<60) interval<<=1;
            }
        }
        if(db)
        {
            recovery(db);
        }
        m_background_thread.detach();
        m_trying_connection=false;
    }
 
    void clear()
    {
        std::for_each(m_databases.begin(), m_databases.end(), std::default_delete<Database>());
        m_databases.clear();
    }
};
 
}
 
#endif //_QTL_DATABASE_POOL_H_