@@ -1654,8 +1654,6 @@ TServiceBase::TServiceBase(
1654
1654
Profiler_.AddFuncGauge (" /authentication_queue_size" , MakeStrong (this ), [this ] {
1655
1655
return AuthenticationQueueSize_.load (std::memory_order::relaxed);
1656
1656
});
1657
-
1658
- ServiceLivenessChecker_->Start ();
1659
1657
}
1660
1658
1661
1659
const TServiceId& TServiceBase::GetServiceId () const
@@ -2431,6 +2429,25 @@ void TServiceBase::DecrementActiveRequestCount()
2431
2429
void TServiceBase::InitContext (IServiceContext* /* context*/ )
2432
2430
{ }
2433
2431
2432
+ void TServiceBase::StartServiceLivenessChecker ()
2433
+ {
2434
+ // Fast path.
2435
+ if (ServiceLivenessCheckerStarted_.load (std::memory_order::relaxed)) {
2436
+ return ;
2437
+ }
2438
+ if (ServiceLivenessCheckerStarted_.exchange (true )) {
2439
+ return ;
2440
+ }
2441
+
2442
+ if (auto checker = ServiceLivenessChecker_.Acquire ()) {
2443
+ checker->Start ();
2444
+ // There may be concurrent ServiceLivenessChecker_.Exchange() call in Stop().
2445
+ if (!ServiceLivenessChecker_.Acquire ()) {
2446
+ YT_UNUSED_FUTURE (checker->Stop ());
2447
+ }
2448
+ }
2449
+ }
2450
+
2434
2451
void TServiceBase::RegisterDiscoverRequest (const TCtxDiscoverPtr& context)
2435
2452
{
2436
2453
auto payload = GetDiscoverRequestPayload (context);
@@ -2440,6 +2457,7 @@ void TServiceBase::RegisterDiscoverRequest(const TCtxDiscoverPtr& context)
2440
2457
auto it = DiscoverRequestsByPayload_.find (payload);
2441
2458
if (it == DiscoverRequestsByPayload_.end ()) {
2442
2459
readerGuard.Release ();
2460
+ StartServiceLivenessChecker ();
2443
2461
auto writerGuard = WriterGuard (DiscoverRequestsByPayloadLock_);
2444
2462
DiscoverRequestsByPayload_[payload].Insert (context, 0 );
2445
2463
} else {
@@ -2706,8 +2724,9 @@ TFuture<void> TServiceBase::Stop()
2706
2724
}
2707
2725
}
2708
2726
2709
- YT_UNUSED_FUTURE (ServiceLivenessChecker_->Stop ());
2710
-
2727
+ if (auto checker = ServiceLivenessChecker_.Exchange (nullptr )) {
2728
+ YT_UNUSED_FUTURE (checker->Stop ());
2729
+ }
2711
2730
return StopResult_.ToFuture ();
2712
2731
}
2713
2732
0 commit comments