{"action":"created","comment":{"url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/comments/56296654","id":56296654,"diff_hunk":"@@ -0,0 +1,85 @@\n+import {ClientPingCodec} from './codec/ClientPingCodec';\n+import HazelcastClient = require('./HazelcastClient');\n+import ClientConnection = require('./invocation/ClientConnection');\n+import {ConnectionHeartbeatListener} from './ConnectionHeartbeatListener';\n+import Q = require('q');\n+\n+var PROPERTY_HEARTBEAT_INTERVAL: string = 'hazelcast.client.heartbeat.interval';\n+var PROPERTY_HEARTBEAT_TIMEOUT: string = 'hazelcast.client.heartbeat.timeout';\n+\n+class Heartbeat {\n+\n+    private client: HazelcastClient;\n+    private heartbeatTimeout: number;\n+    private heartbeatInterval: number;\n+    private listeners: ConnectionHeartbeatListener[] = [];\n+\n+    //Actually it is a NodeJS.Timer. Another typing file that comes with a module we use causes TSD to see\n+    //return type of setTimeout as number. Because of this we defined timer property as `any` type.\n+    private timer: any;\n+\n+    constructor(client: HazelcastClient) {\n+        this.client = client;\n+        this.heartbeatInterval = this.client.getConfig().properties[PROPERTY_HEARTBEAT_INTERVAL];\n+        this.heartbeatTimeout = this.client.getConfig().properties[PROPERTY_HEARTBEAT_TIMEOUT];\n+    }\n+\n+    start() {\n+        this.timer = setTimeout(this.heartbeatFunction.bind(this), this.heartbeatInterval);\n+    }\n+\n+    cancel() {\n+        clearTimeout(this.timer);\n+    }\n+\n+    addListener(heartbeatListener: ConnectionHeartbeatListener) {\n+        this.listeners.push(heartbeatListener);\n+    }\n+\n+    private heartbeatFunction() {\n+        var estConnections = this.client.getConnectionManager().establishedConnections;\n+        for (var address in estConnections) {\n+            if ( estConnections.hasOwnProperty(address)) {\n+                var conn = estConnections[address];\n+                var timeSinceLastRead = new Date().getTime() - conn.lastRead;\n+                if (timeSinceLastRead > this.heartbeatTimeout) {\n+                    if (conn.heartbeating) {\n+                        setImmediate(this.onHeartbeatStopped.bind(this), conn);\n+                    }\n+                }\n+                if (timeSinceLastRead > this.heartbeatInterval) {\n+                    var req = ClientPingCodec.encodeRequest();\n+                    this.client.getInvocationService().invokeOnConnection(conn, req);\n+                } else {\n+                    if (!conn.heartbeating) {\n+                        setImmediate(this.onHeartbeatRestored.bind(this), conn);\n+                    }\n+                }\n+            }\n+        }\n+        this.timer = setTimeout(this.heartbeatFunction.bind(this), this.heartbeatInterval);\n+    }\n+\n+    private onHeartbeatStopped(connection: ClientConnection) {\n+        connection.heartbeating = false;\n+        console.log('heartbeat stopped on ' + connection.address);\n+        this.listeners.forEach((listener) => {\n+            if (listener.hasOwnProperty('onHeartbeatStopped')) {\n+                Q.fcall(listener.onHeartbeatStopped.bind(this), connection);","path":"src/Heartbeat.ts","position":null,"original_position":68,"commit_id":"0795fa0209426f8c3af70a2650116f36a3cde612","original_commit_id":"b1f0124b8d8dc27b435b02df52e6e56405111870","user":{"login":"GhostInAMachine","id":11163575,"avatar_url":"https://avatars.githubusercontent.com/u/11163575?v=3","gravatar_id":"","url":"https://api.github.com/users/GhostInAMachine","html_url":"https://github.com/GhostInAMachine","followers_url":"https://api.github.com/users/GhostInAMachine/followers","following_url":"https://api.github.com/users/GhostInAMachine/following{/other_user}","gists_url":"https://api.github.com/users/GhostInAMachine/gists{/gist_id}","starred_url":"https://api.github.com/users/GhostInAMachine/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GhostInAMachine/subscriptions","organizations_url":"https://api.github.com/users/GhostInAMachine/orgs","repos_url":"https://api.github.com/users/GhostInAMachine/repos","events_url":"https://api.github.com/users/GhostInAMachine/events{/privacy}","received_events_url":"https://api.github.com/users/GhostInAMachine/received_events","type":"User","site_admin":false},"body":"Probably","created_at":"2016-03-16T08:27:38Z","updated_at":"2016-03-16T08:27:38Z","html_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43#discussion_r56296654","pull_request_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43","_links":{"self":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/comments/56296654"},"html":{"href":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43#discussion_r56296654"},"pull_request":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43"}}},"pull_request":{"url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43","id":62404475,"html_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43","diff_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43.diff","patch_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43.patch","issue_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/43","number":43,"state":"open","locked":false,"title":"heartbeat service","user":{"login":"mustafaiman","id":6582349,"avatar_url":"https://avatars.githubusercontent.com/u/6582349?v=3","gravatar_id":"","url":"https://api.github.com/users/mustafaiman","html_url":"https://github.com/mustafaiman","followers_url":"https://api.github.com/users/mustafaiman/followers","following_url":"https://api.github.com/users/mustafaiman/following{/other_user}","gists_url":"https://api.github.com/users/mustafaiman/gists{/gist_id}","starred_url":"https://api.github.com/users/mustafaiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mustafaiman/subscriptions","organizations_url":"https://api.github.com/users/mustafaiman/orgs","repos_url":"https://api.github.com/users/mustafaiman/repos","events_url":"https://api.github.com/users/mustafaiman/events{/privacy}","received_events_url":"https://api.github.com/users/mustafaiman/received_events","type":"User","site_admin":false},"body":"Implements Heartbeat mechanism.\r\nAdds options for `hazelcast.client.heartbeat.interval` and `hazelcast.client.heartbeat.timeout` to config.\r\nDetects unresponsive nodes based on Heartbeat but doesn't do anything about it.","created_at":"2016-03-10T15:17:23Z","updated_at":"2016-03-16T08:27:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f719363fe5dd24caa1ffbd260efc8b5149f1b412","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43/commits","review_comments_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43/comments","review_comment_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/43/comments","statuses_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/statuses/0795fa0209426f8c3af70a2650116f36a3cde612","head":{"label":"mustafaiman:heartbeat","ref":"heartbeat","sha":"0795fa0209426f8c3af70a2650116f36a3cde612","user":{"login":"mustafaiman","id":6582349,"avatar_url":"https://avatars.githubusercontent.com/u/6582349?v=3","gravatar_id":"","url":"https://api.github.com/users/mustafaiman","html_url":"https://github.com/mustafaiman","followers_url":"https://api.github.com/users/mustafaiman/followers","following_url":"https://api.github.com/users/mustafaiman/following{/other_user}","gists_url":"https://api.github.com/users/mustafaiman/gists{/gist_id}","starred_url":"https://api.github.com/users/mustafaiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mustafaiman/subscriptions","organizations_url":"https://api.github.com/users/mustafaiman/orgs","repos_url":"https://api.github.com/users/mustafaiman/repos","events_url":"https://api.github.com/users/mustafaiman/events{/privacy}","received_events_url":"https://api.github.com/users/mustafaiman/received_events","type":"User","site_admin":false},"repo":{"id":48358918,"name":"hazelcast-nodejs-client","full_name":"mustafaiman/hazelcast-nodejs-client","owner":{"login":"mustafaiman","id":6582349,"avatar_url":"https://avatars.githubusercontent.com/u/6582349?v=3","gravatar_id":"","url":"https://api.github.com/users/mustafaiman","html_url":"https://github.com/mustafaiman","followers_url":"https://api.github.com/users/mustafaiman/followers","following_url":"https://api.github.com/users/mustafaiman/following{/other_user}","gists_url":"https://api.github.com/users/mustafaiman/gists{/gist_id}","starred_url":"https://api.github.com/users/mustafaiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mustafaiman/subscriptions","organizations_url":"https://api.github.com/users/mustafaiman/orgs","repos_url":"https://api.github.com/users/mustafaiman/repos","events_url":"https://api.github.com/users/mustafaiman/events{/privacy}","received_events_url":"https://api.github.com/users/mustafaiman/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mustafaiman/hazelcast-nodejs-client","description":"NodeJS Client for Hazelcast, using Hazelcast Open Client Protocol 1.0 for Hazelcast 3.6 and higher","fork":true,"url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client","forks_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/forks","keys_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/teams","hooks_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/hooks","issue_events_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/issues/events{/number}","events_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/events","assignees_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/assignees{/user}","branches_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/branches{/branch}","tags_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/tags","blobs_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/statuses/{sha}","languages_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/languages","stargazers_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/stargazers","contributors_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/contributors","subscribers_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/subscribers","subscription_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/subscription","commits_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/contents/{+path}","compare_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/merges","archive_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/downloads","issues_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/issues{/number}","pulls_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/pulls{/number}","milestones_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/milestones{/number}","notifications_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/labels{/name}","releases_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/releases{/id}","deployments_url":"https://api.github.com/repos/mustafaiman/hazelcast-nodejs-client/deployments","created_at":"2015-12-21T08:02:41Z","updated_at":"2016-03-10T10:09:12Z","pushed_at":"2016-03-16T08:11:50Z","git_url":"git://github.com/mustafaiman/hazelcast-nodejs-client.git","ssh_url":"git@github.com:mustafaiman/hazelcast-nodejs-client.git","clone_url":"https://github.com/mustafaiman/hazelcast-nodejs-client.git","svn_url":"https://github.com/mustafaiman/hazelcast-nodejs-client","homepage":null,"size":134,"stargazers_count":0,"watchers_count":0,"language":"TypeScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"hazelcast-incubator:master","ref":"master","sha":"29cd2d4a528163e2231ac2f1d9ae09c62125bac9","user":{"login":"hazelcast-incubator","id":11215411,"avatar_url":"https://avatars.githubusercontent.com/u/11215411?v=3","gravatar_id":"","url":"https://api.github.com/users/hazelcast-incubator","html_url":"https://github.com/hazelcast-incubator","followers_url":"https://api.github.com/users/hazelcast-incubator/followers","following_url":"https://api.github.com/users/hazelcast-incubator/following{/other_user}","gists_url":"https://api.github.com/users/hazelcast-incubator/gists{/gist_id}","starred_url":"https://api.github.com/users/hazelcast-incubator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hazelcast-incubator/subscriptions","organizations_url":"https://api.github.com/users/hazelcast-incubator/orgs","repos_url":"https://api.github.com/users/hazelcast-incubator/repos","events_url":"https://api.github.com/users/hazelcast-incubator/events{/privacy}","received_events_url":"https://api.github.com/users/hazelcast-incubator/received_events","type":"Organization","site_admin":false},"repo":{"id":45202495,"name":"hazelcast-nodejs-client","full_name":"hazelcast-incubator/hazelcast-nodejs-client","owner":{"login":"hazelcast-incubator","id":11215411,"avatar_url":"https://avatars.githubusercontent.com/u/11215411?v=3","gravatar_id":"","url":"https://api.github.com/users/hazelcast-incubator","html_url":"https://github.com/hazelcast-incubator","followers_url":"https://api.github.com/users/hazelcast-incubator/followers","following_url":"https://api.github.com/users/hazelcast-incubator/following{/other_user}","gists_url":"https://api.github.com/users/hazelcast-incubator/gists{/gist_id}","starred_url":"https://api.github.com/users/hazelcast-incubator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hazelcast-incubator/subscriptions","organizations_url":"https://api.github.com/users/hazelcast-incubator/orgs","repos_url":"https://api.github.com/users/hazelcast-incubator/repos","events_url":"https://api.github.com/users/hazelcast-incubator/events{/privacy}","received_events_url":"https://api.github.com/users/hazelcast-incubator/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client","description":"NodeJS Client for Hazelcast, using Hazelcast Open Client Protocol 1.0 for Hazelcast 3.6 and higher","fork":false,"url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client","forks_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/forks","keys_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/teams","hooks_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/hooks","issue_events_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/events{/number}","events_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/events","assignees_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/assignees{/user}","branches_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/branches{/branch}","tags_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/tags","blobs_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/git/refs{/sha}","trees_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/statuses/{sha}","languages_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/languages","stargazers_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/stargazers","contributors_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/contributors","subscribers_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/subscribers","subscription_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/subscription","commits_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/commits{/sha}","git_commits_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/git/commits{/sha}","comments_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/comments{/number}","issue_comment_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/comments{/number}","contents_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/contents/{+path}","compare_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/merges","archive_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/downloads","issues_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues{/number}","pulls_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls{/number}","milestones_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/milestones{/number}","notifications_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/labels{/name}","releases_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/releases{/id}","deployments_url":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/deployments","created_at":"2015-10-29T18:24:48Z","updated_at":"2016-02-22T11:24:10Z","pushed_at":"2016-03-16T08:11:50Z","git_url":"git://github.com/hazelcast-incubator/hazelcast-nodejs-client.git","ssh_url":"git@github.com:hazelcast-incubator/hazelcast-nodejs-client.git","clone_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client.git","svn_url":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client","homepage":null,"size":198,"stargazers_count":3,"watchers_count":3,"language":"TypeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":6,"mirror_url":null,"open_issues_count":12,"forks":6,"open_issues":12,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43"},"html":{"href":"https://github.com/hazelcast-incubator/hazelcast-nodejs-client/pull/43"},"issue":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/43"},"comments":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/issues/43/comments"},"review_comments":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43/comments"},"review_comment":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/pulls/43/commits"},"statuses":{"href":"https://api.github.com/repos/hazelcast-incubator/hazelcast-nodejs-client/statuses/0795fa0209426f8c3af70a2650116f36a3cde612"}}}}