Skip to content

Commit

Permalink
Changed strategy for probe scheduling to ensure there is only one pen…
Browse files Browse the repository at this point in the history
…ding probe at a time to avoid resource exhaustion in edge cases, e.g. if there is no response from peer on HTTP connect. Minor changes.
  • Loading branch information
mwittig committed Nov 16, 2015
1 parent 894b377 commit 329e56d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

[![npm version](https://badge.fury.io/js/pimatic-probe.svg)](http://badge.fury.io/js/pimatic-probe)

A pimatic plugin to probe HTTP(S), TCP and UDP services.

Note: UDP is currently not supported and will be added at a later stage!
A pimatic plugin to probe HTTP(S) and TCP services.

## Configuration

Expand Down Expand Up @@ -92,7 +90,7 @@ By default, HttpProbe does not verify the server certificate if connected to a H
enabled by setting the `verifyPeerCert` property to `true`. In this case, HttpProbe will fail (`absent` state) if the
server certificate cannot be verified.

{
{
"id": "probe4",
"class": "HttpProbe",
"name": "Router Web Page with Basic Auth",
Expand Down Expand Up @@ -233,4 +231,8 @@ If you wish to hide the sparkline (the mini-graph) of `connectTime` attribute di
* Dependency updates
* 20150918, V0.1.1
* Dependency updates
* Minor changes
* Minor changes
* 20151116, V0.2.0
* Changed strategy for probe scheduling to ensure there is only one pending probe at a time to avoid
resource exhaustion in edge cases, e.g. if there is no response from peer on HTTP connect.
* Minor changes
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "pimatic-probe",
"description": "A pimatic plugin to probe HTTP(S), TCP and UDP services",
"description": "A pimatic plugin to probe HTTP(S), TCP services",
"author": {
"name": "Marcus Wittig",
"url": "https://github.com/mwittig/pimatic-probe"
},
"main": "probe",
"files": [
"probe.coffee",
"README.md",
"probe-config-schema.coffee",
"device-config-schema.coffee",
"LICENSE"
"LICENSE",
"README.md"
],
"version": "0.1.1",
"homepage": "https://github.com/mwittig/pimatic-probe",
Expand Down
42 changes: 21 additions & 21 deletions probe.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,23 @@ module.exports = (env) ->
super()

if not @deviceConfigurationError
@_scheduleUpdate()
# perform an update now
@_requestUpdate()


# poll device according to interval
_scheduleUpdate: () ->
if typeof @intervalObject isnt 'undefined'
clearInterval(=>
@intervalObject
)
if @timeoutObject?
clearTimeout @timeoutObject

# keep updating
if @interval > 0
@intervalObject = setInterval(=>
# keep updating
@timeoutObject = setTimeout( =>
@timeoutObject = null
@_requestUpdate()
, @interval
)

# perform an update now
@_requestUpdate()

_requestUpdate: ->
@_ping().then(=>
Expand All @@ -106,6 +104,8 @@ module.exports = (env) ->
env.logger.error newError if @_lastError isnt newError or @debug
@_lastError = newError
@_setPresence (no)
).finally( =>
@_scheduleUpdate()
)

_ping: ->
Expand Down Expand Up @@ -159,7 +159,7 @@ module.exports = (env) ->
@_presence = lastState?.presence?.value or false
@_port = config.port
@interval = Math.max 1000 * config.interval, 10000
@_timeout = Math.min @interval, config.timeout * 1000
@_connectTimeout = Math.min @interval, config.timeout * 1000

if config.enableConnectTime
@addAttribute('connectTime', {
Expand All @@ -177,26 +177,24 @@ module.exports = (env) ->
env.logger.error "Probe for device id=#{@id}. host=#{config.host}: Name Lookup failed: " + error
else
@_host = address
@_scheduleUpdate()
# perform an update now
@_requestUpdate()
)


# poll device according to interval
_scheduleUpdate: () ->
if typeof @intervalObject isnt 'undefined'
clearInterval(=>
@intervalObject
)
if @timeoutObject?
clearTimeout @timeoutObject

# keep updating
if @interval > 0
@intervalObject = setInterval(=>
# keep updating
@timeoutObject = setTimeout( =>
@timeoutObject = null
@_requestUpdate()
, @interval
)

# perform an update now
@_requestUpdate()

_requestUpdate: ->
@_connect().then(=>
@_setPresence (yes)
Expand All @@ -205,13 +203,15 @@ module.exports = (env) ->
env.logger.error newError if @_lastError isnt newError or @debug
@_lastError = newError
@_setPresence (no)
).finally( =>
@_scheduleUpdate()
)

_connect: ->
return new Promise((resolve, reject) =>
timeStart = process.hrtime()
client = new net.Socket
client.setTimeout(@_timeout, =>
client.setTimeout(@_connectTimeout, =>
client.destroy()
reject(new Error "TCP connect attempt timed out")
)
Expand Down

0 comments on commit 329e56d

Please sign in to comment.