Class: Tapsoob::Chunksize

Inherits:
Object
  • Object
show all
Defined in:
lib/tapsoob/chunksize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunksize) ⇒ Chunksize

Returns a new instance of Chunksize.



8
9
10
11
12
# File 'lib/tapsoob/chunksize.rb', line 8

def initialize(chunksize)
  @chunksize = chunksize
  @idle_secs = 0.0
  @retries = 0
end

Instance Attribute Details

#chunksizeObject (readonly)

Returns the value of attribute chunksize.



6
7
8
# File 'lib/tapsoob/chunksize.rb', line 6

def chunksize
  @chunksize
end

#end_timeObject

Returns the value of attribute end_time.



5
6
7
# File 'lib/tapsoob/chunksize.rb', line 5

def end_time
  @end_time
end

#idle_secsObject

Returns the value of attribute idle_secs.



5
6
7
# File 'lib/tapsoob/chunksize.rb', line 5

def idle_secs
  @idle_secs
end

#retriesObject

Returns the value of attribute retries.



5
6
7
# File 'lib/tapsoob/chunksize.rb', line 5

def retries
  @retries
end

#start_timeObject

Returns the value of attribute start_time.



5
6
7
# File 'lib/tapsoob/chunksize.rb', line 5

def start_time
  @start_time
end

#time_in_dbObject

Returns the value of attribute time_in_db.



5
6
7
# File 'lib/tapsoob/chunksize.rb', line 5

def time_in_db
  @time_in_db
end

Instance Method Details

#calc_new_chunksizeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tapsoob/chunksize.rb', line 38

def calc_new_chunksize
  new_chunksize = if retries > 0
    chunksize
  elsif diff > 3.0
    (chunksize / 3).ceil
  elsif diff > 1.1
    chunksize - 100
  elsif diff < 0.8
    chunksize * 2
  else
    chunksize + 100
  end
  new_chunksize = 1 if new_chunksize < 1
  new_chunksize
end

#diffObject



22
23
24
# File 'lib/tapsoob/chunksize.rb', line 22

def diff
  end_time - start_time - time_in_db - idle_secs
end

#reset_chunksizeObject



18
19
20
# File 'lib/tapsoob/chunksize.rb', line 18

def reset_chunksize
  @chunksize = (retries <= 1) ? 10 : 1
end

#time_deltaObject



31
32
33
34
35
36
# File 'lib/tapsoob/chunksize.rb', line 31

def time_delta
  t1 = Time.now
  yield if block_given?
  t2 = Time.now
  t2 - t1
end

#to_iObject



14
15
16
# File 'lib/tapsoob/chunksize.rb', line 14

def to_i
  chunksize
end