2016-12-10 21:33:45 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SENDERQUEUE_HPP
|
|
|
|
#define INCLUDED_SENDERQUEUE_HPP
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
#include <condition_variable>
|
2016-12-11 10:59:46 -06:00
|
|
|
#include <deque>
|
|
|
|
#include <memory>
|
2016-12-13 18:20:05 -06:00
|
|
|
#include <mutex>
|
2016-12-11 10:59:46 -06:00
|
|
|
#include <vector>
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
#include "common/SigUtil.hpp"
|
2016-12-13 18:20:05 -06:00
|
|
|
#include "LOOLWebSocket.hpp"
|
2016-12-10 21:33:45 -06:00
|
|
|
#include "Log.hpp"
|
|
|
|
|
|
|
|
/// The payload type used to send/receive data.
|
|
|
|
class MessagePayload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum class Type { Text, Binary };
|
|
|
|
|
2016-12-17 13:28:11 -06:00
|
|
|
/// Construct a text message.
|
2016-12-17 20:54:34 -06:00
|
|
|
/// message must include the full first-line.
|
2016-12-17 13:28:11 -06:00
|
|
|
MessagePayload(const std::string& message) :
|
|
|
|
_data(message.data(), message.data() + message.size()),
|
2016-12-17 20:54:34 -06:00
|
|
|
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
2016-12-17 13:28:11 -06:00
|
|
|
_type(Type::Text)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a message from a string with type and
|
|
|
|
/// reserve extra space (total, including message).
|
2016-12-17 20:54:34 -06:00
|
|
|
/// message must include the full first-line.
|
2016-12-17 13:28:11 -06:00
|
|
|
MessagePayload(const std::string& message,
|
|
|
|
const enum Type type,
|
|
|
|
const size_t reserve = 0) :
|
|
|
|
_data(reserve),
|
2016-12-17 20:54:34 -06:00
|
|
|
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
2016-12-17 13:28:11 -06:00
|
|
|
_type(type)
|
|
|
|
{
|
|
|
|
_data.resize(message.size());
|
|
|
|
std::memcpy(_data.data(), message.data(), message.size());
|
|
|
|
}
|
|
|
|
|
2016-12-17 20:54:34 -06:00
|
|
|
/// Construct a message from a character array with type.
|
|
|
|
/// data must be include the full first-line.
|
2016-12-17 13:28:11 -06:00
|
|
|
MessagePayload(const char* data,
|
|
|
|
const size_t size,
|
|
|
|
const enum Type type) :
|
|
|
|
_data(data, data + size),
|
2016-12-17 20:54:34 -06:00
|
|
|
_tokens(LOOLProtocol::tokenize(_data.data(), _data.size())),
|
2016-12-10 21:33:45 -06:00
|
|
|
_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-17 13:28:11 -06:00
|
|
|
size_t size() const { return _data.size(); }
|
|
|
|
const std::vector<char>& data() const { return _data; }
|
|
|
|
|
2016-12-17 20:54:34 -06:00
|
|
|
const std::vector<std::string>& tokens() const { return _tokens; }
|
|
|
|
const std::string& firstToken() const { return _tokens[0]; }
|
|
|
|
|
2016-12-17 13:28:11 -06:00
|
|
|
/// Append more data to the message.
|
|
|
|
void append(const char* data, const size_t size)
|
|
|
|
{
|
|
|
|
const auto curSize = _data.size();
|
|
|
|
_data.resize(curSize + size);
|
|
|
|
std::memcpy(_data.data() + curSize, data, size);
|
|
|
|
}
|
2016-12-10 21:33:45 -06:00
|
|
|
|
|
|
|
/// Returns true if and only if the payload is considered Binary.
|
|
|
|
bool isBinary() const { return _type == Type::Binary; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<char> _data;
|
2016-12-17 20:54:34 -06:00
|
|
|
const std::vector<std::string> _tokens;
|
2016-12-17 13:28:11 -06:00
|
|
|
const Type _type;
|
2016-12-10 21:33:45 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SendItem
|
|
|
|
{
|
2016-12-13 18:20:05 -06:00
|
|
|
std::weak_ptr<LOOLWebSocket> Socket;
|
2016-12-10 21:33:45 -06:00
|
|
|
std::shared_ptr<MessagePayload> Data;
|
2016-12-13 18:20:05 -06:00
|
|
|
std::string Meta;
|
2016-12-10 21:33:45 -06:00
|
|
|
std::chrono::steady_clock::time_point BirthTime;
|
|
|
|
};
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
/// A queue of data to send to certain Session's WS.
|
|
|
|
template <typename Item>
|
2016-12-11 17:07:26 -06:00
|
|
|
class SenderQueue final
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
SenderQueue() :
|
|
|
|
_stop(false)
|
|
|
|
{
|
|
|
|
}
|
2016-12-10 21:33:45 -06:00
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
bool stopping() const { return _stop || TerminationFlag; }
|
|
|
|
void stop()
|
|
|
|
{
|
2016-12-13 18:20:05 -06:00
|
|
|
_stop = true;
|
|
|
|
_cv.notify_all();
|
2016-12-11 10:59:46 -06:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
size_t enqueue(const Item& item)
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2016-12-13 18:20:05 -06:00
|
|
|
if (!stopping())
|
|
|
|
{
|
|
|
|
_queue.push_back(item);
|
|
|
|
}
|
|
|
|
|
2016-12-12 05:08:51 -06:00
|
|
|
const size_t queuesize = _queue.size();
|
2016-12-10 21:33:45 -06:00
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
_cv.notify_one();
|
2016-12-12 05:08:51 -06:00
|
|
|
return queuesize;
|
2016-12-10 21:33:45 -06:00
|
|
|
}
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
bool waitDequeue(Item& item,
|
2016-12-10 21:33:45 -06:00
|
|
|
const size_t timeoutMs = std::numeric_limits<size_t>::max())
|
|
|
|
{
|
|
|
|
const auto timeToWait = std::chrono::milliseconds(timeoutMs);
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (!_queue.empty() ||
|
2016-12-11 10:59:46 -06:00
|
|
|
_cv.wait_for(lock, timeToWait, [this](){ return !_queue.empty() || stopping(); }))
|
2016-12-10 21:33:45 -06:00
|
|
|
{
|
2016-12-11 10:59:46 -06:00
|
|
|
if (!stopping())
|
|
|
|
{
|
|
|
|
item = _queue.front();
|
|
|
|
_queue.pop_front();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
LOG_DBG("SenderQueue: stopping");
|
2016-12-11 10:59:46 -06:00
|
|
|
return false;
|
2016-12-10 21:33:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _queue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex _mutex;
|
|
|
|
std::condition_variable _cv;
|
2016-12-13 18:20:05 -06:00
|
|
|
std::deque<Item> _queue;
|
2016-12-11 10:59:46 -06:00
|
|
|
std::atomic<bool> _stop;
|
2016-12-10 21:33:45 -06:00
|
|
|
};
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
/// Pool of sender threads.
|
|
|
|
/// These are dedicated threads that only dequeue from
|
2016-12-13 18:20:05 -06:00
|
|
|
/// the SenderQueue and send to the target Session's WS.
|
2016-12-11 10:59:46 -06:00
|
|
|
/// This pool has long-running threads that grow
|
|
|
|
/// only on congention and shrink otherwise.
|
2016-12-11 17:07:26 -06:00
|
|
|
class SenderThreadPool final
|
2016-12-11 10:59:46 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
SenderThreadPool() :
|
|
|
|
_optimalThreadCount(std::min(2U, std::thread::hardware_concurrency())),
|
2016-12-11 17:07:26 -06:00
|
|
|
_maxThreadCount(_optimalThreadCount),
|
|
|
|
_idleThreadCount(0),
|
2016-12-11 10:59:46 -06:00
|
|
|
_stop(false)
|
|
|
|
{
|
|
|
|
LOG_INF("Creating SenderThreadPool with " << _optimalThreadCount << " optimal threads.");
|
|
|
|
for (size_t i = 0; i < _optimalThreadCount; ++i)
|
|
|
|
{
|
|
|
|
_threads.push_back(createThread());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~SenderThreadPool()
|
|
|
|
{
|
|
|
|
// Stop us and the queue.
|
|
|
|
stop();
|
2016-12-13 18:20:05 -06:00
|
|
|
//SenderQueue::instance().stop();
|
2016-12-11 10:59:46 -06:00
|
|
|
|
|
|
|
for (const auto& threadData : _threads)
|
|
|
|
{
|
|
|
|
if (threadData && threadData->joinable())
|
|
|
|
{
|
|
|
|
threadData->join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() { _stop = true; }
|
|
|
|
bool stopping() const { return _stop || TerminationFlag; }
|
|
|
|
|
2016-12-11 17:07:26 -06:00
|
|
|
void incMaxThreadCount() { ++_maxThreadCount; }
|
|
|
|
void decMaxThreadCount() { --_maxThreadCount; }
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
private:
|
|
|
|
|
2016-12-11 17:07:26 -06:00
|
|
|
/// Count idle threads safely.
|
|
|
|
/// Decrements count on ctor, and increments on dtor.
|
|
|
|
class IdleCountGuard final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
IdleCountGuard(std::atomic<size_t>& var) :
|
|
|
|
_var(var)
|
|
|
|
{
|
|
|
|
--_var;
|
|
|
|
}
|
|
|
|
|
|
|
|
~IdleCountGuard()
|
|
|
|
{
|
|
|
|
++_var;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<size_t>& _var;
|
|
|
|
};
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
typedef std::thread ThreadData;
|
|
|
|
|
|
|
|
/// Dequeue a SendItem and send it.
|
|
|
|
bool dispatchItem(const size_t timeoutMs);
|
|
|
|
|
|
|
|
/// Create a new thread and add to the pool.
|
|
|
|
std::shared_ptr<ThreadData> createThread();
|
|
|
|
|
|
|
|
/// Rebalance the number of threads.
|
|
|
|
/// Returns true if we need to reduce the threads.
|
|
|
|
bool rebalance();
|
|
|
|
|
2016-12-11 17:07:26 -06:00
|
|
|
/// Grow the pool if congestion is detected.
|
|
|
|
void checkAndGrow();
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
/// The worker thread entry function.
|
|
|
|
void threadFunction(const std::shared_ptr<ThreadData>& data);
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// A minimum of 2, but ideally as many as cores.
|
|
|
|
const size_t _optimalThreadCount;
|
|
|
|
|
2016-12-11 17:07:26 -06:00
|
|
|
/// Never exceed this number of threads.
|
|
|
|
size_t _maxThreadCount;
|
|
|
|
|
|
|
|
/// The number of threads not sending data.
|
|
|
|
std::atomic<size_t> _idleThreadCount;
|
|
|
|
|
2016-12-11 10:59:46 -06:00
|
|
|
/// Stop condition to take the pool down.
|
|
|
|
std::atomic<bool> _stop;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<ThreadData>> _threads;
|
|
|
|
mutable std::mutex _mutex;
|
|
|
|
|
|
|
|
/// How often to do housekeeping when we idle.
|
|
|
|
static constexpr size_t HousekeepIdleIntervalMs = 60000;
|
|
|
|
};
|
2016-12-10 21:33:45 -06:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|