Permalink
Browse files

Request Timeouts: Fixup timeout handling.

This introduces a new "attention" event that is emitted if we receive
a done token with the attention flag set.

This cleans up the implementation a bit and actually fixes an issue
where we would switch back into the LoggedInState too early because we
did not actually wait for the attention acknowledgement.
  • Loading branch information...
arthurschreiber committed Jun 5, 2014
1 parent 0b90f6b commit 09fd8a8393169553862899c413cc86d8ff2bee09
Showing with 25 additions and 18 deletions.
  1. +25 −18 src/connection.coffee
View
@@ -135,21 +135,30 @@ class Connection extends EventEmitter
SENT_ATTENTION:
name: 'SentAttention'
+ enter: ->
+ @attentionReceived = false
events:
socketError: (error) ->
@transitionTo(@STATE.FINAL)
data: (data) ->
@sendDataToTokenStreamParser(data)
+ attention: ->
+ @attentionReceived = true
message: ->
- @transitionTo(@STATE.LOGGED_IN)
- sqlRequest = @request
- @request = undefined
+ # 3.2.5.7 Sent Attention State
+ # Discard any data contained in the response, until we receive the attention response
+ if @attentionReceived
+ sqlRequest = @request
+ @request = undefined
+
+ @transitionTo(@STATE.LOGGED_IN)
+
+ if sqlRequest.canceled
+ sqlRequest.callback(RequestError("Canceled.", 'ECANCEL'))
+ else
+ message = "Timeout: Request failed to complete in #{@config.options.requestTimeout}ms"
+ sqlRequest.callback(RequestError(message, 'ETIMEOUT'))
- if sqlRequest.canceled
- sqlRequest.callback(RequestError("Canceled.", 'ECANCEL'))
- else
- message = "Timeout: Request failed to complete in #{@config.options.requestTimeout}ms"
- sqlRequest.callback(RequestError(message, 'ETIMEOUT'))
FINAL:
name: 'Final'
@@ -333,18 +342,16 @@ class Connection extends EventEmitter
)
@tokenStreamParser.on('done', (token) =>
if @request
- # 3.2.5.7 Sent Attention State
- # Discard any data contained in the response
- if @state == @STATE.SENT_ATTENTION
- @request.emit('attention') if token.attention
- else
- @request.emit('done', token.rowCount, token.more, @request.rows)
+ if token.attention
+ @dispatchEvent("attention")
+
+ @request.emit('done', token.rowCount, token.more, @request.rows)
- if token.rowCount != undefined
- @request.rowCount += token.rowCount
+ if token.rowCount != undefined
+ @request.rowCount += token.rowCount
- if @config.options.rowCollectionOnDone
- @request.rows = []
+ if @config.options.rowCollectionOnDone
+ @request.rows = []
)
@tokenStreamParser.on('resetConnection', (token) =>
@emit('resetConnection')

0 comments on commit 09fd8a8

Please sign in to comment.