Class: LogCourier::Client

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

Overview

Implementation of a single client connection

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/log-courier/client.rb', line 95

def initialize(options = {})
  @options = {
    logger:       nil,
    transport:    'tls',
    spool_size:   1024,
    idle_timeout: 5,
    port:         nil,
    addresses:    [],
  }.merge!(options)

  @logger = @options[:logger]
  @logger['plugin'] = 'output/courier' unless @logger.nil?

  case @options[:transport]
  when 'tcp', 'tls'
    require 'log-courier/client_tcp'
    @client = ClientTcp.new(@options)
  else
    fail 'output/courier: \'transport\' must be tcp or tls'
  end

  fail 'output/courier: \'addresses\' must contain at least one address' if @options[:addresses].empty?
  fail 'output/courier: \'addresses\' only supports a single address at this time' if @options[:addresses].length > 1

  @event_queue = EventQueue.new @options[:spool_size]
  @pending_payloads = {}
  @first_payload = nil
  @last_payload = nil

  # Start the spooler which will collect events into chunks
  @send_ready = false
  @send_mutex = Mutex.new
  @send_cond = ConditionVariable.new
  @spooler_thread = Thread.new do
    run_spooler
  end

  # TODO: Make these configurable?
  @keepalive_timeout = 1800
  @network_timeout = 30

  # TODO: Make pending payload max configurable?
  @max_pending_payloads = 100

  @retry_payload = nil
  @received_payloads = Queue.new

  @pending_ping = false

  # Start the IO thread
  @io_control = EventQueue.new 1
  @io_thread = Thread.new do
    run_io
  end
end

Instance Method Details

#publish(event) ⇒ Object



151
152
153
154
155
# File 'lib/log-courier/client.rb', line 151

def publish(event)
  # Pass the event into the spooler
  @event_queue << event
  return
end

#shutdown(force = false) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/log-courier/client.rb', line 157

def shutdown(force=false)
  if force
    # Raise a shutdown signal in the spooler and wait for it
    @spooler_thread.raise ShutdownSignal
    @spooler_thread.join
    @io_thread.raise ShutdownSignal
  else
    @event_queue.push nil
    @spooler_thread.join
    @io_control << ['!', nil]
  end
  @io_thread.join
  return @pending_payloads.length == 0
end