Prevent requesting more than 3 edit predictions per second (#24203)

Release Notes:

- N/A

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Antonio Scandurra 2025-02-04 18:07:24 +01:00 committed by GitHub
parent cae712e740
commit aea36f0eff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1250,10 +1250,11 @@ pub struct ZetaInlineCompletionProvider {
current_completion: Option<CurrentInlineCompletion>, current_completion: Option<CurrentInlineCompletion>,
/// None if this is entirely disabled for this provider /// None if this is entirely disabled for this provider
provider_data_collection: ProviderDataCollection, provider_data_collection: ProviderDataCollection,
last_request_timestamp: Instant,
} }
impl ZetaInlineCompletionProvider { impl ZetaInlineCompletionProvider {
pub const DEBOUNCE_TIMEOUT: Duration = Duration::from_millis(8); pub const THROTTLE_TIMEOUT: Duration = Duration::from_millis(300);
pub fn new(zeta: Entity<Zeta>, provider_data_collection: ProviderDataCollection) -> Self { pub fn new(zeta: Entity<Zeta>, provider_data_collection: ProviderDataCollection) -> Self {
Self { Self {
@ -1262,6 +1263,7 @@ impl ZetaInlineCompletionProvider {
next_pending_completion_id: 0, next_pending_completion_id: 0,
current_completion: None, current_completion: None,
provider_data_collection, provider_data_collection,
last_request_timestamp: Instant::now(),
} }
} }
} }
@ -1327,7 +1329,7 @@ impl inline_completion::InlineCompletionProvider for ZetaInlineCompletionProvide
&mut self, &mut self,
buffer: Entity<Buffer>, buffer: Entity<Buffer>,
position: language::Anchor, position: language::Anchor,
debounce: bool, _debounce: bool,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
if !self.zeta.read(cx).tos_accepted { if !self.zeta.read(cx).tos_accepted {
@ -1349,13 +1351,17 @@ impl inline_completion::InlineCompletionProvider for ZetaInlineCompletionProvide
self.next_pending_completion_id += 1; self.next_pending_completion_id += 1;
let data_collection_permission = let data_collection_permission =
self.provider_data_collection.data_collection_permission(cx); self.provider_data_collection.data_collection_permission(cx);
let last_request_timestamp = self.last_request_timestamp;
let task = cx.spawn(|this, mut cx| async move { let task = cx.spawn(|this, mut cx| async move {
if debounce { if let Some(timeout) = (last_request_timestamp + Self::THROTTLE_TIMEOUT)
cx.background_executor().timer(Self::DEBOUNCE_TIMEOUT).await; .checked_duration_since(Instant::now())
{
cx.background_executor().timer(timeout).await;
} }
let completion_request = this.update(&mut cx, |this, cx| { let completion_request = this.update(&mut cx, |this, cx| {
this.last_request_timestamp = Instant::now();
this.zeta.update(cx, |zeta, cx| { this.zeta.update(cx, |zeta, cx| {
zeta.request_completion(&buffer, position, data_collection_permission, cx) zeta.request_completion(&buffer, position, data_collection_permission, cx)
}) })