class MemoryLimit

Attributes

limit[R]

Public Class Methods

new(bytes = 256 * 1024 ** 2) click to toggle source
# File lib/unicorn-cuba-base/memory_limit.rb, line 30
def initialize(bytes = 256 * 1024 ** 2)
        log.debug "setting up new memory limit of #{bytes} bytes" if bytes
        @limit = bytes
end

Public Instance Methods

borrow(bytes, reason = nil) click to toggle source
# File lib/unicorn-cuba-base/memory_limit.rb, line 43
def borrow(bytes, reason = nil)
        if reason
                log.debug "borrowing #{bytes} from #{@limit} bytes of limit for #{reason}"
        else
                log.debug "borrowing #{bytes} from #{@limit} bytes of limit"
        end
        
        bytes > @limit and raise MemoryLimitedExceededError
        @limit -= bytes
        bytes
end
get(reason = nil) { |limit| ... } click to toggle source
# File lib/unicorn-cuba-base/memory_limit.rb, line 37
def get(reason = nil)
        yield(@limit).tap do |data|
                borrow(data.bytesize, reason) if data
        end
end
io(io) click to toggle source
# File lib/unicorn-cuba-base/memory_limit.rb, line 65
def io(io)
        io.extend MemoryLimit::IO
        io.root_limit self
        io
end
return(bytes, reason = nil) click to toggle source
# File lib/unicorn-cuba-base/memory_limit.rb, line 55
def return(bytes, reason = nil)
        if reason
                log.debug "returning #{bytes} to #{@limit} bytes of limit used for #{reason}"
        else
                log.debug "returning #{bytes} to #{@limit} bytes of limit"
        end
        @limit += bytes
        bytes
end