diff --git a/src/node/health_check/health.js b/src/node/health_check/health.js index 09a57d38bd285150e77f872aac050eee175d4078..87e00197fe57e4a4b6eb3d4a65439a09c94ac414 100644 --- a/src/node/health_check/health.js +++ b/src/node/health_check/health.js @@ -45,13 +45,22 @@ function HealthImplementation(statusMap) { this.statusMap = _.clone(statusMap); } -HealthImplementation.prototype.setStatus = function(service, status) { - this.statusMap[service] = status; +HealthImplementation.prototype.setStatus = function(host, service, status) { + if (!this.statusMap[host]) { + this.statusMap[host] = {}; + } + this.statusMap[host][service] = status; }; HealthImplementation.prototype.check = function(call, callback){ + var host = call.request.host; var service = call.request.service; - callback(null, {status: _.get(this.statusMap, service, 'UNKNOWN')}); + var status = _.get(this.statusMap, [host, service], null); + if (status === null) { + callback({code:grpc.status.NOT_FOUND}); + } else { + callback(null, {status: status}); + } }; module.exports = { diff --git a/src/node/health_check/health.proto b/src/node/health_check/health.proto index 1f6eb6a80de3c60f977c3f4391a78448cba8ed93..d31df1e0a7c018837d2ede9fed8e0084d552c4a3 100644 --- a/src/node/health_check/health.proto +++ b/src/node/health_check/health.proto @@ -32,7 +32,8 @@ syntax = "proto3"; package grpc.health.v1alpha; message HealthCheckRequest { - string service = 1; + string host = 1; + string service = 2; } message HealthCheckResponse { diff --git a/src/node/test/health_test.js b/src/node/test/health_test.js index 456b686b9b0ccd3bd2f596c3d29d189fa8db382a..4d1a5082e046f297dd152b7231c097beab153758 100644 --- a/src/node/test/health_test.js +++ b/src/node/test/health_test.js @@ -40,13 +40,18 @@ var health = require('../health_check/health.js'); var grpc = require('../'); describe('Health Checking', function() { + var statusMap = { + '': { + '': 'SERVING', + 'grpc.test.TestService': 'NOT_SERVING', + }, + virtual_host: { + 'grpc.test.TestService': 'SERVING' + } + }; var HealthServer = grpc.buildServer([health.service]); var healthServer = new HealthServer({ - 'grpc.health.v1alpha.Health': new health.Implementation({ - '': 'SERVING', - 'grpc.health.v1alpha.Health': 'SERVING', - 'not.serving.Service': 'NOT_SERVING' - }) + 'grpc.health.v1alpha.Health': new health.Implementation(statusMap) }); var healthClient; before(function() { @@ -57,34 +62,41 @@ describe('Health Checking', function() { after(function() { healthServer.shutdown(); }); - it('should respond with SERVING with no service specified', function(done) { - healthClient.check({}, function(err, response) { + it('should say an enabled service is SERVING', function(done) { + healthClient.check({service: ''}, function(err, response) { assert.ifError(err); assert.strictEqual(response.status, 'SERVING'); done(); }); }); - it('should respond that the health check service is SERVING', function(done) { - healthClient.check({service: 'grpc.health.v1alpha.Health'}, + it('should say that a disabled service is NOT_SERVING', function(done) { + healthClient.check({service: 'grpc.test.TestService'}, function(err, response) { assert.ifError(err); - assert.strictEqual(response.status, 'SERVING'); + assert.strictEqual(response.status, 'NOT_SERVING'); done(); }); }); - it('should respond that a disabled service is NOT_SERVING', function(done) { - healthClient.check({service: 'not.serving.Service'}, + it('should say that a service on another host is SERVING', function(done) { + healthClient.check({host: 'virtual_host', service: 'grpc.test.TestService'}, function(err, response) { assert.ifError(err); - assert.strictEqual(response.status, 'NOT_SERVING'); + assert.strictEqual(response.status, 'SERVING'); done(); }); }); - it('should respond with UNKNOWN for an unknown service', function(done) { - healthClient.check({service: 'unknown.service.Name'}, + it('should get NOT_FOUND if the service is not registered', function(done) { + healthClient.check({service: 'not_registered'}, function(err, response) { + assert(err); + assert.strictEqual(err.code, grpc.status.NOT_FOUND); + done(); + }); + }); + it('should get NOT_FOUND if the host is not registered', function(done) { + healthClient.check({host: 'wrong_host', service: 'grpc.test.TestService'}, function(err, response) { - assert.ifError(err); - assert.strictEqual(response.status, 'UNKNOWN'); + assert(err); + assert.strictEqual(err.code, grpc.status.NOT_FOUND); done(); }); });