-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add check for nats connection (#107)
* 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
Showing
8 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
* gRPC | ||
* Memcached | ||
* InfluxDB | ||
* Nats | ||
|
||
## Usage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
* MySQL | ||
* gRPC | ||
* Memcached | ||
* Nats | ||
|
||
## Usage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters