Class: Google::Cloud::Debugger::RequestQuotaManager

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/debugger/request_quota_manager.rb

Overview

RequestQuotaManager

Tracking object used by debugger agent to manage quota in request-based applications. This class tracks the amount of time and number of breakpoints to evaluation in a single session.

The debugger agent doesn't have use a quota manager by default, which means it will evaluate all breakpoints encountered and takes as much time as needed. This class is utilized by Middleware class to limit latency overhead when used in Rack-based applications.

Constant Summary collapse

DEFAULT_TIME_QUOTA =

Default Total time allowed to consume, in seconds

0.05
DEFAULT_COUNT_QUOTA =

Default max number of breakpoints to evaluate

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_quota: DEFAULT_TIME_QUOTA, count_quota: DEFAULT_COUNT_QUOTA) ⇒ RequestQuotaManager

Construct a new RequestQuotaManager instance

Parameters:

  • time_quota (Float)

    The max quota for time consumed.

  • count_quota (Integer)

    The max quota for count usage.



60
61
62
63
64
65
66
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 60

def initialize time_quota: DEFAULT_TIME_QUOTA,
               count_quota: DEFAULT_COUNT_QUOTA
  @time_quota = time_quota
  @time_used = 0
  @count_quota = count_quota
  @count_used = 0
end

Instance Attribute Details

#count_quotaObject

The count quota for this manager



45
46
47
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 45

def count_quota
  @count_quota
end

#count_usedObject

The count quota used



53
54
55
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 53

def count_used
  @count_used
end

#time_quotaObject

The time quota for this manager



41
42
43
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 41

def time_quota
  @time_quota
end

#time_usedObject

The time quota used



49
50
51
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 49

def time_used
  @time_used
end

Instance Method Details

#consume(time: 0) ⇒ Object

Notify the quota manager some resource has been consumed. Each time called increases the count quota usage.

Parameters:

  • time (Float)

    Amount of time to deduct from the time quota.



88
89
90
91
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 88

def consume time: 0
  @time_used += time
  @count_used += 1
end

#more?Boolean

Check if there's more quota left.

Returns:

  • (Boolean)

    True if there's more quota; false otherwise.



72
73
74
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 72

def more?
  (time_used < time_quota) && (count_used < count_quota)
end

#resetObject

Reset all the quota usage.



78
79
80
81
# File 'lib/google/cloud/debugger/request_quota_manager.rb', line 78

def reset
  @time_used = 0
  @count_used = 0
end