Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement Request Timeouts.

The implementation was kindly provided by Jeevan Kumar Jagadesh.
  • Loading branch information...
commit 0b90f6bbd0274fc61057ab118dace033b054ac71 1 parent dd783d6
@arthurschreiber arthurschreiber authored
Showing with 38 additions and 15 deletions.
  1. +38 −15 src/connection.coffee
View
53 src/connection.coffee
@@ -126,6 +126,7 @@ class Connection extends EventEmitter
data: (data) ->
@sendDataToTokenStreamParser(data)
message: ->
+ @clearRequestTimer()
@transitionTo(@STATE.LOGGED_IN)
sqlRequest = @request
@@ -140,14 +141,15 @@ class Connection extends EventEmitter
data: (data) ->
@sendDataToTokenStreamParser(data)
message: ->
- if @request.canceled
- @transitionTo(@STATE.LOGGED_IN)
+ @transitionTo(@STATE.LOGGED_IN)
+ sqlRequest = @request
+ @request = undefined
- sqlRequest = @request
- @request = undefined
+ if sqlRequest.canceled
sqlRequest.callback(RequestError("Canceled.", 'ECANCEL'))
-
- # else: skip all messages, now we are only interested about cancel acknowledgement (2.2.1.6)
+ else
+ message = "Timeout: Request failed to complete in #{@config.options.requestTimeout}ms"
+ sqlRequest.callback(RequestError(message, 'ETIMEOUT'))
FINAL:
name: 'Final'
@@ -183,6 +185,7 @@ class Connection extends EventEmitter
cleanupConnection: ->
if !@closed
@clearConnectTimer()
+ @clearRequestTimer()
@closeConnection()
@emit('end')
@closed = true
@@ -330,16 +333,18 @@ class Connection extends EventEmitter
)
@tokenStreamParser.on('done', (token) =>
if @request
- @request.emit('done', token.rowCount, token.more, @request.rows)
-
- if token.rowCount != undefined
- @request.rowCount += token.rowCount
+ # 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 @config.options.rowCollectionOnDone
- @request.rows = []
-
- if token.attention
- @request.canceled = true
+ if token.rowCount != undefined
+ @request.rowCount += token.rowCount
+
+ if @config.options.rowCollectionOnDone
+ @request.rows = []
)
@tokenStreamParser.on('resetConnection', (token) =>
@emit('resetConnection')
@@ -383,6 +388,10 @@ class Connection extends EventEmitter
createConnectTimer: ->
@connectTimer = setTimeout(@connectTimeout, @config.options.connectTimeout)
+ createRequestTimer: ->
+ if @config.options.requestTimeout
+ @requestTimer = setTimeout(@requestTimeout, @config.options.requestTimeout)
+
connectTimeout: =>
message = "Failed to connect to #{@config.server}:#{@config.options.port} in #{@config.options.connectTimeout}ms"
@@ -391,10 +400,20 @@ class Connection extends EventEmitter
@connectTimer = undefined
@dispatchEvent('connectTimeout')
+ requestTimeout: =>
+ @requestTimer = undefined
+
+ @messageIo.sendMessage(TYPE.ATTENTION)
+ @transitionTo(@STATE.SENT_ATTENTION)
+
clearConnectTimer: ->
if @connectTimer
clearTimeout(@connectTimer)
+ clearRequestTimer: ->
+ if @requestTimer
+ clearTimeout(@requestTimer)
+
transitionTo: (newState) ->
if @state?.exit
@state.exit.apply(@)
@@ -596,6 +615,8 @@ set transaction isolation level read committed'''
@request.rowCount = 0
@request.rows = []
+ @createRequestTimer()
+
@messageIo.sendMessage(packetType, payload.data, @resetConnectionOnNextRequest)
@resetConnectionOnNextRequest = false
@debug.payload(->
@@ -611,6 +632,8 @@ set transaction isolation level read committed'''
@debug.log(message)
false
else
+ @request.canceled = true
+
@messageIo.sendMessage(TYPE.ATTENTION)
@transitionTo(@STATE.SENT_ATTENTION)
true
Please sign in to comment.
Something went wrong with that request. Please try again.