MySQLConnectionPool.h 2.5 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.


Z
zhiru 已提交
19 20 21 22
#include "mysql++/mysql++.h"

#include <string>
#include <unistd.h>
Z
update  
zhiru 已提交
23
#include <atomic>
Z
zhiru 已提交
24

S
starlord 已提交
25
#include "utils/Log.h"
26

Z
update  
zhiru 已提交
27 28 29 30 31
namespace zilliz {
namespace milvus {
namespace engine {
namespace meta {

Z
zhiru 已提交
32
class MySQLConnectionPool : public mysqlpp::ConnectionPool {
S
starlord 已提交
33
 public:
Z
zhiru 已提交
34 35 36 37 38 39 40
    // The object's only constructor
    MySQLConnectionPool(std::string dbName,
                        std::string userName,
                        std::string passWord,
                        std::string serverIp,
                        int port = 0,
                        int maxPoolSize = 8) :
S
starlord 已提交
41 42 43 44 45 46
        db_(dbName),
        user_(userName),
        password_(passWord),
        server_(serverIp),
        port_(port),
        max_pool_size_(maxPoolSize) {
Z
zhiru 已提交
47
        conns_in_use_ = 0;
Z
update  
zhiru 已提交
48
        max_idle_time_ = 10; //10 seconds
Z
zhiru 已提交
49 50 51 52 53 54 55 56
    }

    // The destructor.  We _must_ call ConnectionPool::clear() here,
    // because our superclass can't do it for us.
    ~MySQLConnectionPool() override {
        clear();
    }

Z
update  
zhiru 已提交
57
    mysqlpp::Connection *grab() override;
Z
zhiru 已提交
58 59

    // Other half of in-use conn count limit
Z
update  
zhiru 已提交
60
    void release(const mysqlpp::Connection *pc) override;
Z
zhiru 已提交
61

S
starlord 已提交
62 63 64
//    int getConnectionsInUse();
//
//    void set_max_idle_time(int max_idle);
Z
zhiru 已提交
65

Z
update  
zhiru 已提交
66
    std::string getDB();
Z
update  
zhiru 已提交
67

S
starlord 已提交
68
 protected:
Z
zhiru 已提交
69
    // Superclass overrides
Z
update  
zhiru 已提交
70
    mysqlpp::Connection *create() override;
Z
zhiru 已提交
71

Z
update  
zhiru 已提交
72
    void destroy(mysqlpp::Connection *cp) override;
Z
zhiru 已提交
73

Z
update  
zhiru 已提交
74
    unsigned int max_idle_time() override;
Z
zhiru 已提交
75

S
starlord 已提交
76
 private:
Z
zhiru 已提交
77
    // Number of connections currently in use
Z
update  
zhiru 已提交
78
    std::atomic<int> conns_in_use_;
Z
zhiru 已提交
79 80 81 82 83

    // Our connection parameters
    std::string db_, user_, password_, server_;
    int port_;

Z
update  
zhiru 已提交
84
    int max_pool_size_;
Z
zhiru 已提交
85

Z
update  
zhiru 已提交
86
    unsigned int max_idle_time_;
Z
update  
zhiru 已提交
87 88 89 90 91
};

} // namespace meta
} // namespace engine
} // namespace milvus
S
starlord 已提交
92
} // namespace zilliz