Strict 0.5s time budget for completion requests

Vim still loves to block the main GUI thread on occasion when asking for
completions... to counteract this stupidity, we enforce a hard budget of 0.5s
for all completion requests. If the server doesn't respond by then (it should,
unless something really bad happened), we give up.
This commit is contained in:
Strahinja Val Markovic 2013-10-14 11:08:15 -07:00
parent 6331df95cc
commit c3fcaf2b29
2 changed files with 17 additions and 9 deletions

View File

@ -48,19 +48,25 @@ class BaseRequest( object ):
# This is the blocking version of the method. See below for async. # This is the blocking version of the method. See below for async.
# |timeout| is num seconds to tolerate no response from server before giving
# up; see Requests docs for details (we just pass the param along).
@staticmethod @staticmethod
def PostDataToHandler( data, handler ): def PostDataToHandler( data, handler, timeout = None ):
return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data, return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data,
handler ) ) handler,
timeout ) )
# This returns a future! Use JsonFromFuture to get the value. # This returns a future! Use JsonFromFuture to get the value.
# |timeout| is num seconds to tolerate no response from server before giving
# up; see Requests docs for details (we just pass the param along).
@staticmethod @staticmethod
def PostDataToHandlerAsync( data, handler ): def PostDataToHandlerAsync( data, handler, timeout = None ):
def PostData( data, handler ): def PostData( data, handler, timeout ):
return BaseRequest.session.post( _BuildUri( handler ), return BaseRequest.session.post( _BuildUri( handler ),
data = json.dumps( data ), data = json.dumps( data ),
headers = HEADERS ) headers = HEADERS,
timeout = timeout )
@retries( 3, delay = 0.5 ) @retries( 3, delay = 0.5 )
def DelayedPostData( data, handler ): def DelayedPostData( data, handler ):
@ -71,7 +77,7 @@ class BaseRequest( object ):
if not _CheckServerIsHealthyWithCache(): if not _CheckServerIsHealthyWithCache():
return EXECUTOR.submit( DelayedPostData, data, handler ) return EXECUTOR.submit( DelayedPostData, data, handler )
return PostData( data, handler ) return PostData( data, handler, timeout )
session = FuturesSession( executor = EXECUTOR ) session = FuturesSession( executor = EXECUTOR )

View File

@ -20,8 +20,9 @@
from ycm import base from ycm import base
from ycm import vimsupport from ycm import vimsupport
from ycm.client.base_request import ( BaseRequest, BuildRequestData, from ycm.client.base_request import ( BaseRequest, BuildRequestData,
JsonFromFuture ) JsonFromFuture )
TIMEOUT_SECONDS = 0.5
class CompletionRequest( BaseRequest ): class CompletionRequest( BaseRequest ):
def __init__( self, force_semantic = False ): def __init__( self, force_semantic = False ):
@ -42,7 +43,8 @@ class CompletionRequest( BaseRequest ):
def Start( self, query ): def Start( self, query ):
self.request_data[ 'query' ] = query self.request_data[ 'query' ] = query
self._response_future = self.PostDataToHandlerAsync( self.request_data, self._response_future = self.PostDataToHandlerAsync( self.request_data,
'completions' ) 'completions',
TIMEOUT_SECONDS )
def Done( self ): def Done( self ):