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
// 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.

S
starlord 已提交
18
#include <mysql++/mysql++.h>
Z
zhiru 已提交
19 20

#include <unistd.h>
Z
update  
zhiru 已提交
21
#include <atomic>
S
starlord 已提交
22
#include <string>
Z
zhiru 已提交
23

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

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

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

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

S
starlord 已提交
52 53
    mysqlpp::Connection*
    grab() override;
Z
zhiru 已提交
54 55

    // Other half of in-use conn count limit
S
starlord 已提交
56 57
    void
    release(const mysqlpp::Connection* pc) override;
Z
zhiru 已提交
58

S
starlord 已提交
59 60 61
    //    int getConnectionsInUse();
    //
    //    void set_max_idle_time(int max_idle);
Z
zhiru 已提交
62

S
starlord 已提交
63 64
    std::string
    getDB();
Z
update  
zhiru 已提交
65

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

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

S
starlord 已提交
74 75
    unsigned int
    max_idle_time() override;
Z
zhiru 已提交
76

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

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

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

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

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