bool GcsThrottle::AdmitRequest() {
mutex_lock l(mu_);
- if (!config_.enabled) return true;
UpdateState();
if (available_tokens_ < config_.tokens_per_request) {
- return false;
+ return false || !config_.enabled;
}
available_tokens_ -= config_.tokens_per_request;
return true;
void GcsThrottle::RecordResponse(size_t num_bytes) {
mutex_lock l(mu_);
- if (!config_.enabled) return;
UpdateState();
available_tokens_ -= request_bytes_to_tokens(num_bytes);
}
* purpose of this function is to make available to monitoring or other
* instrumentation the number of available tokens in the pool.
*/
- inline int64 available_tokens() {
+ inline int64 available_tokens() LOCKS_EXCLUDED(mu_) {
mutex_lock l(mu_);
- if (!config_.enabled) return 0;
UpdateState();
return available_tokens_;
}
+ /**
+ * is_enabled determines if the throttle is enabled.
+ *
+ * If !is_enabled(), AdmitRequest() will always return true.
+ */
+ bool is_enabled() LOCKS_EXCLUDED(mu_) {
+ mutex_lock l(mu_);
+ return config_.enabled;
+ }
+
private:
/**
* UpdateState updates the available_tokens_ and last_updated_secs_ variables.
EXPECT_EQ(200000, throttle_.available_tokens());
}
+TEST(GcsThrottleDisabledTest, Disabled) {
+ TestTime time;
+ GcsThrottle throttle(&time);
+ ASSERT_FALSE(throttle.is_enabled()); // Verify throttle is disabled.
+
+ EXPECT_EQ(0, throttle.available_tokens());
+ time.AdvanceSeconds(1);
+ EXPECT_EQ(100000, throttle.available_tokens());
+ EXPECT_TRUE(throttle.AdmitRequest());
+ EXPECT_EQ(99900, throttle.available_tokens());
+ time.AdvanceSeconds(1);
+ EXPECT_EQ(199900, throttle.available_tokens());
+ throttle.RecordResponse(128000000); // 128 MB response.
+ EXPECT_LT(0, throttle.available_tokens());
+ // Admit request even without available tokens
+ EXPECT_TRUE(throttle.AdmitRequest());
+}
+
} // namespace
} // namespace tensorflow