corn 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51b599d133e23dba5f4266e8fa347c1792f024aa
4
- data.tar.gz: a33e7f54214b4506cb5887ba1403a4eca2645165
3
+ metadata.gz: a48acabdb1beaeceb6915bca571c759409f21812
4
+ data.tar.gz: f046e870baf89b719f4c4de7865ad8212f25e82f
5
5
  SHA512:
6
- metadata.gz: 01206afb702625ba0bd4afce972670b04e92a36c4ecb712593155e53901b651fbc1d67e12b2828a359db571532dc4c42994e04ff80864d2e606dc73258057e20
7
- data.tar.gz: 6b53aeb5b82e2fd905df1257662403f7c8a381d55186a6371c4ff05e0ef4186eedbef498aa2999687e8df19058066e7ebf12ac6431b498ef2962e4ad808d2d21
6
+ metadata.gz: 70b0f65086ebb697d15394f7c85df9e2b198c1d38ca4379dd5b79ee2c72894c67243f0feea02672db994f05734e37ef9ecaf1a12f77c5b3e4366623afa2488cf
7
+ data.tar.gz: b8a35e2f0d5c829b51db9f0b8cdcb2b0d98f0839d988447d557dc98f440238718f6d9976ff6fe12eb4a587cbc66b41d9d29850fc02555e88dd3b240eacf4ce72
data/lib/corn/config.rb CHANGED
@@ -19,7 +19,8 @@ module Corn
19
19
  q = !!value == value ? '?' : ''
20
20
  self.class_eval <<-RUBY, __FILE__, __LINE__
21
21
  def self.#{key}#{q}
22
- @config[:#{key}]
22
+ r = @config[:#{key}]
23
+ r.is_a?(Proc) ? r.call : r
23
24
  end
24
25
  RUBY
25
26
  end
data/lib/corn/post.rb CHANGED
@@ -15,8 +15,8 @@ module Corn
15
15
  end
16
16
 
17
17
  def start_post_thread(interval)
18
- if interval < 1
19
- Corn.logger.info("Corn post interval < 1 sec, change it to 1 sec")
18
+ if interval <= 0
19
+ Corn.logger.info("Corn post interval <= 0 sec, change it to 1 sec")
20
20
  interval = 1
21
21
  else
22
22
  Corn.logger.info("Corn post interval #{interval} sec(s)")
@@ -1,3 +1,4 @@
1
+ require 'corn/config'
1
2
  require 'corn/post'
2
3
  require 'corn/rack/request_env'
3
4
  require 'sampling_prof'
@@ -5,21 +6,27 @@ require 'sampling_prof'
5
6
  module Corn
6
7
  module Rack
7
8
  class SlowRequestProfiler
9
+ include Config
10
+ config(:profiling => true,
11
+ :slow_request_threshold => 5,
12
+ :sampling_interval => 0.1,
13
+ :post_interval => 2)
14
+
8
15
  class ProfilingApp
9
- def initialize(app,
10
- slow_request_threshold=5,
11
- sampling_interval=0.1,
12
- post_interval=2)
16
+ def initialize(app, config)
13
17
  @app = app
14
- @slow_request_threshold = slow_request_threshold
15
- @sampling_interval = sampling_interval
16
- @post = Post.new(post_interval)
17
- @prof = SamplingProf.new(@sampling_interval)
18
+ @config = config
19
+ @post = Post.new(config.post_interval)
20
+ @prof = SamplingProf.new(config.sampling_interval)
18
21
  at_exit { terminate }
19
22
  end
20
23
 
21
24
  def call(env)
22
- @prof.profile(output_handler(env)) { @app.call(env) }
25
+ if @config.profiling?
26
+ @prof.profile(output_handler(env)) { @app.call(env) }
27
+ else
28
+ @app.call(env)
29
+ end
23
30
  end
24
31
 
25
32
  def terminate
@@ -30,15 +37,15 @@ module Corn
30
37
  def output_handler(env)
31
38
  request_env = RequestEnv.new(env)
32
39
  lambda do |data|
33
- if request_env.time > @slow_request_threshold
40
+ if request_env.time > @config.slow_request_threshold
34
41
  @post.enqueue(request_env.to_report.merge("data" => data))
35
42
  end
36
43
  end
37
44
  end
38
45
  end
39
46
 
40
- def initialize(app, *args)
41
- @app = Corn.configured? ? ProfilingApp.new(app, *args) : app
47
+ def initialize(app)
48
+ @app = Corn.configured? ? ProfilingApp.new(app, self.class) : app
42
49
  end
43
50
 
44
51
  def call(env)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Li