Class: OsVm::PortReservation

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/osvm/port_reservation.rb

Overview

Singleton class handling port reservations

Constant Summary collapse

PORT_RANGE =
(10_000..30_000)

Instance Method Summary collapse

Constructor Details

#initialize ⇒ PortReservation

Returns a new instance of PortReservation.



19
20
21
22
23
24
25
26
# File 'lib/osvm/port_reservation.rb', line 19

def initialize
  ports = PORT_RANGE.to_a
  @ports = ports.rotate(rand(ports.length))
  @allocations = {}
  @port_locks = {}
  @scoped = false
  @mutex = Mutex.new
end

Instance Method Details

#acquire_port_lock(port) ⇒ Object (protected)



127
128
129
130
131
132
133
134
135
# File 'lib/osvm/port_reservation.rb', line 127

def acquire_port_lock(port)
  FileUtils.mkdir_p(lock_directory, mode: 0o700)
  lock = File.open(File.join(lock_directory, port.to_s), File::RDWR | File::CREAT, 0o600)

  return lock if lock.flock(File::LOCK_EX | File::LOCK_NB)

  lock.close
  nil
end

#allocate_port ⇒ Object (protected)



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/osvm/port_reservation.rb', line 102

def allocate_port
  if @scoped
    port = @ports.shift
    raise 'No ports available for reservation' if port.nil?

    return port
  end

  @ports.each_with_index do |candidate, index|
    lock = acquire_port_lock(candidate)
    next if lock.nil?

    @ports.delete_at(index)
    @port_locks[candidate] = lock
    return candidate
  end

  raise 'No ports available for reservation'
end

#get_port(key:) ⇒ Integer

Parameters:

  • key (any)

Returns:

  • (Integer)


30
31
32
33
34
35
36
37
38
39
# File 'lib/osvm/port_reservation.rb', line 30

def get_port(key:)
  @mutex.synchronize do
    alloc = @allocations[key]
    next(alloc.first) if alloc

    port = allocate_port
    @allocations[key] = [port]
    port
  end
end

#get_ports(key:, size:) ⇒ Array<Integer>

Parameters:

  • key (any)
  • size (Integer) —

    number of ports

Returns:

  • (Array<Integer>)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/osvm/port_reservation.rb', line 50

def get_ports(key:, size:)
  @mutex.synchronize do
    alloc = @allocations[key]
    next(alloc) if alloc

    ports = []

    begin
      size.times { ports << allocate_port }
    rescue StandardError
      ports.each { |port| release_allocated_port(port) }
      raise
    end

    @allocations[key] = ports
    ports
  end
end

#lock_directory ⇒ Object (protected)



137
138
139
140
# File 'lib/osvm/port_reservation.rb', line 137

def lock_directory
  root = ENV.fetch('OSVM_PORT_RESERVATION_ROOT', '/var/tmp')
  File.join(root, "osvm-port-reservations-#{Process.uid}")
end

#release_allocated_port(port) ⇒ Object (protected)



122
123
124
125
# File 'lib/osvm/port_reservation.rb', line 122

def release_allocated_port(port)
  @port_locks.delete(port)&.close
  @ports << port
end

#release_port(key:) ⇒ Object

Parameters:

  • key (any)
  • port (Integer)


43
44
45
# File 'lib/osvm/port_reservation.rb', line 43

def release_port(key:)
  release_ports(key:)
end

#release_ports(key:) ⇒ Object

Parameters:

  • key (any)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/osvm/port_reservation.rb', line 70

def release_ports(key:)
  @mutex.synchronize do
    alloc = @allocations[key]
    next if alloc.nil?

    @allocations.delete(key)
    alloc.each { |port| release_allocated_port(port) }
  end

  nil
end

#reset_to_ports(ports) ⇒ Object

Reset this class and scope available ports to ports

This is useful when forking processes: the parent process calls #get_ports, forks and the child process can be scoped to the reservation using this method.

Parameters:

  • ports (Array<Integer>)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osvm/port_reservation.rb', line 88

def reset_to_ports(ports)
  @mutex.synchronize do
    @port_locks.each_value(&:close)
    @ports = ports.dup
    @allocations = {}
    @port_locks = {}
    @scoped = true
  end

  nil
end