Skip to content

Commit

Permalink
MG-2235 - Check gRPC service is healthy during setup (#2245)
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
  • Loading branch information
rodneyosodo committed Jun 12, 2024
1 parent 75a66a4 commit 487dcc6
Show file tree
Hide file tree
Showing 25 changed files with 123 additions and 53 deletions.
30 changes: 15 additions & 15 deletions auth/api/grpc/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func startGRPCServer(svc auth.Service, port int) {
}

func TestIssue(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -132,7 +132,7 @@ func TestIssue(t *testing.T) {
}

func TestRefresh(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestRefresh(t *testing.T) {
}

func TestIdentify(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -222,7 +222,7 @@ func TestIdentify(t *testing.T) {
}

func TestAuthorize(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -344,7 +344,7 @@ func TestAuthorize(t *testing.T) {
}

func TestAddPolicy(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -398,7 +398,7 @@ func TestAddPolicy(t *testing.T) {
}

func TestAddPolicies(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -460,7 +460,7 @@ func TestAddPolicies(t *testing.T) {
}

func TestDeletePolicy(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -513,7 +513,7 @@ func TestDeletePolicy(t *testing.T) {
}

func TestDeletePolicies(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -576,7 +576,7 @@ func TestDeletePolicies(t *testing.T) {
}

func TestListObjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -626,7 +626,7 @@ func TestListObjects(t *testing.T) {
}

func TestListAllObjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -676,7 +676,7 @@ func TestListAllObjects(t *testing.T) {
}

func TestCountObects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -726,7 +726,7 @@ func TestCountObects(t *testing.T) {
}

func TestListSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -776,7 +776,7 @@ func TestListSubjects(t *testing.T) {
}

func TestListAllSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf(`"Unexpected error creating client connection %s"`, err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -826,7 +826,7 @@ func TestListAllSubjects(t *testing.T) {
}

func TestCountSubjects(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down Expand Up @@ -881,7 +881,7 @@ func TestCountSubjects(t *testing.T) {
}

func TestListPermissions(t *testing.T) {
conn, err := grpc.Dial(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(authAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.Nil(t, err, fmt.Sprintf("Unexpected error creating client connection %s", err))
client := grpcapi.NewClient(conn, time.Second)

Expand Down
2 changes: 1 addition & 1 deletion cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func main() {
return
}

authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions cmd/cassandra-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {
return
}

ac, acHandler, err := auth.Setup(authConfig)
ac, acHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand All @@ -93,7 +93,7 @@ func main() {
return
}

tc, tcHandler, err := auth.SetupAuthz(authConfig)
tc, tcHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/certs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func main() {
return
}

authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/coap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func main() {
return
}

authClient, authHandler, err := auth.SetupAuthz(authConfig)
authClient, authHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func main() {
return
}

authClient, authHandler, err := auth.SetupAuthz(authConfig)
authClient, authHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions cmd/influxdb-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
return
}

ac, acHandler, err := auth.Setup(authConfig)
ac, acHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand All @@ -92,7 +92,7 @@ func main() {
return
}

tc, tcHandler, err := auth.SetupAuthz(authConfig)
tc, tcHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/invitations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func main() {
exitCode = 1
return
}
authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions cmd/mongodb-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
return
}

ac, acHandler, err := auth.Setup(authConfig)
ac, acHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand All @@ -101,7 +101,7 @@ func main() {
return
}

tc, tcHandler, err := auth.SetupAuthz(authConfig)
tc, tcHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/mqtt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func main() {
return
}

authClient, authHandler, err := auth.SetupAuthz(authConfig)
authClient, authHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions cmd/postgres-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {
return
}

ac, acHandler, err := auth.Setup(authConfig)
ac, acHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand All @@ -107,7 +107,7 @@ func main() {
return
}

tc, tcHandler, err := auth.SetupAuthz(authConfig)
tc, tcHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/smpp-notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func main() {
return
}

authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/smtp-notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func main() {
return
}

authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/things/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func main() {
return
}

authServiceClient, authHandler, err := auth.Setup(authConfig)
authServiceClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
4 changes: 2 additions & 2 deletions cmd/timescale-reader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func main() {
return
}

ac, acHandler, err := auth.Setup(authConfig)
ac, acHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand All @@ -107,7 +107,7 @@ func main() {
return
}

tc, tcHandler, err := auth.SetupAuthz(authConfig)
tc, tcHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/twins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func main() {
return
}

authServiceClient, authHandler, err := auth.Setup(authConfig)
authServiceClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/users/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func main() {
return
}

authClient, authHandler, err := auth.Setup(authConfig)
authClient, authHandler, err := auth.Setup(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 1 addition & 1 deletion cmd/ws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func main() {
return
}

authClient, authHandler, err := auth.SetupAuthz(authConfig)
authClient, authHandler, err := auth.SetupAuthz(ctx, authConfig)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down
2 changes: 2 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ services:
container_name: magistrala-invitations
restart: on-failure
depends_on:
- auth
- invitations-db
environment:
MG_INVITATIONS_LOG_LEVEL: ${MG_INVITATIONS_LOG_LEVEL}
Expand Down Expand Up @@ -255,6 +256,7 @@ services:
env_file:
- .env
depends_on:
- auth
- things
- users
- mqtt-adapter
Expand Down
11 changes: 8 additions & 3 deletions internal/server/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
grpchealth "google.golang.org/grpc/health/grpc_health_v1"
)

const (
stopWaitTime = 5 * time.Second
)
const stopWaitTime = 5 * time.Second

type Server struct {
server.BaseServer
server *grpc.Server
registerService serviceRegister
health *health.Server
}

type serviceRegister func(srv *grpc.Server)
Expand Down Expand Up @@ -116,7 +117,10 @@ func (s *Server) Start() error {
grpcServerOptions = append(grpcServerOptions, creds)

s.server = grpc.NewServer(grpcServerOptions...)
s.health = health.NewServer()
grpchealth.RegisterHealthServer(s.server, s.health)
s.registerService(s.server)
s.health.SetServingStatus(s.Name, grpchealth.HealthCheckResponse_SERVING)

go func() {
errCh <- s.server.Serve(listener)
Expand All @@ -136,6 +140,7 @@ func (s *Server) Stop() error {
c := make(chan bool)
go func() {
defer close(c)
s.health.Shutdown()
s.server.GracefulStop()
}()
select {
Expand Down
Loading

0 comments on commit 487dcc6

Please sign in to comment.