The pooled service model is identical to the threaded model, with the exception that when a thread terminates, the pooled services it referenced are returned to a pool, which subsequent threads may allocate from. This makes it an excellent choice for services that are expensive to create.

Methods
Public Class methods
new()

Create a new pooled service model.

     # File lib/copland/service-model.rb, line 215
215:     def initialize
216:       @pools = Hash.new { |table,key| table[key] = Array.new }
217:       @mutex = Mutex.new
218:       @waiters = []
219:     end
Public Instance methods
instantiate( service_point )

This will return the requested service instance if it has already been instantiated for the current thread. If it has not already been created for the current thread, then one will be taken from the pool of available services. If the pool is empty, a new one will be created. When creating a new service instance, the service point is wrapped in a DeferringProxy object, to defer the object’s actual creation until the time it is first accessed.

This method is synchronized for thread-safe access.

     # File lib/copland/service-model.rb, line 228
228:     def instantiate( service_point )
229:       @mutex.synchronize do
230:         update_waiters
231:         prepare_thread
232: 
233:         service = Thread.current[ :pooled_services ][ service_point.full_name ]
234: 
235:         unless service
236:           service_pool = @pools[ service_point.full_name ]
237:           if service_pool.length > 0
238:             service = service_pool.pop
239:             service.__notify__( :service_unpooled )
240:           else
241:             service = DeferringProxy.new( service_point )
242:           end
243:           Thread.current[ :pooled_services ][ service_point.full_name ] = service
244:         end
245: 
246:         return service
247:       end
248:     end