diff options
author | Alexey Borzenkov <snaury@yandex-team.ru> | 2022-02-10 16:47:43 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:43 +0300 |
commit | 330c83f8c116bd45316397b179275e9d87007e7d (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /library/cpp/grpc/server/grpc_server.h | |
parent | 22d92781ba2a10b7fb5b977b7d1a5c40ff53885f (diff) | |
download | ydb-330c83f8c116bd45316397b179275e9d87007e7d.tar.gz |
Restoring authorship annotation for Alexey Borzenkov <snaury@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/grpc/server/grpc_server.h')
-rw-r--r-- | library/cpp/grpc/server/grpc_server.h | 198 |
1 files changed, 99 insertions, 99 deletions
diff --git a/library/cpp/grpc/server/grpc_server.h b/library/cpp/grpc/server/grpc_server.h index 59ed364bc9..d6814a90a0 100644 --- a/library/cpp/grpc/server/grpc_server.h +++ b/library/cpp/grpc/server/grpc_server.h @@ -167,77 +167,77 @@ public: virtual void StopService() noexcept = 0; virtual void InitService(grpc::ServerCompletionQueue* cq, TLoggerPtr logger) = 0; virtual void SetGlobalLimiterHandle(TGlobalLimiter* limiter) = 0; - virtual bool IsUnsafeToShutdown() const = 0; - virtual size_t RequestsInProgress() const = 0; - - /** - * Called before service is added to the server builder. This allows - * service to inspect server options and initialize accordingly. - */ - virtual void SetServerOptions(const TServerOptions& options) = 0; + virtual bool IsUnsafeToShutdown() const = 0; + virtual size_t RequestsInProgress() const = 0; + + /** + * Called before service is added to the server builder. This allows + * service to inspect server options and initialize accordingly. + */ + virtual void SetServerOptions(const TServerOptions& options) = 0; }; template<typename T> class TGrpcServiceBase: public IGRpcService { public: - class TShutdownGuard { - using TOwner = TGrpcServiceBase<T>; - friend class TGrpcServiceBase<T>; - - public: - TShutdownGuard() - : Owner(nullptr) - { } - - ~TShutdownGuard() { - Release(); - } - - TShutdownGuard(TShutdownGuard&& other) - : Owner(other.Owner) - { - other.Owner = nullptr; - } - - TShutdownGuard& operator=(TShutdownGuard&& other) { - if (Y_LIKELY(this != &other)) { - Release(); - Owner = other.Owner; - other.Owner = nullptr; - } - return *this; - } - - explicit operator bool() const { - return bool(Owner); - } - - void Release() { - if (Owner) { - AtomicDecrement(Owner->GuardCount_); - Owner = nullptr; - } - } - - TShutdownGuard(const TShutdownGuard&) = delete; - TShutdownGuard& operator=(const TShutdownGuard&) = delete; - - private: - explicit TShutdownGuard(TOwner* owner) - : Owner(owner) - { } - - private: - TOwner* Owner; - }; - -public: + class TShutdownGuard { + using TOwner = TGrpcServiceBase<T>; + friend class TGrpcServiceBase<T>; + + public: + TShutdownGuard() + : Owner(nullptr) + { } + + ~TShutdownGuard() { + Release(); + } + + TShutdownGuard(TShutdownGuard&& other) + : Owner(other.Owner) + { + other.Owner = nullptr; + } + + TShutdownGuard& operator=(TShutdownGuard&& other) { + if (Y_LIKELY(this != &other)) { + Release(); + Owner = other.Owner; + other.Owner = nullptr; + } + return *this; + } + + explicit operator bool() const { + return bool(Owner); + } + + void Release() { + if (Owner) { + AtomicDecrement(Owner->GuardCount_); + Owner = nullptr; + } + } + + TShutdownGuard(const TShutdownGuard&) = delete; + TShutdownGuard& operator=(const TShutdownGuard&) = delete; + + private: + explicit TShutdownGuard(TOwner* owner) + : Owner(owner) + { } + + private: + TOwner* Owner; + }; + +public: using TCurrentGRpcService = T; void StopService() noexcept override { with_lock(Lock_) { - AtomicSet(ShuttingDown_, 1); - + AtomicSet(ShuttingDown_, 1); + // Send TryCansel to event (can be send after finishing). // Actual dtors will be called from grpc thread, so deadlock impossible for (auto* request : Requests_) { @@ -246,21 +246,21 @@ public: } } - TShutdownGuard ProtectShutdown() noexcept { - AtomicIncrement(GuardCount_); - if (IsShuttingDown()) { - AtomicDecrement(GuardCount_); - return { }; - } - - return TShutdownGuard(this); - }; - - bool IsUnsafeToShutdown() const override { - return AtomicGet(GuardCount_) > 0; - } - - size_t RequestsInProgress() const override { + TShutdownGuard ProtectShutdown() noexcept { + AtomicIncrement(GuardCount_); + if (IsShuttingDown()) { + AtomicDecrement(GuardCount_); + return { }; + } + + return TShutdownGuard(this); + }; + + bool IsUnsafeToShutdown() const override { + return AtomicGet(GuardCount_) > 0; + } + + size_t RequestsInProgress() const override { size_t c = 0; with_lock(Lock_) { c = Requests_.size(); @@ -268,9 +268,9 @@ public: return c; } - void SetServerOptions(const TServerOptions& options) override { - SslServer_ = bool(options.SslData); - NeedAuth_ = options.UseAuth; + void SetServerOptions(const TServerOptions& options) override { + SslServer_ = bool(options.SslData); + NeedAuth_ = options.UseAuth; } void SetGlobalLimiterHandle(TGlobalLimiter* /*limiter*/) override {} @@ -280,32 +280,32 @@ public: return AtomicGet(ShuttingDown_); } - bool SslServer() const { - return SslServer_; - } - + bool SslServer() const { + return SslServer_; + } + bool NeedAuth() const { return NeedAuth_; } - bool RegisterRequestCtx(ICancelableContext* req) { + bool RegisterRequestCtx(ICancelableContext* req) { with_lock(Lock_) { - auto r = Requests_.emplace(req); - Y_VERIFY(r.second, "Ctx already registered"); - - if (IsShuttingDown()) { - // Server is already shutting down - Requests_.erase(r.first); - return false; - } + auto r = Requests_.emplace(req); + Y_VERIFY(r.second, "Ctx already registered"); + + if (IsShuttingDown()) { + // Server is already shutting down + Requests_.erase(r.first); + return false; + } } - - return true; + + return true; } void DeregisterRequestCtx(ICancelableContext* req) { with_lock(Lock_) { - Y_VERIFY(Requests_.erase(req), "Ctx is not registered"); + Y_VERIFY(Requests_.erase(req), "Ctx is not registered"); } } @@ -313,15 +313,15 @@ protected: using TGrpcAsyncService = typename TCurrentGRpcService::AsyncService; TGrpcAsyncService Service_; - TGrpcAsyncService* GetService() override { + TGrpcAsyncService* GetService() override { return &Service_; } private: TAtomic ShuttingDown_ = 0; - TAtomic GuardCount_ = 0; + TAtomic GuardCount_ = 0; - bool SslServer_ = false; + bool SslServer_ = false; bool NeedAuth_ = false; THashSet<ICancelableContext*> Requests_; |