Class: Flare::Util::Bwlimit

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/flare/util/bwlimit.rb

Overview

Description

Constant Summary collapse

DefaultBandwidth =

1Mbps

1024*1024
Ki =
1024
Mi =
1024*Ki
Gi =
1024*Mi
Bit =
1
Byte =
8

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #error, #fatal, #info, logger, #puts, set_logger, #trace, #warn

Constructor Details

#initialize(bwlimit) ⇒ Bwlimit

Returns a new instance of Bwlimit.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flare/util/bwlimit.rb', line 24

def initialize(bwlimit)
  @limit = Bwlimit.bps(bwlimit)
  @basetime = @starttime = Time.now
  @duration = 1.0
  @history = []
  @minwait = 0.01
  @speed = 0
  @bytes = 0
  @totalbytes = 0
  @thresh = 1*Ki
end

Class Method Details

.bps(bw) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/flare/util/bwlimit.rb', line 103

def self.bps(bw)
  return 0 if bw.nil?
  case bw
  when /^(\d+)$/
    $1.to_i*Bit
  when /^(\d+)B$/
    $1.to_i*Byte
  when /^(\d+)k$/
    $1.to_i*Ki*Bit
  when /^(\d+)kB$/
    $1.to_i*Ki*Byte
  when /^(\d+)M$/
    $1.to_i*Mi*Bit
  when /^(\d+)MB$/
    $1.to_i*Mi*Byte
  when /^(\d+)G$/
    $1.to_i*Gi*Bit
  when /^(\d+)GB$/
    $1.to_i*Gi*Byte
  else
    bw.to_i
  end
end

Instance Method Details

#bpsObject



44
45
46
# File 'lib/flare/util/bwlimit.rb', line 44

def bps
  @limit
end

#historyObject



91
92
93
# File 'lib/flare/util/bwlimit.rb', line 91

def history
  @history.dup
end

#inc(bytes) ⇒ Object



52
53
54
55
# File 'lib/flare/util/bwlimit.rb', line 52

def inc(bytes)
  @bytes += bytes
  @totalbytes += bytes
end

#limitObject



40
41
42
# File 'lib/flare/util/bwlimit.rb', line 40

def limit
  @limit
end

#limit=(bwlimit) ⇒ Object



36
37
38
# File 'lib/flare/util/bwlimit.rb', line 36

def limit=(bwlimit)
  @limit = Bwlimit.bps(bwlimit)
end

#pasttime(now = Time.now) ⇒ Object



70
71
72
# File 'lib/flare/util/bwlimit.rb', line 70

def pasttime(now = Time.now)
  now-@basetime
end

#resetObject



48
49
50
# File 'lib/flare/util/bwlimit.rb', line 48

def reset
  @basetime = @starttime = Time.now
end

#speedObject



95
96
97
# File 'lib/flare/util/bwlimit.rb', line 95

def speed
  @speed
end

#time_to_wait(now = Time.now) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/flare/util/bwlimit.rb', line 57

def time_to_wait(now = Time.now)
  waitsec = 0
  limit = @limit/Byte
  allowed = (now-@basetime)*limit
  if @bytes > allowed
    waitsec = ((@bytes-allowed).to_f/limit)
  end
  unless waitsec > 0
    waitsec = @minwait if waitsec < @minwait
  end
  waitsec
end

#totalbytesObject



99
100
101
# File 'lib/flare/util/bwlimit.rb', line 99

def totalbytes
  @totalbytes
end

#waitObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/flare/util/bwlimit.rb', line 74

def wait
  waitsec = 0
  if @limit > 0 && @bytes > @thresh
    now = Time.now
    sleep time_to_wait(now)
    diff = pasttime(now)
    if diff > @duration
      @speed = @bytes*Byte/diff
      debug "#{@speed} bps"
      @bytes = 0
      @basetime = now
      @history << {:time => now-@starttime, :speed => @speed}
    end
  end
  waitsec
end