Skip to content

Commit

Permalink
fix(provider/tcp): rename 'invert' option to declarative 'closed'
Browse files Browse the repository at this point in the history
  • Loading branch information
isometry committed Apr 3, 2024
1 parent f095a4d commit de0daa7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/provider/tcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The TCP Provider is configured through the platform-health server's configuratio
* `name` (required): The name of the TCP service instance, used to identify the service in the health reports.
* `host` (required): The hostname or IP address of the TCP service to monitor.
* `port` (default: `80`): The port number of the TCP service to monitor.
* `invert` (default: `false`): Reverse logic to report "unhealthy" if port is open and "healthy" if it is closed.
* `closed` (default: `false`): Reverse logic to report "healthy" if port is closed and "unhealthy" if it is open.
* `timeout` (default: `1s`): The maximum time to wait for a connection to be established before timing out.

### Example
Expand Down
8 changes: 4 additions & 4 deletions pkg/provider/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type TCP struct {
Name string `mapstructure:"name"`
Host string `mapstructure:"host"`
Port int `mapstructure:"port" default:"80"`
Invert bool `mapstructure:"invert" default:"false"`
Closed bool `mapstructure:"closed" default:"false"`
Timeout time.Duration `mapstructure:"timeout" default:"1s"`
}

Expand All @@ -33,8 +33,8 @@ func (i *TCP) LogValue() slog.Value {
slog.String("name", i.Name),
slog.String("host", i.Host),
slog.Int("port", i.Port),
slog.Bool("closed", i.Closed),
slog.Any("timeout", i.Timeout),
slog.Bool("invert", i.Invert),
}
return slog.GroupValue(logAttr...)
}
Expand Down Expand Up @@ -68,14 +68,14 @@ func (i *TCP) GetHealth(ctx context.Context) *ph.HealthCheckResponse {
dialer := &net.Dialer{}
conn, err := dialer.DialContext(ctx, "tcp", address)
if err != nil {
if i.Invert {
if i.Closed {
return component.Healthy()
} else {
return component.Unhealthy(err.Error())
}
} else {
_ = conn.Close()
if i.Invert {
if i.Closed {
return component.Unhealthy("port open")
} else {
return component.Healthy()
Expand Down
12 changes: 6 additions & 6 deletions pkg/provider/tcp/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestTCP(t *testing.T) {
tests := []struct {
name string
port int
invert bool
closed bool
timeout time.Duration
expected ph.Status
}{
Expand All @@ -40,14 +40,14 @@ func TestTCP(t *testing.T) {
expected: ph.Status_HEALTHY,
},
{
name: "Port closed",
name: "Port closed, wanted open",
port: 1,
expected: ph.Status_UNHEALTHY,
},
{
name: "Port closed, expect failure",
name: "Port closed, wanted closed",
port: 1,
invert: true,
closed: true,
expected: ph.Status_HEALTHY,
},
{
Expand All @@ -59,7 +59,7 @@ func TestTCP(t *testing.T) {
{
name: "Expected timeout",
port: port,
invert: true,
closed: true,
timeout: time.Nanosecond,
expected: ph.Status_HEALTHY,
},
Expand All @@ -71,7 +71,7 @@ func TestTCP(t *testing.T) {
Name: tt.name,
Host: "localhost",
Port: tt.port,
Invert: tt.invert,
Closed: tt.closed,
Timeout: tt.timeout,
}
instance.SetDefaults()
Expand Down

0 comments on commit de0daa7

Please sign in to comment.