aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dns/thread.cpp
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:17 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:17 +0300
commitd3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch)
treedd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/dns/thread.cpp
parent72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff)
downloadydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/dns/thread.cpp')
-rw-r--r--library/cpp/dns/thread.cpp178
1 files changed, 89 insertions, 89 deletions
diff --git a/library/cpp/dns/thread.cpp b/library/cpp/dns/thread.cpp
index ca9c1ff8a1..8b27d2d527 100644
--- a/library/cpp/dns/thread.cpp
+++ b/library/cpp/dns/thread.cpp
@@ -1,133 +1,133 @@
#include "thread.h"
-#include "magic.h"
-
+#include "magic.h"
+
#include <util/network/socket.h>
#include <util/thread/factory.h>
#include <util/thread/lfqueue.h>
#include <util/system/event.h>
-#include <util/generic/vector.h>
+#include <util/generic/vector.h>
#include <util/generic/singleton.h>
-
+
using namespace NDns;
-
-namespace {
+
+namespace {
class TThreadedResolver: public IThreadFactory::IThreadAble, public TNonCopyable {
- struct TResolveRequest {
+ struct TResolveRequest {
inline TResolveRequest(const TString& host, ui16 port)
: Host(host)
, Port(port)
- {
- }
-
+ {
+ }
+
inline TNetworkAddressPtr Wait() {
E.Wait();
-
- if (!Error) {
- if (!Result) {
+
+ if (!Error) {
+ if (!Result) {
ythrow TNetworkResolutionError(EAI_AGAIN) << TStringBuf(": resolver down");
- }
-
- return Result;
- }
-
- Error->Raise();
-
+ }
+
+ return Result;
+ }
+
+ Error->Raise();
+
ythrow TNetworkResolutionError(EAI_FAIL) << TStringBuf(": shit happen");
- }
-
+ }
+
inline void Resolve() noexcept {
- try {
+ try {
Result = new TNetworkAddress(Host, Port);
- } catch (...) {
- Error = SaveError();
- }
-
- Wake();
- }
-
+ } catch (...) {
+ Error = SaveError();
+ }
+
+ Wake();
+ }
+
inline void Wake() noexcept {
E.Signal();
- }
-
+ }
+
TString Host;
ui16 Port;
TManualEvent E;
TNetworkAddressPtr Result;
- IErrorRef Error;
- };
-
- public:
+ IErrorRef Error;
+ };
+
+ public:
inline TThreadedResolver()
: E_(TSystemEvent::rAuto)
{
T_.push_back(SystemThreadFactory()->Run(this));
- }
-
+ }
+
inline ~TThreadedResolver() override {
Schedule(nullptr);
-
+
for (size_t i = 0; i < T_.size(); ++i) {
- T_[i]->Join();
- }
-
- {
+ T_[i]->Join();
+ }
+
+ {
TResolveRequest* rr = nullptr;
-
- while (Q_.Dequeue(&rr)) {
- if (rr) {
- rr->Wake();
- }
- }
- }
- }
-
- static inline TThreadedResolver* Instance() {
- return Singleton<TThreadedResolver>();
- }
-
+
+ while (Q_.Dequeue(&rr)) {
+ if (rr) {
+ rr->Wake();
+ }
+ }
+ }
+ }
+
+ static inline TThreadedResolver* Instance() {
+ return Singleton<TThreadedResolver>();
+ }
+
inline TNetworkAddressPtr Resolve(const TString& host, ui16 port) {
TResolveRequest rr(host, port);
-
- Schedule(&rr);
-
- return rr.Wait();
- }
-
- private:
- inline void Schedule(TResolveRequest* rr) {
- Q_.Enqueue(rr);
- E_.Signal();
- }
-
+
+ Schedule(&rr);
+
+ return rr.Wait();
+ }
+
+ private:
+ inline void Schedule(TResolveRequest* rr) {
+ Q_.Enqueue(rr);
+ E_.Signal();
+ }
+
void DoExecute() override {
- while (true) {
+ while (true) {
TResolveRequest* rr = nullptr;
-
- while (!Q_.Dequeue(&rr)) {
- E_.Wait();
- }
-
- if (rr) {
- rr->Resolve();
- } else {
- break;
- }
- }
-
+
+ while (!Q_.Dequeue(&rr)) {
+ E_.Wait();
+ }
+
+ if (rr) {
+ rr->Resolve();
+ } else {
+ break;
+ }
+ }
+
Schedule(nullptr);
- }
-
- private:
- TLockFreeQueue<TResolveRequest*> Q_;
+ }
+
+ private:
+ TLockFreeQueue<TResolveRequest*> Q_;
TSystemEvent E_;
typedef TAutoPtr<IThreadFactory::IThread> IThreadRef;
TVector<IThreadRef> T_;
- };
+ };
}
-
+
namespace NDns {
TNetworkAddressPtr ThreadedResolve(const TString& host, ui16 port) {
return TThreadedResolver::Instance()->Resolve(host, port);
- }
-}
+ }
+}