Module: Gitlab::ExclusiveLeaseHelpers
- Included in:
- AuthorizedProjectUpdate::ProjectRecalculateWorker, BatchedGitRefUpdates::CleanupSchedulerService, BatchedGitRefUpdates::ProjectCleanupService, Ci::ArchiveTraceService, Ci::BuildTraceChunk, Ci::ClickHouse::DataIngestion::FinishedPipelinesSyncService, Ci::CreateCommitStatusService, Ci::JobArtifacts::DestroyAllExpiredService, Ci::JobArtifacts::UpdateUnknownLockedStatusService, Ci::PipelineArtifacts::DestroyAllExpiredService, Ci::Pipelines::AddJobService, Ci::UnlockPipelineService, Ci::UpdateBuildStateService, ClickHouse::MigrationSupport::ExclusiveLock, ClickHouse::SyncStrategies::BaseSyncStrategy, CounterAttribute, DesignManagement::Version, Environments::AutoRecoverService, Environments::AutoStopService, Environments::ScheduleToDeleteReviewAppsService, Ci::Trace, Counters::BufferedCounter, GithubImport::UserFinder, Import::SourceUserMapper, GitlabServicePingWorker, LooseForeignKeys::CleanupWorker, Members::DestroyService, MergeRequests::MergeabilityCheckService, MergeRequests::ProcessScheduledMergeWorker, PipelineScheduleWorker, PostReceive, Projects::RemoveRefs, RepositoryUpdateRemoteMirrorWorker, ScheduleMigrateExternalDiffsWorker, Users::MigrateRecordsToGhostUserInBatchesWorker, WebHooks::LogExecutionService
- Defined in:
- lib/gitlab/exclusive_lease_helpers.rb,
lib/gitlab/exclusive_lease_helpers/sleeping_lock.rb
Overview
This module provides helper methods which are integrated with GitLab::ExclusiveLease
Defined Under Namespace
Classes: SleepingLock
Constant Summary collapse
- FailedToObtainLockError =
Class.new(StandardError)
Instance Method Summary collapse
-
#in_lock(key, ttl: 1.minute, retries: 10, sleep_sec: 0.01.seconds) ⇒ Object
This helper method blocks a process/thread until the lease can be acquired, either due to the lease TTL expiring, or due to the current holder explicitly releasing their hold.
Instance Method Details
#in_lock(key, ttl: 1.minute, retries: 10, sleep_sec: 0.01.seconds) ⇒ Object
This helper method blocks a process/thread until the lease can be acquired, either due to the lease TTL expiring, or due to the current holder explicitly releasing their hold.
If the lease cannot be obtained, raises ‘FailedToObtainLockError`.
(from 1 to retries - 1)
Note: It’s basically discouraged to use this method in a webserver thread,
because this ties up all thread related resources until all `retries` are consumed.
This could potentially eat up all connection pools.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gitlab/exclusive_lease_helpers.rb', line 31 def in_lock(key, ttl: 1.minute, retries: 10, sleep_sec: 0.01.seconds) raise ArgumentError, 'Key needs to be specified' unless key Gitlab::Instrumentation::ExclusiveLock.increment_requested_count lease = SleepingLock.new(key, timeout: ttl, delay: sleep_sec) with_instrumentation(:wait) do lease.obtain(1 + retries) end with_instrumentation(:hold) do yield(lease.retried?, lease) end ensure lease&.cancel end |