Class: RestCore::ThreadPool

Inherits:
Object
  • Object
show all
Includes:
RestCore
Defined in:
lib/rest-core/thread_pool.rb

Defined Under Namespace

Classes: Queue, Task

Constant Summary

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestCore

eagerload, id

Constructor Details

#initialize(client_class) ⇒ ThreadPool

Returns a new instance of ThreadPool.



66
67
68
69
70
71
72
# File 'lib/rest-core/thread_pool.rb', line 66

def initialize client_class
  @client_class = client_class
  @queue        = Queue.new
  @mutex        = Mutex.new
  @workers      = []
  @waiting      = 0
end

Instance Attribute Details

#client_classObject (readonly)

Returns the value of attribute client_class.



64
65
66
# File 'lib/rest-core/thread_pool.rb', line 64

def client_class
  @client_class
end

Class Method Details

.[](client_class) ⇒ Object



60
61
62
# File 'lib/rest-core/thread_pool.rb', line 60

def self.[] client_class
  (@pools ||= {})[client_class] ||= new(client_class)
end

Instance Method Details

#defer(mutex = nil, &job) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/rest-core/thread_pool.rb', line 90

def defer mutex=nil, &job
  mutex.synchronize do
    task = Task.new(job, mutex)
    queue << task
    spawn_worker if waiting == 0 && workers.size < max_size
    task
  end
end

#idle_timeObject



86
87
88
# File 'lib/rest-core/thread_pool.rb', line 86

def idle_time
  client_class.pool_idle_time
end

#inspectObject



74
75
76
# File 'lib/rest-core/thread_pool.rb', line 74

def inspect
  "#<#{self.class.name} client_class=#{client_class}>"
end

#max_sizeObject



82
83
84
# File 'lib/rest-core/thread_pool.rb', line 82

def max_size
  client_class.pool_size
end

#shutdownObject

Block on shutting down, and should not add more jobs while shutting down



104
105
106
107
108
# File 'lib/rest-core/thread_pool.rb', line 104

def shutdown
  workers.size.times{ trim(true) }
  workers.first.join && trim(true) until workers.empty?
  queue.clear
end

#sizeObject



78
79
80
# File 'lib/rest-core/thread_pool.rb', line 78

def size
  workers.size
end

#trim(force = false) ⇒ Object



99
100
101
# File 'lib/rest-core/thread_pool.rb', line 99

def trim force=false
  queue << lambda{ |_| false } if force || waiting > 0
end