mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2024-12-22 22:16:37 +08:00
provider::Pool. Inherit optional properties of the base provider.
This commit is contained in:
parent
4cded07f08
commit
d8a2384cc6
@ -365,7 +365,9 @@ public:
|
||||
|
||||
Pool(const std::shared_ptr<TProvider>& provider, v_int64 maxResources, v_int64 maxResourceTTL)
|
||||
: PoolTemplate<TResource, AcquisitionProxyImpl>(provider, maxResources, maxResourceTTL)
|
||||
{}
|
||||
{
|
||||
TProvider::m_properties = provider->getProperties();
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
#define oatpp_provider_Provider_hpp
|
||||
|
||||
#include "oatpp/core/async/Coroutine.hpp"
|
||||
#include "oatpp/core/data/share/MemoryLabel.hpp"
|
||||
|
||||
namespace oatpp { namespace provider {
|
||||
|
||||
@ -35,13 +36,53 @@ namespace oatpp { namespace provider {
|
||||
*/
|
||||
template <class T>
|
||||
class Provider {
|
||||
protected:
|
||||
|
||||
void setProperty(const oatpp::String& key, const oatpp::String& value) {
|
||||
m_properties[key] = value;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::unordered_map<data::share::StringKeyLabelCI, data::share::StringKeyLabel> m_properties;
|
||||
public:
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
Provider() = default;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
* @param properties
|
||||
*/
|
||||
Provider(const std::unordered_map<data::share::StringKeyLabelCI, data::share::StringKeyLabel>& properties)
|
||||
: m_properties(properties)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Virtual destructor.
|
||||
*/
|
||||
virtual ~Provider() = default;
|
||||
|
||||
/**
|
||||
* Some optional properties that user might want to know. <br>
|
||||
* Note: All properties are optional and user should not rely on this.
|
||||
*/
|
||||
const std::unordered_map<oatpp::data::share::StringKeyLabelCI, oatpp::data::share::StringKeyLabel>& getProperties() const {
|
||||
return m_properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get optional property
|
||||
*/
|
||||
data::share::StringKeyLabel getProperty(const oatpp::String& key) const {
|
||||
auto it = m_properties.find(key);
|
||||
if(it == m_properties.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resource.
|
||||
* @return - resource.
|
||||
|
@ -28,21 +28,5 @@ namespace oatpp { namespace network {
|
||||
|
||||
const char* const ConnectionProvider::PROPERTY_HOST = "host";
|
||||
const char* const ConnectionProvider::PROPERTY_PORT = "port";
|
||||
|
||||
void ConnectionProvider::setProperty(const oatpp::String& key, const oatpp::String& value) {
|
||||
m_properties[key] = value;
|
||||
}
|
||||
|
||||
const std::unordered_map<oatpp::data::share::StringKeyLabelCI, oatpp::data::share::StringKeyLabel>& ConnectionProvider::getProperties() {
|
||||
return m_properties;
|
||||
}
|
||||
|
||||
oatpp::data::share::StringKeyLabel ConnectionProvider::getProperty(const oatpp::String& key) {
|
||||
auto it = m_properties.find(key);
|
||||
if(it == m_properties.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
}}
|
||||
|
@ -41,6 +41,7 @@ namespace oatpp { namespace network {
|
||||
*/
|
||||
class ConnectionProvider : public provider::Provider<data::stream::IOStream> {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Predefined property key for HOST.
|
||||
*/
|
||||
@ -50,38 +51,7 @@ public:
|
||||
* Predefined property key for PORT.
|
||||
*/
|
||||
static const char* const PROPERTY_PORT;
|
||||
public:
|
||||
/**
|
||||
* Convenience typedef for &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
typedef oatpp::data::stream::IOStream IOStream;
|
||||
|
||||
/**
|
||||
* Convenience typedef for &id:oatpp::async::Action;.
|
||||
*/
|
||||
typedef oatpp::async::Action Action;
|
||||
typedef oatpp::async::Action (oatpp::async::AbstractCoroutine::*AsyncCallback)(const std::shared_ptr<IOStream>&);
|
||||
private:
|
||||
std::unordered_map<oatpp::data::share::StringKeyLabelCI, oatpp::data::share::StringKeyLabel> m_properties;
|
||||
protected:
|
||||
|
||||
/*
|
||||
* Set optional property
|
||||
*/
|
||||
void setProperty(const oatpp::String& key, const oatpp::String& value);
|
||||
public:
|
||||
|
||||
/**
|
||||
* Some optional properties that user might want to know. <br>
|
||||
* Note: All properties are optional and user should not rely on this.
|
||||
*/
|
||||
const std::unordered_map<oatpp::data::share::StringKeyLabelCI, oatpp::data::share::StringKeyLabel>& getProperties();
|
||||
|
||||
/**
|
||||
* Get optional property
|
||||
*/
|
||||
oatpp::data::share::StringKeyLabel getProperty(const oatpp::String& key);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -268,7 +268,7 @@ oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::strea
|
||||
|
||||
}
|
||||
|
||||
void SimpleTCPConnectionProvider::invalidate(const std::shared_ptr<IOStream>& connection) {
|
||||
void SimpleTCPConnectionProvider::invalidate(const std::shared_ptr<data::stream::IOStream>& connection) {
|
||||
|
||||
/************************************************
|
||||
* WARNING!!!
|
||||
|
@ -69,20 +69,20 @@ public:
|
||||
* Get connection.
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
std::shared_ptr<IOStream> get() override;
|
||||
std::shared_ptr<data::stream::IOStream> get() override;
|
||||
|
||||
/**
|
||||
* Get connection in asynchronous manner.
|
||||
* @return - &id:oatpp::async::CoroutineStarterForResult;.
|
||||
*/
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getAsync() override;
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> getAsync() override;
|
||||
|
||||
/**
|
||||
* Call shutdown read and write on an underlying file descriptor.
|
||||
* `connection` **MUST** be an object previously obtained from **THIS** connection provider.
|
||||
* @param connection
|
||||
*/
|
||||
void invalidate(const std::shared_ptr<IOStream>& connection) override;
|
||||
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override;
|
||||
|
||||
/**
|
||||
* Get host name.
|
||||
|
@ -334,7 +334,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::get(
|
||||
|
||||
}
|
||||
|
||||
void SimpleTCPConnectionProvider::invalidate(const std::shared_ptr<IOStream>& connection) {
|
||||
void SimpleTCPConnectionProvider::invalidate(const std::shared_ptr<data::stream::IOStream>& connection) {
|
||||
|
||||
/************************************************
|
||||
* WARNING!!!
|
||||
|
@ -82,8 +82,8 @@ private:
|
||||
oatpp::v_io_handle instantiateServer();
|
||||
private:
|
||||
bool prepareConnectionHandle(oatpp::v_io_handle handle);
|
||||
std::shared_ptr<IOStream> getDefaultConnection();
|
||||
std::shared_ptr<IOStream> getExtendedConnection();
|
||||
std::shared_ptr<data::stream::IOStream> getDefaultConnection();
|
||||
std::shared_ptr<data::stream::IOStream> getExtendedConnection();
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -119,7 +119,7 @@ public:
|
||||
* Get incoming connection.
|
||||
* @return &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
std::shared_ptr<IOStream> get() override;
|
||||
std::shared_ptr<data::stream::IOStream> get() override;
|
||||
|
||||
/**
|
||||
* No need to implement this.<br>
|
||||
@ -129,7 +129,7 @@ public:
|
||||
* <br>
|
||||
* *It may be implemented later*
|
||||
*/
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getAsync() override {
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> getAsync() override {
|
||||
/*
|
||||
* No need to implement this.
|
||||
* For Asynchronous IO in oatpp it is considered to be a good practice
|
||||
@ -146,7 +146,7 @@ public:
|
||||
* `connection` **MUST** be an object previously obtained from **THIS** connection provider.
|
||||
* @param connection
|
||||
*/
|
||||
void invalidate(const std::shared_ptr<IOStream>& connection) override;
|
||||
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override;
|
||||
|
||||
/**
|
||||
* Get port.
|
||||
|
@ -43,7 +43,7 @@ void ConnectionProvider::stop() {
|
||||
|
||||
}
|
||||
|
||||
std::shared_ptr<ConnectionProvider::IOStream> ConnectionProvider::get() {
|
||||
std::shared_ptr<data::stream::IOStream> ConnectionProvider::get() {
|
||||
auto submission = m_interface->connect();
|
||||
if(submission->isValid()) {
|
||||
auto socket = submission->getSocket();
|
||||
|
@ -75,19 +75,19 @@ public:
|
||||
* Get connection.
|
||||
* @return - `std::shared_ptr` to &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
std::shared_ptr<IOStream> get() override;
|
||||
std::shared_ptr<data::stream::IOStream> get() override;
|
||||
|
||||
/**
|
||||
* Get connection in asynchronous manner.
|
||||
* @return - &id:oatpp::async::CoroutineStarterForResult;.
|
||||
*/
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getAsync() override;
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> getAsync() override;
|
||||
|
||||
/**
|
||||
* Does nothing.
|
||||
* @param connection
|
||||
*/
|
||||
void invalidate(const std::shared_ptr<IOStream>& connection) override {
|
||||
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override {
|
||||
(void)connection;
|
||||
// DO Nothing.
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ void ConnectionProvider::stop() {
|
||||
m_interface->notifyAcceptors();
|
||||
}
|
||||
|
||||
std::shared_ptr<ConnectionProvider::IOStream> ConnectionProvider::get() {
|
||||
std::shared_ptr<data::stream::IOStream> ConnectionProvider::get() {
|
||||
auto socket = m_interface->accept(m_open);
|
||||
if(socket) {
|
||||
socket->setMaxAvailableToReadWrtie(m_maxAvailableToRead, m_maxAvailableToWrite);
|
||||
|
@ -74,7 +74,7 @@ public:
|
||||
* Get incoming connection.
|
||||
* @return &id:oatpp::data::stream::IOStream;.
|
||||
*/
|
||||
std::shared_ptr<IOStream> get() override;
|
||||
std::shared_ptr<data::stream::IOStream> get() override;
|
||||
|
||||
/**
|
||||
* **NOT IMPLEMENTED!**<br>
|
||||
@ -85,7 +85,7 @@ public:
|
||||
* <br>
|
||||
* *It may be implemented later.*
|
||||
*/
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<oatpp::data::stream::IOStream>&> getAsync() override {
|
||||
oatpp::async::CoroutineStarterForResult<const std::shared_ptr<data::stream::IOStream>&> getAsync() override {
|
||||
/*
|
||||
* No need to implement this.
|
||||
* For Asynchronous IO in oatpp it is considered to be a good practice
|
||||
@ -101,7 +101,7 @@ public:
|
||||
* Does nothing.
|
||||
* @param connection
|
||||
*/
|
||||
void invalidate(const std::shared_ptr<IOStream>& connection) override {
|
||||
void invalidate(const std::shared_ptr<data::stream::IOStream>& connection) override {
|
||||
(void)connection;
|
||||
// DO Nothing.
|
||||
}
|
||||
|
@ -83,7 +83,7 @@ public:
|
||||
|
||||
std::atomic<v_int64> counter;
|
||||
|
||||
std::shared_ptr<IOStream> get() override {
|
||||
std::shared_ptr<oatpp::data::stream::IOStream> get() override {
|
||||
++ counter;
|
||||
return std::make_shared<StubStream>();
|
||||
}
|
||||
@ -108,7 +108,7 @@ public:
|
||||
// DO NOTHING
|
||||
}
|
||||
|
||||
void invalidate(const std::shared_ptr<IOStream>& connection) override {
|
||||
void invalidate(const std::shared_ptr<oatpp::data::stream::IOStream>& connection) override {
|
||||
(void)connection;
|
||||
// DO Nothing.
|
||||
}
|
||||
|
@ -265,6 +265,7 @@ void ClientRetryTest::onRun() {
|
||||
runServer(m_port, 2, 6, false, controller);
|
||||
|
||||
clientThread.join();
|
||||
connectionPool->stop();
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user