diff options
author | elviandante <elviandante@yandex-team.ru> | 2022-02-10 16:49:47 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:47 +0300 |
commit | 643ddee8bd6125a18c4b1506c35bee857f64f4d2 (patch) | |
tree | 9b6a77d0a7c57a5e85a0ed2744f2a54ba1a46d9f /library/cpp | |
parent | 767f05356832cfac686778897626e124d257dbc8 (diff) | |
download | ydb-643ddee8bd6125a18c4b1506c35bee857f64f4d2.tar.gz |
Restoring authorship annotation for <elviandante@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/cache/cache.h | 222 | ||||
-rw-r--r-- | library/cpp/cache/ut/cache_ut.cpp | 40 | ||||
-rw-r--r-- | library/cpp/coroutine/engine/coroutine_ut.cpp | 22 | ||||
-rw-r--r-- | library/cpp/coroutine/engine/impl.h | 10 | ||||
-rw-r--r-- | library/cpp/http/fetch/httpload.h | 2 | ||||
-rw-r--r-- | library/cpp/logger/system.cpp | 6 | ||||
-rw-r--r-- | library/cpp/logger/system.h | 2 | ||||
-rw-r--r-- | library/cpp/monlib/counters/counters.h | 54 | ||||
-rw-r--r-- | library/cpp/monlib/service/service.cpp | 268 | ||||
-rw-r--r-- | library/cpp/monlib/service/service.h | 110 | ||||
-rw-r--r-- | library/cpp/monlib/ya.make | 2 |
11 files changed, 369 insertions, 369 deletions
diff --git a/library/cpp/cache/cache.h b/library/cpp/cache/cache.h index 6dc997076d..b16fdfeff1 100644 --- a/library/cpp/cache/cache.h +++ b/library/cpp/cache/cache.h @@ -1,13 +1,13 @@ #pragma once - + #include <util/generic/algorithm.h> #include <util/generic/ptr.h> -#include <util/generic/intrlist.h> -#include <util/generic/hash_set.h> +#include <util/generic/intrlist.h> +#include <util/generic/hash_set.h> #include <util/generic/vector.h> #include <util/generic/yexception.h> #include <utility> - + template <class TValue> struct TUniformSizeProvider { size_t operator()(const TValue&) { @@ -16,54 +16,54 @@ struct TUniformSizeProvider { }; template <typename TKey, typename TValue, class TSizeProvider = TUniformSizeProvider<TValue>> -class TLRUList { -public: +class TLRUList { +public: TLRUList(size_t maxSize, const TSizeProvider& sizeProvider = TSizeProvider()) - : List() + : List() , SizeProvider(sizeProvider) , ItemsAmount(0) , TotalSize(0) - , MaxSize(maxSize) + , MaxSize(maxSize) { } - + public: struct TItem: public TIntrusiveListItem<TItem> { - typedef TIntrusiveListItem<TItem> TBase; - TItem(const TKey& key, const TValue& value = TValue()) - : TBase() - , Key(key) - , Value(value) + typedef TIntrusiveListItem<TItem> TBase; + TItem(const TKey& key, const TValue& value = TValue()) + : TBase() + , Key(key) + , Value(value) { } - - TItem(const TItem& rhs) - : TBase() - , Key(rhs.Key) - , Value(rhs.Value) + + TItem(const TItem& rhs) + : TBase() + , Key(rhs.Key) + , Value(rhs.Value) { } - + bool operator<(const TItem& rhs) const { - return Key < rhs.Key; - } + return Key < rhs.Key; + } bool operator==(const TItem& rhs) const { - return Key == rhs.Key; - } - - TKey Key; - TValue Value; - - struct THash { + return Key == rhs.Key; + } + + TKey Key; + TValue Value; + + struct THash { size_t operator()(const TItem& item) const { return ::THash<TKey>()(item.Key); } - }; - }; - -public: - TItem* Insert(TItem* item) { + }; + }; + +public: + TItem* Insert(TItem* item) { List.PushBack(item); ++ItemsAmount; TotalSize += SizeProvider(item->Value); @@ -76,26 +76,26 @@ public: if (TotalSize > MaxSize && ItemsAmount > 1) { deleted = GetOldest(); Erase(deleted); - } + } return deleted; - } + } - TItem* GetOldest() { + TItem* GetOldest() { typename TListType::TIterator it = List.Begin(); Y_ASSERT(it != List.End()); - return &*it; - } + return &*it; + } - void Erase(TItem* item) { - item->Unlink(); + void Erase(TItem* item) { + item->Unlink(); --ItemsAmount; TotalSize -= SizeProvider(item->Value); - } + } - void Promote(TItem* item) { - item->Unlink(); + void Promote(TItem* item) { + item->Unlink(); List.PushBack(item); - } + } size_t GetSize() const { return ItemsAmount; @@ -115,15 +115,15 @@ public: MaxSize = newSize; } -private: +private: typedef TIntrusiveList<TItem> TListType; TListType List; TSizeProvider SizeProvider; size_t ItemsAmount; size_t TotalSize; - size_t MaxSize; -}; - + size_t MaxSize; +}; + template <typename TKey, typename TValue> class TLFUList { public: @@ -374,36 +374,36 @@ private: }; template <typename TKey, typename TValue, typename TListType, typename TDeleter> -class TCache { +class TCache { typedef typename TListType::TItem TItem; - typedef typename TItem::THash THash; + typedef typename TItem::THash THash; typedef THashMultiSet<TItem, THash> TIndex; - typedef typename TIndex::iterator TIndexIterator; - typedef typename TIndex::const_iterator TIndexConstIterator; - -public: - class TIterator { - public: - explicit TIterator(const TIndexConstIterator& iter) - : Iter(iter) + typedef typename TIndex::iterator TIndexIterator; + typedef typename TIndex::const_iterator TIndexConstIterator; + +public: + class TIterator { + public: + explicit TIterator(const TIndexConstIterator& iter) + : Iter(iter) { } TValue& operator*() { - return const_cast<TValue&>(Iter->Value); - } + return const_cast<TValue&>(Iter->Value); + } TValue* operator->() { - return const_cast<TValue*>(&Iter->Value); - } - + return const_cast<TValue*>(&Iter->Value); + } + bool operator==(const TIterator& rhs) const { - return Iter == rhs.Iter; - } + return Iter == rhs.Iter; + } bool operator!=(const TIterator& rhs) const { - return Iter != rhs.Iter; - } + return Iter != rhs.Iter; + } TIterator& operator++() { ++Iter; @@ -420,17 +420,17 @@ public: friend class TCache<TKey, TValue, TListType, TDeleter>; - private: - TIndexConstIterator Iter; - }; - + private: + TIndexConstIterator Iter; + }; + TCache(TListType&& list, bool multiValue = false) - : Index() + : Index() , List(std::move(list)) , MultiValue(multiValue) - { - } - + { + } + ~TCache() { Clear(); } @@ -443,17 +443,17 @@ public: return TIterator(Index.begin()); } - TIterator End() const { - return TIterator(Index.end()); - } - - TIterator Find(const TKey& key) { - TIndexIterator it = Index.find(TItem(key)); - if (it != Index.end()) - List.Promote(const_cast<TItem*>(&*it)); - return TIterator(it); - } - + TIterator End() const { + return TIterator(Index.end()); + } + + TIterator Find(const TKey& key) { + TIndexIterator it = Index.find(TItem(key)); + if (it != Index.end()) + List.Promote(const_cast<TItem*>(&*it)); + return TIterator(it); + } + TIterator FindWithoutPromote(const TKey& key) const { return TIterator(Index.find(TItem(key))); } @@ -480,7 +480,7 @@ public: if (!MultiValue && Index.find(tmpItem) != Index.end()) return false; TIndexIterator it = Index.insert(tmpItem); - + TItem* insertedItem = const_cast<TItem*>(&*it); auto removedItem = List.Insert(insertedItem); auto insertedWasRemoved = removedItem == insertedItem; @@ -490,11 +490,11 @@ public: insertedWasRemoved = insertedWasRemoved || insertedItem == removedItem; EraseFromIndex(removedItem); } - } - + } + Y_ASSERT(Index.size() == List.GetSize()); return !insertedWasRemoved; - } + } void Update(const TKey& key, const TValue& value) { if (MultiValue) @@ -508,18 +508,18 @@ public: Y_ASSERT(Index.size() == List.GetSize()); } - void Erase(TIterator it) { + void Erase(TIterator it) { TItem* item = const_cast<TItem*>(&*it.Iter); List.Erase(item); TDeleter::Destroy(item->Value); - Index.erase(it.Iter); + Index.erase(it.Iter); Y_ASSERT(Index.size() == List.GetSize()); - } + } - bool Empty() const { - return Index.empty(); - } + bool Empty() const { + return Index.empty(); + } void Clear() { for (TIndexIterator it = Index.begin(); it != Index.end(); ++it) { @@ -545,8 +545,8 @@ public: return List.GetMaxSize(); } -protected: - TIndex Index; +protected: + TIndex Index; TListType List; bool MultiValue; @@ -566,14 +566,14 @@ protected: Y_ASSERT(it != End()); Index.erase(it.Iter); } -}; - -struct TNoopDelete { - template <class T> +}; + +struct TNoopDelete { + template <class T> static inline void Destroy(const T&) noexcept { - } -}; - + } +}; + template <typename TKey, typename TValue, typename TDeleter = TNoopDelete, class TSizeProvider = TUniformSizeProvider<TValue>> class TLRUCache: public TCache<TKey, TValue, TLRUList<TKey, TValue, TSizeProvider>, TDeleter> { using TListType = TLRUList<TKey, TValue, TSizeProvider>; @@ -584,13 +584,13 @@ public: : TBase(TListType(maxSize, sizeProvider), multiValue) { } - + public: typedef typename TBase::TIterator TIterator; - TValue& GetOldest() { - return TBase::List.GetOldest()->Value; - } + TValue& GetOldest() { + return TBase::List.GetOldest()->Value; + } TIterator FindOldest() { return TBase::Empty() ? TBase::End() : this->FindByItem(TBase::List.GetOldest()); @@ -599,7 +599,7 @@ public: size_t TotalSize() const { return TBase::List.GetTotalSize(); } -}; +}; template <typename TKey, typename TValue, typename TDeleter = TNoopDelete> class TLFUCache: public TCache<TKey, TValue, TLFUList<TKey, TValue>, TDeleter> { diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 329872cfde..f3a1a502af 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -116,31 +116,31 @@ Y_UNIT_TEST_SUITE(TCacheTest) { Y_UNIT_TEST(SimpleTest) { typedef TLRUCache<int, TString> TCache; - TCache s(2); // size 2 - s.Insert(1, "abcd"); - UNIT_ASSERT(s.Find(1) != s.End()); - UNIT_ASSERT_EQUAL(*s.Find(1), "abcd"); - s.Insert(2, "defg"); - UNIT_ASSERT(s.GetOldest() == "abcd"); - s.Insert(3, "hjkl"); - UNIT_ASSERT(s.GetOldest() == "defg"); - // key 1 will be deleted - UNIT_ASSERT(s.Find(1) == s.End()); - UNIT_ASSERT(s.Find(2) != s.End()); - UNIT_ASSERT(*s.Find(2) == "defg"); - UNIT_ASSERT(s.Find(3) != s.End()); - UNIT_ASSERT(*s.Find(3) == "hjkl"); - + TCache s(2); // size 2 + s.Insert(1, "abcd"); + UNIT_ASSERT(s.Find(1) != s.End()); + UNIT_ASSERT_EQUAL(*s.Find(1), "abcd"); + s.Insert(2, "defg"); + UNIT_ASSERT(s.GetOldest() == "abcd"); + s.Insert(3, "hjkl"); + UNIT_ASSERT(s.GetOldest() == "defg"); + // key 1 will be deleted + UNIT_ASSERT(s.Find(1) == s.End()); + UNIT_ASSERT(s.Find(2) != s.End()); + UNIT_ASSERT(*s.Find(2) == "defg"); + UNIT_ASSERT(s.Find(3) != s.End()); + UNIT_ASSERT(*s.Find(3) == "hjkl"); + UNIT_ASSERT(!s.Insert(3, "abcd")); UNIT_ASSERT(*s.Find(3) == "hjkl"); s.Update(3, "abcd"); UNIT_ASSERT(*s.Find(3) == "abcd"); - TCache::TIterator it = s.Find(3); - s.Erase(it); - UNIT_ASSERT(s.Find(3) == s.End()); + TCache::TIterator it = s.Find(3); + s.Erase(it); + UNIT_ASSERT(s.Find(3) == s.End()); } - + Y_UNIT_TEST(LRUWithCustomSizeProviderTest) { typedef TLRUCache<int, TString, TNoopDelete, size_t(*)(const TString&)> TCache; TCache s(10, false, [](auto& string) { return string.size(); }); // size 10 @@ -355,7 +355,7 @@ Y_UNIT_TEST_SUITE(TCacheTest) { s.Insert(3, "789"); UNIT_ASSERT(s.Find(1) != s.End()); // Key 2 should have been deleted } -} +} Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { typedef TThreadSafeCache<ui32, TString, ui32> TCache; diff --git a/library/cpp/coroutine/engine/coroutine_ut.cpp b/library/cpp/coroutine/engine/coroutine_ut.cpp index 8b372496a2..0c183a14f4 100644 --- a/library/cpp/coroutine/engine/coroutine_ut.cpp +++ b/library/cpp/coroutine/engine/coroutine_ut.cpp @@ -189,18 +189,18 @@ public: void RunTask2(TCont*) { j = 2; } -}; - -void TCoroTest::TestMemFun() { +}; + +void TCoroTest::TestMemFun() { i0 = 0; - TContExecutor e(32000); - TTestObject obj; - e.Create<TTestObject, &TTestObject::RunTask1>(&obj, "test1"); - e.Execute<TTestObject, &TTestObject::RunTask2>(&obj); - UNIT_ASSERT_EQUAL(obj.i, 1); - UNIT_ASSERT_EQUAL(obj.j, 2); -} - + TContExecutor e(32000); + TTestObject obj; + e.Create<TTestObject, &TTestObject::RunTask1>(&obj, "test1"); + e.Execute<TTestObject, &TTestObject::RunTask2>(&obj); + UNIT_ASSERT_EQUAL(obj.i, 1); + UNIT_ASSERT_EQUAL(obj.j, 2); +} + void TCoroTest::TestSimpleX2() { { i0 = 0; diff --git a/library/cpp/coroutine/engine/impl.h b/library/cpp/coroutine/engine/impl.h index 283a96ecf1..47fd23a944 100644 --- a/library/cpp/coroutine/engine/impl.h +++ b/library/cpp/coroutine/engine/impl.h @@ -130,10 +130,10 @@ static void ContHelperFunc(TCont* cont, void* arg) { } template <typename T, void (T::*M)(TCont*)> -static void ContHelperMemberFunc(TCont* c, void* arg) { +static void ContHelperMemberFunc(TCont* c, void* arg) { ((reinterpret_cast<T*>(arg))->*M)(c); -} - +} + class IUserEvent : public TIntrusiveListItem<IUserEvent> { @@ -176,7 +176,7 @@ public: void Execute(T* obj) noexcept { Execute(ContHelperMemberFunc<T, M>, obj); } - + template <class Functor> TCont* Create( Functor& f, @@ -194,7 +194,7 @@ public: ) noexcept { return Create(ContHelperMemberFunc<T, M>, obj, name, customStackSize); } - + TCont* Create( TContFunc func, void* arg, diff --git a/library/cpp/http/fetch/httpload.h b/library/cpp/http/fetch/httpload.h index e22e4b809e..68b5ff60da 100644 --- a/library/cpp/http/fetch/httpload.h +++ b/library/cpp/http/fetch/httpload.h @@ -6,7 +6,7 @@ #include <util/system/compat.h> #include <util/string/vector.h> -#include <util/network/ip.h> +#include <util/network/ip.h> #include <library/cpp/uri/http_url.h> #include <library/cpp/http/misc/httpcodes.h> diff --git a/library/cpp/logger/system.cpp b/library/cpp/logger/system.cpp index 42233f63d2..4cdaf92f53 100644 --- a/library/cpp/logger/system.cpp +++ b/library/cpp/logger/system.cpp @@ -32,14 +32,14 @@ TSysLogBackend::TSysLogBackend(const char* ident, EFacility facility, int flags) LOG_LOCAL6, LOG_LOCAL7}; - int sysflags = LOG_NDELAY | LOG_PID; + int sysflags = LOG_NDELAY | LOG_PID; if (flags & LogPerror) { - sysflags |= LOG_PERROR; + sysflags |= LOG_PERROR; } if (flags & LogCons) { - sysflags |= LOG_CONS; + sysflags |= LOG_CONS; } openlog(Ident.data(), sysflags, f2sf[(size_t)facility]); diff --git a/library/cpp/logger/system.h b/library/cpp/logger/system.h index b8c60b3023..eb566cb5da 100644 --- a/library/cpp/logger/system.h +++ b/library/cpp/logger/system.h @@ -32,7 +32,7 @@ public: LogPerror = 1, LogCons = 2 }; - + TSysLogBackend(const char* ident, EFacility facility, int flags = 0); ~TSysLogBackend() override; diff --git a/library/cpp/monlib/counters/counters.h b/library/cpp/monlib/counters/counters.h index 038b55f0c8..b2e2d2794b 100644 --- a/library/cpp/monlib/counters/counters.h +++ b/library/cpp/monlib/counters/counters.h @@ -1,5 +1,5 @@ #pragma once - + #include <util/datetime/base.h> #include <util/generic/algorithm.h> #include <util/generic/list.h> @@ -8,27 +8,27 @@ #include <util/generic/singleton.h> #include <util/generic/vector.h> #include <util/str_stl.h> -#include <util/stream/output.h> +#include <util/stream/output.h> #include <util/string/util.h> -#include <util/system/atomic.h> +#include <util/system/atomic.h> #include <util/system/defaults.h> #include <util/system/guard.h> #include <util/system/sem.h> #include <util/system/spinlock.h> - + #include <array> -namespace NMonitoring { +namespace NMonitoring { #define BEGIN_OUTPUT_COUNTERS \ void OutputImpl(IOutputStream& out) { \ char prettyBuf[32]; #define END_OUTPUT_COUNTERS \ out.Flush(); \ } - + #define OUTPUT_NAMED_COUNTER(var, name) out << name << ": \t" << var << NMonitoring::PrettyNum(var, prettyBuf, 32) << '\n' #define OUTPUT_COUNTER(var) OUTPUT_NAMED_COUNTER(var, #var); - + char* PrettyNumShort(i64 val, char* buf, size_t size); char* PrettyNum(i64 val, char* buf, size_t size); @@ -39,13 +39,13 @@ namespace NMonitoring { public: using TValue = TAtomic; using TValueBase = TAtomicBase; - + TDeprecatedCounter() : Value() , Derivative(false) { } - + TDeprecatedCounter(TValue value, bool derivative = false) : Value(value) , Derivative(derivative) @@ -62,7 +62,7 @@ namespace NMonitoring { TValueBase Val() const { return AtomicGet(Value); } - + void Set(TValue val) { AtomicSet(Value, val); } @@ -73,7 +73,7 @@ namespace NMonitoring { TValueBase Dec() { return AtomicDecrement(Value); } - + TValueBase Add(const TValue val) { return AtomicAdd(Value, val); } @@ -88,21 +88,21 @@ namespace NMonitoring { void operator++(int) { Inc(); } - + void operator--() { Dec(); } void operator--(int) { Dec(); } - + void operator+=(TValue rhs) { Add(rhs); } void operator-=(TValue rhs) { Sub(rhs); } - + TValueBase operator=(TValue rhs) { AtomicSwap(&Value, rhs); return rhs; @@ -111,7 +111,7 @@ namespace NMonitoring { bool operator!() const { return AtomicGet(Value) == 0; } - + TAtomic& GetAtomic() { return Value; } @@ -120,14 +120,14 @@ namespace NMonitoring { TAtomic Value; bool Derivative; }; - + template <typename T> struct TDeprecatedCountersBase { virtual ~TDeprecatedCountersBase() { } - + virtual void OutputImpl(IOutputStream&) = 0; - + static T& Instance() { return *Singleton<T>(); } @@ -337,14 +337,14 @@ namespace NMonitoring { } static inline IOutputStream& operator<<(IOutputStream& o, const NMonitoring::TDeprecatedCounter& rhs) { - return o << rhs.Val(); -} - -template <size_t N> + return o << rhs.Val(); +} + +template <size_t N> static inline IOutputStream& operator<<(IOutputStream& o, const std::array<NMonitoring::TDeprecatedCounter, N>& rhs) { for (typename std::array<NMonitoring::TDeprecatedCounter, N>::const_iterator it = rhs.begin(); it != rhs.end(); ++it) { - if (!!*it) - o << *it << Endl; - } - return o; -} + if (!!*it) + o << *it << Endl; + } + return o; +} diff --git a/library/cpp/monlib/service/service.cpp b/library/cpp/monlib/service/service.cpp index 929efbf816..559aba661e 100644 --- a/library/cpp/monlib/service/service.cpp +++ b/library/cpp/monlib/service/service.cpp @@ -5,64 +5,64 @@ #include <library/cpp/http/fetch/httpheader.h> #include <library/cpp/http/fetch/httpfsm.h> #include <library/cpp/uri/http_url.h> - + #include <util/generic/buffer.h> #include <util/stream/str.h> #include <util/stream/buffer.h> #include <util/stream/zerocopy.h> #include <util/string/vector.h> -namespace NMonitoring { +namespace NMonitoring { class THttpClient: public IHttpRequest { - public: + public: void ServeRequest(THttpInput& in, IOutputStream& out, const NAddr::IRemoteAddr* remoteAddr, const THandler& Handler) { - try { - try { + try { + try { RemoteAddr = remoteAddr; - THttpHeaderParser parser; - parser.Init(&Header); + THttpHeaderParser parser; + parser.Init(&Header); if (parser.Execute(in.FirstLine().data(), in.FirstLine().size()) < 0) { - out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; - return; - } + out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; + return; + } if (Url.Parse(Header.GetUrl().data()) != THttpURL::ParsedOK) { - out << "HTTP/1.1 400 Invalid url\r\nConnection: Close\r\n\r\n"; - return; - } + out << "HTTP/1.1 400 Invalid url\r\nConnection: Close\r\n\r\n"; + return; + } TString path = GetPath(); if (!path.StartsWith('/')) { out << "HTTP/1.1 400 Bad request\r\nConnection: Close\r\n\r\n"; return; } Headers = &in.Headers(); - CgiParams.Scan(Url.Get(THttpURL::FieldQuery)); + CgiParams.Scan(Url.Get(THttpURL::FieldQuery)); } catch (...) { - out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n"; + out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n"; YSYSLOG(TLOG_ERR, "THttpClient: internal error while serving monitoring request: %s", CurrentExceptionMessage().data()); - } - + } + if (Header.http_method == HTTP_METHOD_POST) TransferData(&in, &PostContent); - Handler(out, *this); - out.Finish(); + Handler(out, *this); + out.Finish(); } catch (...) { auto msg = CurrentExceptionMessage(); out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n" << msg; out.Finish(); YSYSLOG(TLOG_ERR, "THttpClient: error while serving monitoring request: %s", msg.data()); - } - } - + } + } + const char* GetURI() const override { return Header.request_uri.c_str(); - } + } const char* GetPath() const override { - return Url.Get(THttpURL::FieldPath); - } + return Url.Get(THttpURL::FieldPath); + } const TCgiParameters& GetParams() const override { - return CgiParams; - } + return CgiParams; + } const TCgiParameters& GetPostParams() const override { if (PostParams.empty() && !PostContent.Buffer().Empty()) const_cast<THttpClient*>(this)->ScanPostParams(); @@ -90,125 +90,125 @@ namespace NMonitoring { return RemoteAddr ? NAddr::PrintHostAndPort(*RemoteAddr) : TString(); } - private: - THttpRequestHeader Header; + private: + THttpRequestHeader Header; const THttpHeaders* Headers = nullptr; - THttpURL Url; + THttpURL Url; TCgiParameters CgiParams; TCgiParameters PostParams; TBufferOutput PostContent; const NAddr::IRemoteAddr* RemoteAddr = nullptr; - }; - - /* TCoHttpServer */ - + }; + + /* TCoHttpServer */ + class TCoHttpServer::TConnection: public THttpClient { - public: + public: TConnection(const TCoHttpServer::TAcceptFull& acc, const TCoHttpServer& parent) : Socket(acc.S->Release()) , RemoteAddr(acc.Remote) - , Parent(parent) - { - } - + , Parent(parent) + { + } + void operator()(TCont* c) { - try { - THolder<TConnection> me(this); - TContIO io(Socket, c); - THttpInput in(&io); - THttpOutput out(&io, &in); - // buffer reply so there will be ne context switching - TStringStream s; + try { + THolder<TConnection> me(this); + TContIO io(Socket, c); + THttpInput in(&io); + THttpOutput out(&io, &in); + // buffer reply so there will be ne context switching + TStringStream s; ServeRequest(in, s, RemoteAddr, Parent.Handler); - out << s.Str(); - out.Finish(); + out << s.Str(); + out.Finish(); } catch (...) { YSYSLOG(TLOG_WARNING, "TCoHttpServer::TConnection: error: %s\n", CurrentExceptionMessage().data()); - } - } + } + } - private: - TSocketHolder Socket; + private: + TSocketHolder Socket; const NAddr::IRemoteAddr* RemoteAddr; - const TCoHttpServer& Parent; - }; - + const TCoHttpServer& Parent; + }; + TCoHttpServer::TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler) - : Executor(executor) - , Listener(this, &executor) + : Executor(executor) + , Listener(this, &executor) , Handler(std::move(handler)) - , BindAddr(bindAddr) - , Port(port) - { + , BindAddr(bindAddr) + , Port(port) + { try { Listener.Bind(TIpAddress(bindAddr, port)); } catch (yexception e) { Y_FAIL("TCoHttpServer::TCoHttpServer: couldn't bind to %s:%d\n", bindAddr.data(), port); } - } - - void TCoHttpServer::Start() { - Listener.Listen(); - } - - void TCoHttpServer::Stop() { - Listener.Stop(); - } - + } + + void TCoHttpServer::Start() { + Listener.Listen(); + } + + void TCoHttpServer::Stop() { + Listener.Stop(); + } + void TCoHttpServer::OnAcceptFull(const TAcceptFull& acc) { THolder<TConnection> conn(new TConnection(acc, *this)); - Executor.Create(*conn, "client"); + Executor.Create(*conn, "client"); Y_UNUSED(conn.Release()); - } - - void TCoHttpServer::OnError() { - throw; // just rethrow - } - + } + + void TCoHttpServer::OnError() { + throw; // just rethrow + } + void TCoHttpServer::ProcessRequest(IOutputStream& out, const IHttpRequest& request) { - try { - TNetworkAddress addr(BindAddr, Port); - TSocket sock(addr); - TSocketOutput sock_out(sock); - TSocketInput sock_in(sock); + try { + TNetworkAddress addr(BindAddr, Port); + TSocket sock(addr); + TSocketOutput sock_out(sock); + TSocketInput sock_in(sock); sock_out << "GET " << request.GetURI() << " HTTP/1.0\r\n\r\n"; - THttpInput http_in(&sock_in); - try { - out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; - TransferData(&http_in, &out); + THttpInput http_in(&sock_in); + try { + out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; + TransferData(&http_in, &out); } catch (...) { YSYSLOG(TLOG_DEBUG, "TCoHttpServer: while getting data from backend: %s", CurrentExceptionMessage().data()); - } + } } catch (const yexception& /*e*/) { - out << "HTTP/1.1 500 Internal server error\nConnection: Close\n\n"; + out << "HTTP/1.1 500 Internal server error\nConnection: Close\n\n"; YSYSLOG(TLOG_DEBUG, "TCoHttpServer: while getting data from backend: %s", CurrentExceptionMessage().data()); - } - } - - /* TMtHttpServer */ - + } + } + + /* TMtHttpServer */ + class TMtHttpServer::TConnection: public TClientRequest, public THttpClient { - public: - TConnection(const TMtHttpServer& parent) - : Parent(parent) - { - } - + public: + TConnection(const TMtHttpServer& parent) + : Parent(parent) + { + } + bool Reply(void*) override { ServeRequest(Input(), Output(), NAddr::GetPeerAddr(Socket()).Get(), Parent.Handler); - return true; - } - - private: - const TMtHttpServer& Parent; - }; + return true; + } + private: + const TMtHttpServer& Parent; + }; + TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool) - : THttpServer(this, options, pool) + : THttpServer(this, options, pool) , Handler(std::move(handler)) - { - } - + { + } + TMtHttpServer::TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool) : THttpServer(this, /* mainWorkers = */pool, /* failWorkers = */pool, options) , Handler(std::move(handler)) @@ -233,36 +233,36 @@ namespace NMonitoring { THttpServer::Stop(); } - TClientRequest* TMtHttpServer::CreateClient() { - return new TConnection(*this); - } - - /* TService */ - + TClientRequest* TMtHttpServer::CreateClient() { + return new TConnection(*this); + } + + /* TService */ + TMonService::TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort, THandler coHandler, THandler mtHandler) : CoServer(executor, "127.0.0.1", internalPort, std::move(coHandler)) , MtServer(THttpServerOptions(externalPort), std::bind(&TMonService::DispatchRequest, this, std::placeholders::_1, std::placeholders::_2)) , MtHandler(std::move(mtHandler)) - { - } - - void TMonService::Start() { - MtServer.Start(); - CoServer.Start(); - } - - void TMonService::Stop() { - MtServer.Stop(); - CoServer.Stop(); - } - + { + } + + void TMonService::Start() { + MtServer.Start(); + CoServer.Start(); + } + + void TMonService::Stop() { + MtServer.Stop(); + CoServer.Stop(); + } + void TMonService::DispatchRequest(IOutputStream& out, const IHttpRequest& request) { - if (strcmp(request.GetPath(), "/") == 0) { - out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; - MtHandler(out, request); + if (strcmp(request.GetPath(), "/") == 0) { + out << "HTTP/1.1 200 Ok\nConnection: Close\n\n"; + MtHandler(out, request); } else - CoServer.ProcessRequest(out, request); - } - + CoServer.ProcessRequest(out, request); + } + } diff --git a/library/cpp/monlib/service/service.h b/library/cpp/monlib/service/service.h index 2f66dddaf8..2ea5f0f4a2 100644 --- a/library/cpp/monlib/service/service.h +++ b/library/cpp/monlib/service/service.h @@ -1,65 +1,65 @@ #pragma once - + #include <library/cpp/coroutine/engine/impl.h> #include <library/cpp/coroutine/listener/listen.h> #include <library/cpp/http/fetch/httpheader.h> #include <library/cpp/http/server/http.h> #include <library/cpp/logger/all.h> -#include <util/network/ip.h> +#include <util/network/ip.h> #include <library/cpp/cgiparam/cgiparam.h> - + #include <functional> - + struct TMonitor; - -namespace NMonitoring { - struct IHttpRequest { + +namespace NMonitoring { + struct IHttpRequest { virtual ~IHttpRequest() { } - virtual const char* GetURI() const = 0; - virtual const char* GetPath() const = 0; + virtual const char* GetURI() const = 0; + virtual const char* GetPath() const = 0; virtual const TCgiParameters& GetParams() const = 0; virtual const TCgiParameters& GetPostParams() const = 0; virtual TStringBuf GetPostContent() const = 0; virtual HTTP_METHOD GetMethod() const = 0; virtual const THttpHeaders& GetHeaders() const = 0; virtual TString GetRemoteAddr() const = 0; - }; - // first param - output stream to write result to - // second param - URL of request + }; + // first param - output stream to write result to + // second param - URL of request typedef std::function<void(IOutputStream&, const IHttpRequest&)> THandler; - + class TCoHttpServer: private TContListener::ICallBack { - public: - // initialize and schedule coroutines for execution + public: + // initialize and schedule coroutines for execution TCoHttpServer(TContExecutor& executor, const TString& bindAddr, TIpPort port, THandler handler); - void Start(); - void Stop(); - - // this function implements THandler interface - // by forwarding it to the httpserver - // @note this call may be blocking; don't use inside coroutines - // @throws may throw in case of connection error, etc + void Start(); + void Stop(); + + // this function implements THandler interface + // by forwarding it to the httpserver + // @note this call may be blocking; don't use inside coroutines + // @throws may throw in case of connection error, etc void ProcessRequest(IOutputStream&, const IHttpRequest&); - private: - class TConnection; - - // ICallBack implementation + private: + class TConnection; + + // ICallBack implementation void OnAcceptFull(const TAcceptFull& a) override; void OnError() override; - private: - TContExecutor& Executor; - TContListener Listener; - THandler Handler; + private: + TContExecutor& Executor; + TContListener Listener; + THandler Handler; TString BindAddr; - TIpPort Port; - }; - + TIpPort Port; + }; + class TMtHttpServer: public THttpServer, private THttpServer::ICallBack { - public: + public: TMtHttpServer(const TOptions& options, THandler handler, IThreadFactory* pool = nullptr); TMtHttpServer(const TOptions& options, THandler handler, TSimpleSharedPtr<IThreadPool> pool); @@ -82,31 +82,31 @@ namespace NMonitoring { */ void Stop(); - private: - class TConnection; + private: + class TConnection; TClientRequest* CreateClient() override; - - THandler Handler; - }; - - // this class implements hybrid coroutine and threaded approach - // requests for main page which holds counters and simple tables are served in a thread - // requests for other pages which include access with inter-thread synchonization - // will be served in a coroutine context - class TMonService { - public: + + THandler Handler; + }; + + // this class implements hybrid coroutine and threaded approach + // requests for main page which holds counters and simple tables are served in a thread + // requests for other pages which include access with inter-thread synchonization + // will be served in a coroutine context + class TMonService { + public: TMonService(TContExecutor& executor, TIpPort internalPort, TIpPort externalPort, THandler coHandler, THandler mtHandler); - void Start(); - void Stop(); + void Start(); + void Stop(); - protected: + protected: void DispatchRequest(IOutputStream& out, const IHttpRequest&); - private: - TCoHttpServer CoServer; - TMtHttpServer MtServer; - THandler MtHandler; - }; - + private: + TCoHttpServer CoServer; + TMtHttpServer MtServer; + THandler MtHandler; + }; + } diff --git a/library/cpp/monlib/ya.make b/library/cpp/monlib/ya.make index 9bd236d6fd..9736635f59 100644 --- a/library/cpp/monlib/ya.make +++ b/library/cpp/monlib/ya.make @@ -42,4 +42,4 @@ RECURSE( service/auth/tvm service/pages service/pages/tablesorter -) +) |