lpxc 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/lpxc.rb +48 -8
  2. metadata +8 -6
  3. checksums.yaml +0 -7
@@ -28,6 +28,27 @@ class LogMsgQueue
28
28
  end
29
29
  end
30
30
 
31
+ # Like SizedQueue, but drops instead of blocking. Pass one of these in
32
+ # as :request_queue if you would prefer loss to slowing down in cases
33
+ # of back-pressure.
34
+ class DroppingSizedQueue < SizedQueue
35
+ # Returns true/false depending on whether the queue is full or not
36
+ def push(obj)
37
+ @mutex.synchronize do
38
+ return false unless @que.length < @max
39
+
40
+ @que.push obj
41
+ begin
42
+ t = @waiting.shift
43
+ t.wakeup if t
44
+ rescue ThreadError
45
+ retry
46
+ end
47
+ return true
48
+ end
49
+ end
50
+ end
51
+
31
52
  class Lpxc
32
53
 
33
54
  #After parsing opts and initializing defaults, the initializer
@@ -59,8 +80,9 @@ class Lpxc
59
80
  @conn_timeout = opts[:conn_timeout] || 2
60
81
  @batch_size = opts[:batch_size] || 300
61
82
  @flush_interval = opts[:flush_interval] || 0.5
62
- @logplex_url = URI(opts[:logplex_url] || ENV["LOGPLEX_URL"] ||
63
- raise("Must set logplex url."))
83
+ @user_agent = opts[:user_agent] || ENV["LPXC_USER_AGENT"] || "Lpxc (Ruby #{RUBY_VERSION})"
84
+ @logplex_url = URI((opts[:logplex_url] || ENV["LOGPLEX_URL"] ||
85
+ raise("Must set logplex url.")).to_s)
64
86
 
65
87
  #Keep track of the number of requests that the outlet
66
88
  #is processing. This value is used by the wait function.
@@ -71,7 +93,20 @@ class Lpxc
71
93
 
72
94
  #Start the processing threads.
73
95
  Thread.new {outlet}
74
- Thread.new {delay_flush} if opts[:disable_delay_flush].nil?
96
+ Thread.new {delay_flush} unless opts[:disable_delay_flush]
97
+ at_exit {wait} unless opts[:disable_at_exit_flush]
98
+ end
99
+
100
+ #Automatically create an Lpxc client object for a given URL if none exists,
101
+ #and use it to send msg using the token from the URL.
102
+ def self.puts(msg, url, opts={})
103
+ @lock = Mutex.new
104
+ url = url.is_a?(URI) ? url : URI(url)
105
+ server = [url.host, url.port, url.scheme]
106
+ @clients ||= {}
107
+ opts[:logplex_url] = url
108
+ c = @lock.synchronize { @clients[server] ||= Lpxc.new(opts) }
109
+ c.puts(msg, url.password)
75
110
  end
76
111
 
77
112
  #The interface to publish logs into the stream.
@@ -92,11 +127,15 @@ class Lpxc
92
127
  #Wait until all of the data has been cleared from memory.
93
128
  #This is useful if you don't want your program to exit before
94
129
  #we are able to deliver log messages to logplex.
95
- def wait
96
- sleep(0.1) until
97
- @hash.length.zero? &&
98
- @request_queue.empty? &&
99
- @req_in_flight.zero?
130
+ def wait(max=10)
131
+ Timeout.timeout(max) do
132
+ sleep(0.1) until
133
+ @hash.length.zero? &&
134
+ @request_queue.empty? &&
135
+ @req_in_flight.zero?
136
+ end
137
+ true
138
+ rescue Timeout::Error => _
100
139
  end
101
140
 
102
141
  # Empty all log messages into a request queue. Messages will be grouped
@@ -131,6 +170,7 @@ class Lpxc
131
170
  req = Net::HTTP::Post.new(@logplex_url.path)
132
171
  req.basic_auth("token", tok)
133
172
  req.add_field('Content-Type', 'application/logplex-1')
173
+ req.add_field('User-Agent', @user_agent) if @user_agent
134
174
  req.body = body
135
175
  @request_queue.enq(req)
136
176
  @last_flush = Time.now
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lpxc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Ryan Smith (♠ ace hacker)
@@ -20,25 +21,26 @@ files:
20
21
  homepage: http://github.com/ryandotsmith/lpxc
21
22
  licenses:
22
23
  - MIT
23
- metadata: {}
24
24
  post_install_message:
25
25
  rdoc_options: []
26
26
  require_paths:
27
27
  - lib
28
28
  required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
29
30
  requirements:
30
- - - '>='
31
+ - - ! '>='
31
32
  - !ruby/object:Gem::Version
32
33
  version: '0'
33
34
  required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
34
36
  requirements:
35
- - - '>='
37
+ - - ! '>='
36
38
  - !ruby/object:Gem::Version
37
39
  version: '0'
38
40
  requirements: []
39
41
  rubyforge_project:
40
- rubygems_version: 2.0.3
42
+ rubygems_version: 1.8.23
41
43
  signing_key:
42
- specification_version: 4
44
+ specification_version: 3
43
45
  summary: Ruby client for logplex.
44
46
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: a9a59ea66505168840a485b178c9f805df781f4b
4
- data.tar.gz: 1181f1e2976a3a6a0fccf4e88a006c66bca90815
5
- SHA512:
6
- metadata.gz: 2c7f0c812aa0f867262d7e178bafa9e804704d0493d5951b65334d479253c3566162cbde8a6ebd9c82f4534e0b650d5780d7bee6b721aee27d57b48d411d02ff
7
- data.tar.gz: ef326d1790d550779e7c6e86851228d0dc16fc1da8e87b735ce7a05dab91154ba99291184212c2134b2a6a644f39f983fc757376a47c5f34ab4b2ce7f3362dde