2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2021-01-05 14:39:07 +00:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#ifndef POSTGIS_CONNECTION_MANAGER_HPP
|
|
|
|
#define POSTGIS_CONNECTION_MANAGER_HPP
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#include "connection.hpp"
|
|
|
|
|
|
|
|
// mapnik
|
2018-09-07 09:07:33 +00:00
|
|
|
#include <mapnik/params.hpp>
|
2006-10-05 09:00:36 +00:00
|
|
|
#include <mapnik/pool.hpp>
|
2015-06-02 10:10:41 +00:00
|
|
|
#include <mapnik/util/singleton.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
|
|
|
|
// boost
|
2007-06-12 08:59:54 +00:00
|
|
|
#include <boost/optional.hpp>
|
2008-02-04 16:12:13 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
// stl
|
|
|
|
#include <string>
|
2013-09-25 05:57:01 +00:00
|
|
|
#include <memory>
|
2012-04-08 00:20:56 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
using mapnik::CreateStatic;
|
2007-03-16 10:11:37 +00:00
|
|
|
using mapnik::Pool;
|
|
|
|
using mapnik::singleton;
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
template<typename T>
|
2005-06-14 15:06:59 +00:00
|
|
|
class ConnectionCreator
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
public:
|
2018-09-07 09:07:33 +00:00
|
|
|
ConnectionCreator(mapnik::parameters const& params)
|
2022-01-26 09:43:31 +00:00
|
|
|
: host_{params.get<std::string>("host")}
|
|
|
|
, port_{params.get<std::string>("port")}
|
|
|
|
, dbname_{params.get<std::string>("dbname")}
|
|
|
|
, user_{params.get<std::string>("user")}
|
|
|
|
, password_{params.get<std::string>("password")}
|
|
|
|
, connect_timeout_{params.get<std::string>("connect_timeout", "4")}
|
|
|
|
, application_name_{params.get<std::string>("application_name")}
|
|
|
|
{}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
T* operator()() const { return new T(connection_string_safe(), password_); }
|
|
|
|
|
|
|
|
inline std::string id() const { return connection_string_safe(); }
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2010-06-25 15:23:09 +00:00
|
|
|
inline std::string connection_string() const
|
2012-11-28 02:37:22 +00:00
|
|
|
{
|
|
|
|
std::string connect_str = connection_string_safe();
|
2018-09-07 09:07:33 +00:00
|
|
|
append_param(connect_str, "password=", password_);
|
2012-11-28 02:37:22 +00:00
|
|
|
return connect_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string connection_string_safe() const
|
2010-06-25 15:23:09 +00:00
|
|
|
{
|
|
|
|
std::string connect_str;
|
2018-09-07 09:07:33 +00:00
|
|
|
append_param(connect_str, "host=", host_);
|
|
|
|
append_param(connect_str, "port=", port_);
|
|
|
|
append_param(connect_str, "dbname=", dbname_);
|
|
|
|
append_param(connect_str, "user=", user_);
|
|
|
|
append_param(connect_str, "connect_timeout=", connect_timeout_);
|
|
|
|
if (!append_param(connect_str, "application_name=", application_name_))
|
|
|
|
{
|
|
|
|
// only set fallback_application_name, so that application_name
|
|
|
|
// can still be overriden with PGAPPNAME environment variable
|
|
|
|
append_param(connect_str, "fallback_application_name=", "mapnik");
|
|
|
|
}
|
2010-06-25 15:23:09 +00:00
|
|
|
return connect_str;
|
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
private:
|
2018-09-07 09:07:33 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static bool append_param(std::string& dest, char const* key, std::string const& val)
|
2018-09-07 09:07:33 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
if (val.empty())
|
|
|
|
return false;
|
|
|
|
if (!dest.empty())
|
|
|
|
dest += ' ';
|
2018-09-07 09:07:33 +00:00
|
|
|
dest += key;
|
|
|
|
dest += val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
static bool append_param(std::string& dest, char const* key, boost::optional<std::string> const& opt)
|
2018-09-07 09:07:33 +00:00
|
|
|
{
|
|
|
|
return opt && append_param(dest, key, *opt);
|
|
|
|
}
|
|
|
|
|
2011-11-19 19:35:07 +00:00
|
|
|
boost::optional<std::string> host_;
|
|
|
|
boost::optional<std::string> port_;
|
|
|
|
boost::optional<std::string> dbname_;
|
|
|
|
boost::optional<std::string> user_;
|
2018-09-07 09:07:33 +00:00
|
|
|
boost::optional<std::string> password_;
|
2011-11-19 19:35:07 +00:00
|
|
|
boost::optional<std::string> connect_timeout_;
|
2018-09-07 09:07:33 +00:00
|
|
|
boost::optional<std::string> application_name_;
|
2005-06-14 15:06:59 +00:00
|
|
|
};
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
class ConnectionManager : public singleton<ConnectionManager, CreateStatic>
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
public:
|
|
|
|
using PoolType = Pool<Connection, ConnectionCreator>;
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
private:
|
2013-05-16 15:08:42 +00:00
|
|
|
friend class CreateStatic<ConnectionManager>;
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
using ContType = std::map<std::string, std::shared_ptr<PoolType>>;
|
2014-07-07 17:23:15 +00:00
|
|
|
using HolderType = std::shared_ptr<Connection>;
|
2005-06-14 15:06:59 +00:00
|
|
|
ContType pools_;
|
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
public:
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2022-01-26 09:43:31 +00:00
|
|
|
bool registerPool(ConnectionCreator<Connection> const& creator, unsigned initialSize, unsigned maxSize)
|
2011-11-14 03:37:50 +00:00
|
|
|
{
|
2018-02-28 09:35:19 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
#endif
|
2012-11-28 17:04:53 +00:00
|
|
|
ContType::const_iterator itr = pools_.find(creator.id());
|
|
|
|
|
|
|
|
if (itr != pools_.end())
|
2006-11-17 21:10:28 +00:00
|
|
|
{
|
2012-11-28 19:16:10 +00:00
|
|
|
itr->second->set_initial_size(initialSize);
|
2012-11-28 17:38:18 +00:00
|
|
|
itr->second->set_max_size(maxSize);
|
2012-11-28 17:04:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-26 09:43:31 +00:00
|
|
|
return pools_
|
|
|
|
.insert(std::make_pair(creator.id(), std::make_shared<PoolType>(creator, initialSize, maxSize)))
|
|
|
|
.second;
|
2006-11-17 21:10:28 +00:00
|
|
|
}
|
|
|
|
return false;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2013-09-20 13:00:11 +00:00
|
|
|
std::shared_ptr<PoolType> getPool(std::string const& key)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2018-02-28 09:35:19 +00:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
#endif
|
2022-01-26 09:43:31 +00:00
|
|
|
ContType::const_iterator itr = pools_.find(key);
|
|
|
|
if (itr != pools_.end())
|
2006-11-17 21:10:28 +00:00
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
2013-09-20 13:00:11 +00:00
|
|
|
static const std::shared_ptr<PoolType> emptyPool;
|
2006-11-17 21:10:28 +00:00
|
|
|
return emptyPool;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2011-11-14 03:37:50 +00:00
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
ConnectionManager() {}
|
2022-01-26 09:43:31 +00:00
|
|
|
|
|
|
|
private:
|
2005-06-14 15:06:59 +00:00
|
|
|
ConnectionManager(const ConnectionManager&);
|
|
|
|
ConnectionManager& operator=(const ConnectionManager);
|
2011-11-14 03:37:50 +00:00
|
|
|
};
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#endif // POSTGIS_CONNECTION_MANAGER_HPP
|