1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
This commit is contained in:
stefanocasazza 2020-01-10 16:47:31 +01:00
parent 84678faf83
commit ad813f58de
16 changed files with 1924 additions and 632 deletions

View File

@ -65,10 +65,10 @@ typedef void (*vPFcscs)(const UString&,const UString&);
class UREDISClusterMaster;
class U_EXPORT UREDISClient_Base : public UClient_Base, UEventFd {
class U_EXPORT UREDISClient_Base : public UClient_Base {
public:
~UREDISClient_Base();
~UREDISClient_Base() { U_TRACE_DTOR(0, UREDISClient_Base); }
// RESPONSE
@ -787,22 +787,6 @@ public:
(withPayloads ? U_CONSTANT_SIZE("WITHPAYLOADS") : 0), "WITHPAYLOADS"));
}
// define method VIRTUAL of class UEventFd
#if defined(U_STDCPP_ENABLE) && defined(HAVE_CXX20) && defined(U_LINUX) && !defined(__clang__)
virtual int handlerRead() U_DECL_FINAL;
#endif
virtual void handlerDelete()
{
U_TRACE_NO_PARAM(0, "UREDISClient_Base::handlerDelete()")
U_INTERNAL_DUMP("UREDISClient_Base::handlerDelete() -> client = %p", this);
U_INTERNAL_DUMP("UEventFd::fd = %d", UEventFd::fd)
UEventFd::fd = -1;
}
#if defined(U_STDCPP_ENABLE) && defined(DEBUG)
const char* dump(bool reset) const;
#endif
@ -814,8 +798,6 @@ protected:
static ptrdiff_t diff;
static UREDISClient_Base* pthis;
static UHashMap<void*>* pchannelCallbackMap;
UREDISClient_Base() : UClient_Base(U_NULLPTR)
{
U_TRACE_CTOR(0, UREDISClient_Base, "")
@ -999,7 +981,7 @@ private:
#if defined(U_STDCPP_ENABLE) && defined(HAVE_CXX20) && defined(U_LINUX) && !defined(__clang__)
class UREDISClusterClient : public UREDISClient<UTCPSocket> {
class U_EXPORT UREDISClusterClient : public UREDISClient<UTCPSocket>, public UEventFd {
public:
enum class ClientType : uint8_t {
@ -1012,9 +994,23 @@ public:
const ClientType type;
UREDISClusterMaster *master;
//virtual void handlerDelete() U_DECL_FINAL;
virtual int handlerRead() U_DECL_FINAL;
virtual void handlerDelete() U_DECL_FINAL
{
U_TRACE_NO_PARAM(0, "UREDISClusterClient::handlerDelete()")
U_INTERNAL_DUMP("UREDISClusterClient::handlerDelete() -> client = %p", this);
U_INTERNAL_DUMP("UEventFd::fd = %d", UEventFd::fd)
UEventFd::fd = -1;
}
UREDISClusterClient(UREDISClusterMaster *_master, const ClientType _type) : UREDISClient<UTCPSocket>(), type(_type), master(_master) {}
#if defined(U_STDCPP_ENABLE) && defined(DEBUG)
const char* dump(bool _reset) const { return UREDISClient_Base::dump(_reset); }
#endif
};
struct RedisClusterNode {
@ -1057,6 +1053,7 @@ private:
UREDISClusterClient *subscriptionClient;
UREDISClusterClient *managementClient;
UHashMap<RedisClusterNode *> *clusterNodes;
UHashMap<void*>* pchannelCallbackMap;
static uint16_t hashslotForKey(UStringType&& hashableKey) {return u_crc16(U_STRING_TO_PARAM(hashableKey)) % 16384;}
@ -1098,6 +1095,10 @@ private:
template<bool psuedoSilence>
UREDISClusterClient* sendToCluster(uint16_t hashslot, UStringType&& pipeline, UREDISClusterClient* workingClient)
{
U_TRACE_NO_PARAM(0, "UREDISClusterMaster::sendToCluster");
U_DUMP("pipeline = %.*s", pipeline.size(), pipeline.data());
ClusterError error;
retry:
@ -1116,7 +1117,6 @@ private:
{
case ClusterError::moved:
{
// U_DUMP("(D) calling calculateNodeMap");
calculateNodeMap();
workingClient = clientForHashslot(hashslot);
break;
@ -1152,7 +1152,7 @@ private:
return sendToCluster<psuedoSilence>(hashslot, std::forward<B>(pipeline), clientForHashslot(hashslot));
}
public:
public:
U_MEMORY_TEST
U_MEMORY_ALLOCATOR
@ -1191,7 +1191,9 @@ public:
~UREDISClusterMaster()
{
U_DELETE(subscriptionClient);
U_DELETE(managementClient);
if (clusterNodes) U_DELETE(clusterNodes);
if (pchannelCallbackMap) U_DELETE(pchannelCallbackMap);
}
#if defined(DEBUG)
@ -1202,6 +1204,9 @@ public:
class UCompileTimeRESPEncoder : public UCompileTimeStringFormatter {
private:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
template<bool isPartial, size_t workingIndex = 0, size_t workingSegmentCount = 0, typename StringClass, typename... Xs, typename T, typename... Ts>
static constexpr auto generateSegments(StringClass format, size_t& outputCount, std::tuple<Xs...>&& workingCommand, T&& t, Ts&&... ts)
{
@ -1253,6 +1258,7 @@ private:
}
}
}
#pragma GCC diagnostic pop
template<bool isPartial, bool overwrite, auto format, typename... Ts>
static size_t encode_impl(size_t writePosition, UString& workingString, Ts&&... ts)
@ -1361,6 +1367,8 @@ public:
pipeline.append(command);
spans.emplace_back(commandCount, UREDISClusterMaster::hashslotForKey(std::forward<A>(hashableKey)), beginning, pipeline.size(), spans.size());
U_DUMP("appended, %.*s", pipeline.size() - beginning, pipeline.data());
}
template <auto format, UStringType A, typename... Ts>
@ -1373,6 +1381,8 @@ public:
size_t commandCount = UCompileTimeRESPEncoder::encode_add<format>(pipeline, std::forward<Ts>(ts)...);
spans.emplace_back(commandCount, UREDISClusterMaster::hashslotForKey(std::forward<A>(hashableKey)), beginning, pipeline.size(), spans.size());
U_DUMP("appended, %.*s", pipeline.size() - beginning, pipeline.data());
}
AnonymousClusterPipeline() : pipeline(300U) {}

View File

@ -2648,6 +2648,8 @@ public:
long strtol( bool check_for_suffix = false) const { return rep->strtol( check_for_suffix); }
unsigned long strtoul(bool check_for_suffix = false) const { return rep->strtoul(check_for_suffix); }
static UString humanReadableByteCountBin(int64_t bytes);
// UTF8 <--> ISO Latin 1
static UString toUTF8(const unsigned char* t, uint32_t tlen)

View File

@ -16,20 +16,8 @@
uint32_t UREDISClient_Base::start;
ptrdiff_t UREDISClient_Base::diff;
UHashMap<void*>* UREDISClient_Base::pchannelCallbackMap;
UREDISClient_Base* UREDISClient_Base::pthis;
UREDISClient_Base::~UREDISClient_Base()
{
U_TRACE_DTOR(0, UREDISClient_Base)
if (pchannelCallbackMap)
{
U_DELETE(pchannelCallbackMap)
pchannelCallbackMap = U_NULLPTR;
}
}
// Connect to REDIS server
void UREDISClient_Base::init()
@ -439,13 +427,13 @@ bool UREDISClient_Base::deleteKeys(const char* pattern, uint32_t len) // Delete
#if defined(U_STDCPP_ENABLE) && defined(HAVE_CXX20) && defined(U_LINUX) && !defined(__clang__)
// this is called for subscribed channels
int UREDISClient_Base::handlerRead()
int UREDISClusterClient::handlerRead()
{
// BytesRead(100) = "*3\r\n$7\r\nmessage\r\n$19\r\n{ABC}.trafficSignal\r\n$1\r\n1\r\n*3\r\n$7\r\nmessage\r\n$19\r\n{DEF}.trafficSignal\r\n$1\r\n1\r\n"
U_TRACE_NO_PARAM(0, "UREDISClient_Base::handlerRead()")
U_TRACE_NO_PARAM(0, "UREDISClusterClient::handlerRead()")
if ((clear(), UClient_Base::response.setEmpty(), UClient_Base::readResponse(U_SINGLE_READ)))
if ((clear(), response.setEmpty(), readResponse(U_SINGLE_READ)))
{
processResponse();
@ -457,7 +445,7 @@ int UREDISClient_Base::handlerRead()
{
if (vitem[index] == "message"_ctv)
{
vPFcscs callback = (vPFcscs)pchannelCallbackMap->at(vitem[index + 1]);
vPFcscs callback = (vPFcscs)(master->pchannelCallbackMap->at(vitem[index + 1]));
if (callback) callback(vitem[index + 1], vitem[index + 2]);
}
}
@ -468,8 +456,6 @@ int UREDISClient_Base::handlerRead()
ClusterError UREDISClusterMaster::checkResponseForClusterErrors(const UString& response, size_t offset)
{
U_TRACE_NO_PARAM(0, "checkResponseForClusterErrors()")
// all of these errors are very rare, and only occur in the midst of cluster topology changes
// -MOVED 3999 127.0.0.1:6381 => the hashslot has been moved to another master node
@ -500,8 +486,6 @@ void UREDISClusterMaster::calculateNodeMap()
UString& response = managementClient->response;
U_WARNING("CLUSTER SLOTS response = %.*s", response.size(), response.data());
uint16_t lowHashSlot;
uint16_t highHashSlot;
UString compositeAddress(50U);
@ -588,6 +572,8 @@ bool UREDISClusterMaster::connect(const char* host, unsigned int _port)
{
U_TRACE(0, "UREDISClusterMaster::connect(%S,%u)", host, _port)
managementClient->UEventFd::op_mask |= EPOLLET;
if (managementClient->connect(host, _port))
{
calculateNodeMap();
@ -598,16 +584,11 @@ bool UREDISClusterMaster::connect(const char* host, unsigned int _port)
{
subscriptionClient->connect(randomNode->ipAddress.c_str(), randomNode->port);
U_INTERNAL_ASSERT_EQUALS(UREDISClient_Base::pchannelCallbackMap, U_NULLPTR)
U_NEW(UHashMap<void*>, UREDISClient_Base::pchannelCallbackMap, UHashMap<void*>());
U_NEW(UHashMap<void*>, pchannelCallbackMap, UHashMap<void*>());
subscriptionClient->UEventFd::fd = subscriptionClient->getFd();
subscriptionClient->UEventFd::op_mask |= EPOLLET;
U_DUMP("subscriptionClient = %p", subscriptionClient);
UServer_Base::addHandlerEvent(subscriptionClient);
U_RETURN(true);
@ -621,16 +602,20 @@ void UREDISClusterMaster::clusterUnsubscribe(const UString& channel) // unregist
{
U_TRACE(0, "UREDISClusterMaster::clusterUnsubscribe(%V)", channel.rep)
subscriptionClient->sendRequest(U_CTV_TO_PARAM("UNSUBSCRIBE "_ctv), channel);
(void)subscriptionClient->pchannelCallbackMap->erase(channel);
UCompileTimeRESPEncoder::encode<"UNSUBSCRIBE {}"_ctv>(subscriptionClient->response, channel);
subscriptionClient->sendRequest(subscriptionClient->response);
(void)pchannelCallbackMap->erase(channel);
}
void UREDISClusterMaster::clusterSubscribe(const UString& channel, vPFcscs callback) // register the callback for messages published to the given channels
{
U_TRACE(0, "UREDISClusterMaster::clusterSubscribe(%V,%p)", channel.rep, callback)
subscriptionClient->sendRequest(U_CTV_TO_PARAM("SUBSCRIBE "_ctv), channel);
subscriptionClient->pchannelCallbackMap->insert(channel, (const void*)callback);
UCompileTimeRESPEncoder::encode<"SUBSCRIBE {}"_ctv>(subscriptionClient->response, channel);
subscriptionClient->sendRequest(subscriptionClient->response);
UString channelCopy(U_STRING_TO_PARAM(channel));
pchannelCallbackMap->insert(channelCopy, (const void*)callback);
}
static void getNextCommandResponse(const UString& string, size_t& marker)

View File

@ -17,13 +17,6 @@
class U_EXPORT Fortune {
public:
// Check for memory error
U_MEMORY_TEST
// Allocator e Deallocator
U_MEMORY_ALLOCATOR
U_MEMORY_DEALLOCATOR
uint32_t id;
UString message;
@ -40,8 +33,6 @@ public:
Fortune(const Fortune& f) : id(f.id), message(f.message)
{
U_TRACE_CTOR(5, Fortune, "%p", &f)
U_MEMORY_TEST_COPY(f)
}
~Fortune()
@ -159,7 +150,7 @@ public:
{
u_put_unalignedp64(ptr, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(ptr+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('1','2','2','7','\r','\n','C','o'));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('x','x','x','x','\r','\n','C','o'));
u_put_unalignedp64(ptr+24, U_MULTICHAR_CONSTANT64('n','t','e','n','t','-','T','y'));
u_put_unalignedp64(ptr+32, U_MULTICHAR_CONSTANT64('p','e',':',' ','t','e','x','t'));
u_put_unalignedp64(ptr+40, U_MULTICHAR_CONSTANT64('/','h','t','m','l',';',' ','c'));
@ -180,13 +171,11 @@ public:
u_put_unalignedp64(ptr+160, U_MULTICHAR_CONSTANT64('<','/','t','h','>','<','/','t'));
u_put_unalignedp16(ptr+168, U_MULTICHAR_CONSTANT16('r','>'));
pwbuffer = ptr + U_CONSTANT_SIZE("Content-Length: 1227\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
pwbuffer = ptr + U_CONSTANT_SIZE("Content-Length: xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
"<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
}
U_INTERNAL_ASSERT_EQUALS(u_get_unalignedp64(UClientImage_Base::wbuffer->data()), U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'))
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: 1227\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n") + 1227);
}
static void endQuery()
@ -203,6 +192,7 @@ public:
pvfortune->sort(Fortune::cmp_obj);
char* ptr = pwbuffer;
uint32_t content_length = U_CONSTANT_SIZE("<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
for (uint32_t sz, i = 0, n = pvfortune->size(); i < n; ++i)
{
@ -228,6 +218,17 @@ public:
u_put_unalignedp64(ptr, U_MULTICHAR_CONSTANT64('<','/','t','a','b','l','e','>'));
u_put_unalignedp64(ptr+8, U_MULTICHAR_CONSTANT64('<','/','b','o','d','y','>','<'));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('/','h','t','m','l','>','\0','\0'));
content_length += (ptr - pwbuffer) + U_CONSTANT_SIZE("</table></body></html>");
U_INTERNAL_ASSERT_EQUALS(content_length, 1227)
ptr = pwbuffer - U_CONSTANT_SIZE("xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
"<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
(void) u_num2str32(content_length, ptr);
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n") + content_length);
}
static void doQuery(vPF handlerQuery)
@ -246,10 +247,7 @@ public:
# ifdef U_STATIC_ORM_DRIVER_PGSQL
U_INTERNAL_DUMP("UServer_Base::handler_db2 = %p", UServer_Base::handler_db2)
if (UServer_Base::handler_db2 == U_NULLPTR)
{
U_NEW(UEventDB, UServer_Base::handler_db2, UEventDB);
}
if (UServer_Base::handler_db2 == U_NULLPTR) UServer_Base::handler_db2 = new UEventDB();
# endif
}
@ -257,15 +255,15 @@ public:
{
U_TRACE_NO_PARAM(5, "Fortune::handlerFork()")
U_NEW_STRING(pmessage, UString(101U));
pmessage = new UString(101U);
U_NEW(UVector<Fortune*>, pvfortune, UVector<Fortune*>);
pvfortune = new UVector<Fortune*>();
Fortune* elem;
for (uint32_t i = 0; i < 13; ++i)
{
U_NEW(Fortune, elem, Fortune(i+1));
elem = new Fortune(i+1);
pvfortune->push(elem);
}
@ -277,7 +275,7 @@ public:
if (psql_fortune == U_NULLPTR)
{
U_NEW(UOrmSession, psql_fortune, UOrmSession(U_CONSTANT_TO_PARAM("fortune")));
psql_fortune = new UOrmSession(U_CONSTANT_TO_PARAM("fortune"));
if (psql_fortune->isReady() == false)
{
@ -290,7 +288,7 @@ public:
return;
}
U_NEW(UOrmStatement, pstmt_fortune, UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune")));
pstmt_fortune = new UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune"));
handlerFork();
@ -313,33 +311,33 @@ public:
}
#ifdef DEBUG
static void handlerEnd()
{
U_TRACE_NO_PARAM(5, "Fortune::handlerEnd()")
static void handlerEnd()
{
U_TRACE_NO_PARAM(5, "Fortune::handlerEnd()")
U_INTERNAL_ASSERT_POINTER(pmessage)
U_INTERNAL_ASSERT_POINTER(pvfortune)
U_INTERNAL_ASSERT_POINTER(pmessage)
U_INTERNAL_ASSERT_POINTER(pvfortune)
U_DELETE(pmessage)
U_DELETE(pvfortune)
}
U_DELETE(pmessage)
U_DELETE(pvfortune)
}
static void handlerEndSql()
{
U_TRACE_NO_PARAM(5, "Fortune::handlerEndSql()")
static void handlerEndSql()
{
U_TRACE_NO_PARAM(5, "Fortune::handlerEndSql()")
if (pstmt_fortune)
{
handlerEnd();
if (pstmt_fortune)
{
handlerEnd();
U_DELETE(psql_fortune)
U_DELETE(pstmt_fortune)
U_DELETE(psql_fortune)
U_DELETE(pstmt_fortune)
pstmt_fortune = U_NULLPTR;
}
}
pstmt_fortune = U_NULLPTR;
}
}
const char* dump(bool breset) const;
const char* dump(bool breset) const;
#endif
private:

View File

@ -26,14 +26,14 @@ if (u_get_unalignedp64(pwbuffer+44) != U_MULTICHAR_CONSTANT64('n','/','j','s','o
{
u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
u_put_unalignedp32(pwbuffer+16, U_MULTICHAR_CONSTANT32('2','7','\r','\n'));
u_put_unalignedp32(pwbuffer+16, U_MULTICHAR_CONSTANT32('x','x','\r','\n'));
u_put_unalignedp64(pwbuffer+20, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(pwbuffer+28, U_MULTICHAR_CONSTANT64('T','y','p','e',':',' ','a','p'));
u_put_unalignedp64(pwbuffer+36, U_MULTICHAR_CONSTANT64('p','l','i','c','a','t','i','o'));
u_put_unalignedp64(pwbuffer+44, U_MULTICHAR_CONSTANT64('n','/','j','s','o','n','\r','\n'));
u_put_unalignedp16(pwbuffer+52, U_MULTICHAR_CONSTANT16('\r','\n'));
ptr = pwbuffer + U_CONSTANT_SIZE("Content-Length: 27\r\nContent-Type: application/json\r\n\r\n");
ptr = pwbuffer + U_CONSTANT_SIZE("Content-Length: xx\r\nContent-Type: application/json\r\n\r\n");
}
U_INTERNAL_ASSERT_EQUALS(u_get_unalignedp64(UClientImage_Base::wbuffer->data()), U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'))
@ -42,5 +42,11 @@ UValue::pstringify = ptr;
UValue(*pkey, *pvalue).stringify();
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: 27\r\nContent-Type: application/json\r\n\r\n") + 27);
uint32_t content_length = (UValue::pstringify - ptr);
U_INTERNAL_ASSERT_EQUALS(content_length, 27)
(void) u_num2str32(content_length, ptr - U_CONSTANT_SIZE("xx\r\nContent-Type: application/json\r\n\r\n"));
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: xx\r\nContent-Type: application/json\r\n\r\n") + content_length);
-->

View File

@ -1902,6 +1902,24 @@ long double UStringRep::strtold() const
}
#endif
UString UString::humanReadableByteCountBin(int64_t bytes)
{
U_TRACE(0, "UString::humanReadableByteCountBin(%lld)", bytes)
UString result(32U);
int64_t b = (bytes == LONG_MIN ? LONG_MAX : llabs(bytes));
if (b < 1024L) result.snprintf(U_CONSTANT_TO_PARAM("%lld B"), bytes);
else if (b <= (0xfffccccccccccccL >> 40)) result.snprintf(U_CONSTANT_TO_PARAM("%.1f KiB"), bytes / 0x1p10);
else if (b <= (0xfffccccccccccccL >> 30)) result.snprintf(U_CONSTANT_TO_PARAM("%.1f MiB"), bytes / 0x1p20);
else if (b <= (0xfffccccccccccccL >> 20)) result.snprintf(U_CONSTANT_TO_PARAM("%.1f GiB"), bytes / 0x1p30);
else if (b <= (0xfffccccccccccccL >> 10)) result.snprintf(U_CONSTANT_TO_PARAM("%.1f TiB"), bytes / 0x1p40);
else if (b <= 0xfffccccccccccccL) result.snprintf(U_CONSTANT_TO_PARAM("%.1f PiB"), (bytes >> 10) / 0x1p40);
else result.snprintf(U_CONSTANT_TO_PARAM("%.1f EiB"), (bytes >> 20) / 0x1p40);
U_RETURN_STRING(result);
}
// UTF8 <--> ISO Latin 1
UStringRep* UStringRep::fromUTF8(const unsigned char* s, uint32_t n)

View File

@ -17,7 +17,8 @@
#include <ulib/utility/semaphore.h>
#ifdef U_LINUX
U_DUMP_KERNEL_VERSION(LINUX_VERSION_CODE)
// socket already dumps this
//U_DUMP_KERNEL_VERSION(LINUX_VERSION_CODE)
#endif
USemaphore* USemaphore::first;

View File

@ -617,8 +617,13 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
*/
uint8_t opcode, masking_key[4];
U_DUMP("len = %lu", len);
// 0xffff == 65535
uint32_t header_length = (len > 125U ? 2U : 0) + (len > 0xffff ? 8U : 0);
U_DUMP("header_length = %lu", header_length);
if (isServer) header_length += 2U;
else
{
@ -628,8 +633,12 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
uint32_t ncount = header_length + len;
UString tmp(ncount), compressed;
unsigned char* header = (unsigned char*)tmp.data();
// 1 MB
static UString buffer(1048576U);
buffer.setEmpty();
buffer.reserve(ncount);
unsigned char* header = (unsigned char*)buffer.data();
switch (type)
{
@ -643,6 +652,8 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
opcode = U_WS_OPCODE_TEXT;
# ifdef USE_LIBBROTLI
UString compressed;
if (compressed = UStringExt::brotli(data, len, (U_PARALLELIZATION_CHILD ? BROTLI_MAX_QUALITY : UHTTP::brotli_level_for_dynamic_content)))
{
opcode = U_WS_OPCODE_BROTLI;
@ -685,7 +696,7 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 4:
{
header[1] = 126;
u_put_unalignedp16(header+2, htons(len));
u_put_unalignedp16(header+2, htons((uint16_t)len));
break;
}
case 12:
@ -721,6 +732,7 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
default: break; // never reached
}
switch (header_length)
{
// server
@ -728,10 +740,7 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 4:
case 12:
{
for (uint32_t i = 0; i < len; ++i)
{
header[2+i] = data[i];
}
memcpy(header + header_length, data, len);
break;
}
// client
@ -739,9 +748,10 @@ bool UWebSocket::sendData(const bool isServer, USocket* socket, int type, const
case 8:
case 16:
{
// we should SIMD this
for (uint32_t i = 0; i < len; ++i)
{
header[6+i] = (data[i] ^ masking_key[i % 4]) & 0xff;
header[header_length + i] = (data[i] ^ masking_key[i % 4]) & 0xff;
}
break;
}

View File

@ -45,9 +45,9 @@ export ORM_DRIVER ORM_OPTION UMEMPOOL
# ----------------------------------------------------------------------------------------------------------------------------------------------------------
# JSON
# ----------------------------------------------------------------------------------------------------------------------------------------------------------
#UMEMPOOL="237,0,0,49,273,-15,-14,-20,36"
#sed -i "s|TCP_LINGER_SET .*|TCP_LINGER_SET 0|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
#sed -i "s|LISTEN_BACKLOG .*|LISTEN_BACKLOG 256|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
UMEMPOOL="237,0,0,49,273,-15,-14,-20,36"
sed -i "s|TCP_LINGER_SET .*|TCP_LINGER_SET 0|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
sed -i "s|LISTEN_BACKLOG .*|LISTEN_BACKLOG 256|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
# ----------------------------------------------------------------------------------------------------------------------------------------------------------
#Running 15s test @ http://localhost:8080/json
# 4 threads and 256 connections
@ -69,11 +69,11 @@ export ORM_DRIVER ORM_OPTION UMEMPOOL
#ORM_OPTION="host=localhost dbname=../db/hello_world"
#ORM_DRIVER="mysql"
#ORM_OPTION="host=localhost user=benchmarkdbuser password=benchmarkdbpass character-set=utf8 dbname=hello_world"
ORM_DRIVER="pgsql"
ORM_OPTION="host=localhost user=benchmarkdbuser password=benchmarkdbpass dbname=hello_world"
UMEMPOOL="581,0,0,66,16416,-7,-20,-23,31"
sed -i "s|TCP_LINGER_SET .*|TCP_LINGER_SET -2|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
sed -i "s|LISTEN_BACKLOG .*|LISTEN_BACKLOG 256|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
#ORM_DRIVER="pgsql"
#ORM_OPTION="host=localhost user=benchmarkdbuser password=benchmarkdbpass dbname=hello_world"
#UMEMPOOL="581,0,0,66,16416,-7,-20,-23,31"
#sed -i "s|TCP_LINGER_SET .*|TCP_LINGER_SET -2|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
#sed -i "s|LISTEN_BACKLOG .*|LISTEN_BACKLOG 256|g" benchmark/FrameworkBenchmarks/fbenchmark.cfg
# ----------------------------------------------------------------------------------------------------------------------------------------------------------
#Running 15s test @ http://localhost:8080/cached_worlds?queries=20
# 4 threads and 256 connections

View File

@ -1 +1 @@
06D4
06E9

View File

@ -33,8 +33,6 @@ public:
Fortune(const Fortune& f) : id(f.id), message(f.message)
{
U_TRACE_CTOR(5, Fortune, "%p", &f)
U_MEMORY_TEST_COPY(f)
}
~Fortune()
@ -152,7 +150,7 @@ public:
{
u_put_unalignedp64(ptr, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(ptr+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('1','2','2','7','\r','\n','C','o'));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('x','x','x','x','\r','\n','C','o'));
u_put_unalignedp64(ptr+24, U_MULTICHAR_CONSTANT64('n','t','e','n','t','-','T','y'));
u_put_unalignedp64(ptr+32, U_MULTICHAR_CONSTANT64('p','e',':',' ','t','e','x','t'));
u_put_unalignedp64(ptr+40, U_MULTICHAR_CONSTANT64('/','h','t','m','l',';',' ','c'));
@ -173,13 +171,11 @@ public:
u_put_unalignedp64(ptr+160, U_MULTICHAR_CONSTANT64('<','/','t','h','>','<','/','t'));
u_put_unalignedp16(ptr+168, U_MULTICHAR_CONSTANT16('r','>'));
pwbuffer = ptr + U_CONSTANT_SIZE("Content-Length: 1227\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
pwbuffer = ptr + U_CONSTANT_SIZE("Content-Length: xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
"<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
}
U_INTERNAL_ASSERT_EQUALS(u_get_unalignedp64(UClientImage_Base::wbuffer->data()), U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'))
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: 1227\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n") + 1227);
}
static void endQuery()
@ -196,6 +192,7 @@ public:
pvfortune->sort(Fortune::cmp_obj);
char* ptr = pwbuffer;
uint32_t content_length = U_CONSTANT_SIZE("<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
for (uint32_t sz, i = 0, n = pvfortune->size(); i < n; ++i)
{
@ -221,6 +218,17 @@ public:
u_put_unalignedp64(ptr, U_MULTICHAR_CONSTANT64('<','/','t','a','b','l','e','>'));
u_put_unalignedp64(ptr+8, U_MULTICHAR_CONSTANT64('<','/','b','o','d','y','>','<'));
u_put_unalignedp64(ptr+16, U_MULTICHAR_CONSTANT64('/','h','t','m','l','>','\0','\0'));
content_length += (ptr - pwbuffer) + U_CONSTANT_SIZE("</table></body></html>");
U_INTERNAL_ASSERT_EQUALS(content_length, 1227)
ptr = pwbuffer - U_CONSTANT_SIZE("xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"
"<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
(void) u_num2str32(content_length, ptr);
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: xxxx\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n") + content_length);
}
static void doQuery(vPF handlerQuery)
@ -239,10 +247,7 @@ public:
# ifdef U_STATIC_ORM_DRIVER_PGSQL
U_INTERNAL_DUMP("UServer_Base::handler_db2 = %p", UServer_Base::handler_db2)
if (UServer_Base::handler_db2 == U_NULLPTR)
{
U_NEW(UEventDB, UServer_Base::handler_db2, UEventDB);
}
if (UServer_Base::handler_db2 == U_NULLPTR) UServer_Base::handler_db2 = new UEventDB();
# endif
}
@ -250,15 +255,15 @@ public:
{
U_TRACE_NO_PARAM(5, "Fortune::handlerFork()")
U_NEW_STRING(pmessage, UString(101U));
pmessage = new UString(101U);
U_NEW(UVector<Fortune*>, pvfortune, UVector<Fortune*>);
pvfortune = new UVector<Fortune*>();
Fortune* elem;
for (uint32_t i = 0; i < 13; ++i)
{
U_NEW(Fortune, elem, Fortune(i+1));
elem = new Fortune(i+1);
pvfortune->push(elem);
}
@ -270,7 +275,7 @@ public:
if (psql_fortune == U_NULLPTR)
{
U_NEW(UOrmSession, psql_fortune, UOrmSession(U_CONSTANT_TO_PARAM("fortune")));
psql_fortune = new UOrmSession(U_CONSTANT_TO_PARAM("fortune"));
if (psql_fortune->isReady() == false)
{
@ -283,7 +288,7 @@ public:
return;
}
U_NEW(UOrmStatement, pstmt_fortune, UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune")));
pstmt_fortune = new UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune"));
handlerFork();

View File

@ -26,14 +26,14 @@ if (u_get_unalignedp64(pwbuffer+44) != U_MULTICHAR_CONSTANT64('n','/','j','s','o
{
u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('L','e','n','g','t','h',':',' '));
u_put_unalignedp32(pwbuffer+16, U_MULTICHAR_CONSTANT32('2','7','\r','\n'));
u_put_unalignedp32(pwbuffer+16, U_MULTICHAR_CONSTANT32('x','x','\r','\n'));
u_put_unalignedp64(pwbuffer+20, U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'));
u_put_unalignedp64(pwbuffer+28, U_MULTICHAR_CONSTANT64('T','y','p','e',':',' ','a','p'));
u_put_unalignedp64(pwbuffer+36, U_MULTICHAR_CONSTANT64('p','l','i','c','a','t','i','o'));
u_put_unalignedp64(pwbuffer+44, U_MULTICHAR_CONSTANT64('n','/','j','s','o','n','\r','\n'));
u_put_unalignedp16(pwbuffer+52, U_MULTICHAR_CONSTANT16('\r','\n'));
ptr = pwbuffer + U_CONSTANT_SIZE("Content-Length: 27\r\nContent-Type: application/json\r\n\r\n");
ptr = pwbuffer + U_CONSTANT_SIZE("Content-Length: xx\r\nContent-Type: application/json\r\n\r\n");
}
U_INTERNAL_ASSERT_EQUALS(u_get_unalignedp64(UClientImage_Base::wbuffer->data()), U_MULTICHAR_CONSTANT64('C','o','n','t','e','n','t','-'))
@ -42,5 +42,11 @@ UValue::pstringify = ptr;
UValue(*pkey, *pvalue).stringify();
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: 27\r\nContent-Type: application/json\r\n\r\n") + 27);
uint32_t content_length = (UValue::pstringify - ptr);
U_INTERNAL_ASSERT_EQUALS(content_length, 27)
(void) u_num2str32(content_length, ptr - U_CONSTANT_SIZE("xx\r\nContent-Type: application/json\r\n\r\n"));
UClientImage_Base::wbuffer->size_adjust_constant(U_CONSTANT_SIZE("Content-Length: xx\r\nContent-Type: application/json\r\n\r\n") + content_length);
-->

View File

@ -2,7 +2,7 @@ userver {
PORT 8080
PREFORK_CHILD 4
TCP_LINGER_SET -2
TCP_LINGER_SET 0
LISTEN_BACKLOG 256
DOCUMENT_ROOT benchmark/FrameworkBenchmarks/ULib/www
PID_FILE ../userver_tcp.pid

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1501,6 +1501,10 @@ U_EXPORT main(int argc, char* argv[])
U_TRACE(5, "main(%d)", argc)
UString byte_count = UString::humanReadableByteCountBin(U_STRING_MAX_SIZE);
U_ASSERT( byte_count == U_STRING_FROM_CONSTANT("4.0 GiB") )
#if defined(U_STDCPP_ENABLE) && defined(HAVE_CXX20) && defined(U_LINUX)
UString token = U_STRING_FROM_CONSTANT("a"),
firstname = U_STRING_FROM_CONSTANT("Victor"),