Class: LogCourier::ClientTcp

Inherits:
Object
  • Object
show all
Defined in:
lib/log-courier/client_tcp.rb

Overview

TLS transport implementation

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ClientTcp

Returns a new instance of ClientTcp.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/log-courier/client_tcp.rb', line 24

def initialize(options = {})
  @options = {
    logger: nil,
    transport: 'tls',
    ssl_ca: nil,
    ssl_certificate: nil,
    ssl_key: nil,
    ssl_key_passphrase: nil,
    min_tls_version: 1.2,
    disable_handshake: false,
  }.merge!(options)

  @logger = @options[:logger]

  raise "output/courier: 'port' is required" if @options[:port].nil?

  return unless @options[:transport] == 'tls'

  raise "output/courier: 'ssl_ca' is required if 'transport' is 'tls'" if @options[:ssl_ca].nil?

  c = 0
  [:ssl_certificate, :ssl_key].each do
    c += 1
  end
  raise 'output/courier: \'ssl_certificate\' and \'ssl_key\' must be specified together' if c == 1
end

Instance Method Details

#connect(io_control) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/log-courier/client_tcp.rb', line 51

def connect(io_control)
  loop do
    begin
      if tls_connect
        return unless handshake(io_control)

        break
      end
    rescue ShutdownSignal
      return
    end

    # TODO: Make this configurable
    sleep 5
  end

  @send_q = SizedQueue.new 1
  @send_paused = false

  @send_thread = Thread.new do
    run_send io_control
  rescue ShutdownSignal
    # Shutdown
  rescue StandardError => e
    @logger&.warn e, hint: 'Unknown write error'
    io_control << ['F']
  end
  @recv_thread = Thread.new do
    run_recv io_control
  rescue ShutdownSignal
    # Shutdown
  rescue StandardError => e
    @logger&.warn e, hint: 'Unknown read error'
    io_control << ['F']
  end
  nil
end

#disconnectObject



89
90
91
92
93
94
95
# File 'lib/log-courier/client_tcp.rb', line 89

def disconnect
  @send_thread&.raise ShutdownSignal
  @send_thread&.join
  @recv_thread&.raise ShutdownSignal
  @recv_thread&.join
  nil
end

#pause_sendObject



103
104
105
106
107
108
109
# File 'lib/log-courier/client_tcp.rb', line 103

def pause_send
  return if @send_paused

  @send_paused = true
  @send_q << nil
  nil
end

#resume_sendObject



115
116
117
118
119
120
121
# File 'lib/log-courier/client_tcp.rb', line 115

def resume_send
  if @send_paused
    @send_paused = false
    @send_q << nil
  end
  nil
end

#send(signature, message) ⇒ Object



97
98
99
100
101
# File 'lib/log-courier/client_tcp.rb', line 97

def send(signature, message)
  # Add to send queue
  @send_q << ([signature, message.length].pack('A4N') + message)
  nil
end

#send_paused?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/log-courier/client_tcp.rb', line 111

def send_paused?
  @send_paused
end