Module: UnionStationHooks::Utils

Extended by:
Utils
Included in:
Context, Utils
Defined in:
lib/union_station_hooks_core/utils.rb

Overview

Various utility methods.

Defined Under Namespace

Classes: ProcessTimes

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/union_station_hooks_core/utils.rb', line 33

def self.included(klass)
  # When included into another class, make sure that Utils
  # methods are made private.
  public_instance_methods(false).each do |method_name|
    klass.send(:private, method_name)
  end
end

Instance Method Details

#base64(data) ⇒ Object

Base64-encodes the given data. Newlines are removed. This is like ‘Base64.strict_encode64`, but also works on Ruby 1.8 which doesn’t have that method.



121
122
123
# File 'lib/union_station_hooks_core/utils.rb', line 121

def base64(data)
  Base64.strict_encode64(data)
end

#connect_to_server(address) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/union_station_hooks_core/utils.rb', line 67

def connect_to_server(address)
  case get_socket_address_type(address)
  when :unix
    UNIXSocket.new(address.sub(/^unix:/, ''))
  when :tcp
    host, port = address.sub(%r{^tcp://}, '').split(':', 2)
    port = port.to_i
    TCPSocket.new(host, port)
  else
    raise ArgumentError, "Unknown socket address type for '#{address}'."
  end
end

#encoded_timestampObject



107
108
109
110
111
# File 'lib/union_station_hooks_core/utils.rb', line 107

def encoded_timestamp
  time = Time.now
  timestamp = time.to_i * 1_000_000 + time.usec
  timestamp.to_s(36)
end

#get_socket_address_type(address) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/union_station_hooks_core/utils.rb', line 57

def get_socket_address_type(address)
  if address =~ %r{^unix:.}
    :unix
  elsif address =~ %r{^tcp://.}
    :tcp
  else
    :unknown
  end
end

#local_socket_address?(address) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/union_station_hooks_core/utils.rb', line 80

def local_socket_address?(address)
  case get_socket_address_type(address)
  when :unix
    return true
  when :tcp
    host, _port = address.sub(%r{^tcp://}, '').split(':', 2)
    host == '127.0.0.1' || host == '::1' || host == 'localhost'
  else
    raise ArgumentError, "Unknown socket address type for '#{address}'."
  end
end

#monotime_usec_from_time(time = Time.now) ⇒ Object



103
104
105
# File 'lib/union_station_hooks_core/utils.rb', line 103

def monotime_usec_from_time(time = Time.now)
  timestamp = time.to_i * 1_000_000 + time.usec - UnionStationHooks.get_delta_monotonic
end

#monotime_usec_nowObject

Workaround for approximating the monotonic clock



98
99
100
# File 'lib/union_station_hooks_core/utils.rb', line 98

def monotime_usec_now
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond)
end

#process_timesObject



164
165
166
# File 'lib/union_station_hooks_core/utils.rb', line 164

def process_times
  PhusionPassenger::NativeSupport.process_times
end

#process_ust_router_reply(channel, error_description, error_class = RuntimeError, unexpected_error_class = RuntimeError) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/union_station_hooks_core/utils.rb', line 128

def process_ust_router_reply(channel, error_description,
                             error_class = RuntimeError,
                             unexpected_error_class = RuntimeError)
  result = channel.read
  if result.nil?
    raise unexpected_error_class,
      "#{error_description}: UstRouter did not send a reply"
  end
  process_ust_router_reply_message(result, error_description,
    error_class, unexpected_error_class)
  result
end

#process_ust_router_reply_message(message, error_description, error_class = RuntimeError, unexpected_error_class = RuntimeError) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/union_station_hooks_core/utils.rb', line 141

def process_ust_router_reply_message(message, error_description,
                                     error_class = RuntimeError,
                                     unexpected_error_class = RuntimeError)
  if message[0] != 'status'
    raise unexpected_error_class,
      "#{error_description}: expected UstRouter to respond with " \
      "'status', but got #{message.inspect} instead"
  end

  if message[1] == 'error'
    if message[2]
      raise error_class, "#{error_description}: #{message[2]}"
    else
      raise error_class, "#{error_description} (no server message given)"
    end
  elsif message[1] != 'ok'
    raise unexpected_error_class,
      "#{error_description}: expected UstRouter to respond with " \
      "'ok' or 'error', but got #{message.inspect} instead"
  end
end

#require_key(options, key) ⇒ Object



41
42
43
44
45
# File 'lib/union_station_hooks_core/utils.rb', line 41

def require_key(options, key)
  if !options.key?(key)
    raise ArgumentError, "Option #{key.inspect} is required"
  end
end

#require_non_empty_key(options, key) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/union_station_hooks_core/utils.rb', line 47

def require_non_empty_key(options, key)
  value = options[key]
  if value.nil? || value.empty?
    raise ArgumentError, "Option #{key.inspect} is required " \
      'and must be non-empty'
  else
    value
  end
end