Class: Gitlab::ReferenceCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/reference_counter.rb

Overview

Reference Counter

A reference counter is used as a mechanism to identify when a repository is being accessed by a writable operation.

Maintenance operations would use this as a clue to when it should execute significant changes in order to avoid disrupting running traffic

Constant Summary collapse

REFERENCE_EXPIRE_TIME =
600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gl_repository) ⇒ ReferenceCounter

Reference Counter instance

Examples:

Gitlab::ReferenceCounter.new('project-1')

Parameters:

  • gl_repository (String)

    repository identifier

See Also:

  • GlRepository::RepoType.identifier_for_repositorable

23
24
25
26
# File 'lib/gitlab/reference_counter.rb', line 23

def initialize(gl_repository)
  @gl_repository = gl_repository
  @key = "git-receive-pack-reference-counter:#{gl_repository}"
end

Instance Attribute Details

#gl_repositoryObject (readonly)

Returns the value of attribute gl_repository


14
15
16
# File 'lib/gitlab/reference_counter.rb', line 14

def gl_repository
  @gl_repository
end

#keyObject (readonly)

Returns the value of attribute key


14
15
16
# File 'lib/gitlab/reference_counter.rb', line 14

def key
  @key
end

Instance Method Details

#decreaseBoolean

Decrease the counter

Returns:

  • (Boolean)

    whether operation was a success


50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/reference_counter.rb', line 50

def decrease
  redis_cmd do |redis|
    current_value = redis.decr(key)
    if current_value < 0
      Gitlab::AppLogger.warn("Reference counter for #{gl_repository} decreased" \
        " when its value was less than 1. Resetting the counter.")
      redis.del(key)
    end
  end
end

#expires_inInteger

When the reference counter would expire

Returns:

  • (Integer)

    Number in seconds until expiration or false if never


75
76
77
78
79
# File 'lib/gitlab/reference_counter.rb', line 75

def expires_in
  Gitlab::Redis::SharedState.with do |redis|
    redis.ttl(key)
  end
end

#increaseBoolean

Increase the counter

Returns:

  • (Boolean)

    whether operation was a success


40
41
42
43
44
45
# File 'lib/gitlab/reference_counter.rb', line 40

def increase
  redis_cmd do |redis|
    redis.incr(key)
    redis.expire(key, REFERENCE_EXPIRE_TIME)
  end
end

#reset!Boolean

Reset the reference counter

Returns:

  • (Boolean)

    whether reset was a success


65
66
67
68
69
# File 'lib/gitlab/reference_counter.rb', line 65

def reset!
  redis_cmd do |redis|
    redis.del(key)
  end
end

#valueInteger

Return the actual counter value

Returns:

  • (Integer)

    value


31
32
33
34
35
# File 'lib/gitlab/reference_counter.rb', line 31

def value
  Gitlab::Redis::SharedState.with do |redis|
    (redis.get(key) || 0).to_i
  end
end