Skip to content

Commit

Permalink
feat: add check for nats connection (#107)
Browse files Browse the repository at this point in the history
* feat: add check for nats connection

* add nats dependency to docker-compose.yml

* go imports nats check

* update doc

* add missing nats env variable

* address PR comments
  • Loading branch information
hosseio authored Jun 28, 2023
1 parent a076350 commit c2891e9
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* gRPC
* Memcached
* InfluxDB
* Nats

## Usage

Expand Down
4 changes: 4 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ tasks:
sh: docker-compose port influxdb 8086
CASSANDRA_HOST:
sh: docker-compose port cassandra 9042
NATS_HOST:
sh: docker-compose port nats 4222

env:
HEALTH_GO_PG_PQ_DSN: 'postgres://test:test@{{.PG_PQ_HOST}}/test?sslmode=disable'
HEALTH_GO_PG_PGX4_DSN: 'postgres://test:test@{{.PG_PGX4_HOST}}/test?sslmode=disable'
Expand All @@ -64,3 +67,4 @@ tasks:
HEALTH_GO_MD_DSN: 'memcached://localhost:{{.MEMCACHED_HOST}}/'
HEALTH_GO_INFLUXDB_URL: 'http://{{.INFLUX_HOST}}'
HEALTH_GO_CASSANDRA_HOST: '{{.CASSANDRA_HOST}}'
HEALTH_GO_NATS_DSN: 'nats://{{.NATS_HOST}}'
32 changes: 32 additions & 0 deletions checks/nats/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package nats

import (
"context"
"fmt"

"github.com/nats-io/nats.go"
)

// Config is the NATS checker configuration settings container.
type Config struct {
// DSN is the NATS instance connection DSN. Required.
DSN string
}

// New creates new NATS health check that verifies the status of the connection.
func New(config Config) func(ctx context.Context) error {
return func(ctx context.Context) error {
nc, err := nats.Connect(config.DSN)
if err != nil {
return fmt.Errorf("nats health check failed on client creation: %w", err)
}
defer nc.Close()

status := nc.Status()
if status != nats.CONNECTED {
return fmt.Errorf("nats health check failed as connection status is %s", status)
}

return nil
}
}
29 changes: 29 additions & 0 deletions checks/nats/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nats

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
)

const natsDSNEnv = "HEALTH_GO_NATS_DSN"

func TestNew(t *testing.T) {
check := New(Config{
DSN: getDSN(t),
})

err := check(context.Background())
require.NoError(t, err)
}

func getDSN(t *testing.T) string {
t.Helper()

dsn, ok := os.LookupEnv(natsDSNEnv)
require.True(t, ok)

return dsn
}
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,10 @@ services:
interval: 15s
timeout: 10s
retries: 10

nats:
container_name: nats
image: nats:2.9.11
command: "-js -sd /data"
ports:
- "4222:4222"
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* MySQL
* gRPC
* Memcached
* Nats

## Usage

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/jackc/pgx/v4 v4.18.1
github.com/jackc/pgx/v5 v5.4.1
github.com/lib/pq v1.10.9
github.com/nats-io/nats.go v1.27.1
github.com/rabbitmq/amqp091-go v1.8.1
github.com/stretchr/testify v1.8.4
github.com/vitorsalgado/mocha/v2 v2.0.2
Expand Down Expand Up @@ -58,6 +59,9 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/nats-io/nats-server/v2 v2.9.19 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -177,6 +178,15 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/nats-io/jwt/v2 v2.4.1 h1:Y35W1dgbbz2SQUYDPCaclXcuqleVmpbRa7646Jf2EX4=
github.com/nats-io/nats-server/v2 v2.9.19 h1:OF9jSKZGo425C/FcVVIvNgpd36CUe7aVTTXEZRJk6kA=
github.com/nats-io/nats-server/v2 v2.9.19/go.mod h1:aTb/xtLCGKhfTFLxP591CMWfkdgBmcUUSkiSOe5A3gw=
github.com/nats-io/nats.go v1.27.1 h1:OuYnal9aKVSnOzLQIzf7554OXMCG7KbaTkCSBHRcSoo=
github.com/nats-io/nats.go v1.27.1/go.mod h1:XpbWUlOElGwTYbMR7imivs7jJj9GtK7ypv321Wp6pjc=
github.com/nats-io/nkeys v0.4.4 h1:xvBJ8d69TznjcQl9t6//Q5xXuVhyYiSos6RPtvQNTwA=
github.com/nats-io/nkeys v0.4.4/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5sNvt64=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
Expand Down Expand Up @@ -334,6 +344,7 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down

0 comments on commit c2891e9

Please sign in to comment.