Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Check to send result to closed channel #85

Merged
merged 2 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ import (
"net/http"
"time"

"github.com/hellofresh/health-go/v4"
healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
"github.com/hellofresh/health-go/v5"
healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
// add some checks on instance creation
h, _ := health.New(health.WithComponent(health.Component{
Name: "myservice",
Version: "v1.0",
}), health.WithChecks(health.Config{
Name: "myservice",
Version: "v1.0",
}), health.WithChecks(health.Config{
Name: "rabbitmq",
Timeout: time.Second * 5,
SkipOnErr: true,
Expand Down Expand Up @@ -82,16 +82,16 @@ import (
"time"

"github.com/go-chi/chi"
"github.com/hellofresh/health-go/v4"
healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
"github.com/hellofresh/health-go/v5"
healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
// add some checks on instance creation
h, _ := health.New(health.WithComponent(health.Component{
Name: "myservice",
Version: "v1.0",
}), health.WithChecks(health.Config{
Name: "myservice",
Version: "v1.0",
}), health.WithChecks(health.Config{
Name: "rabbitmq",
Timeout: time.Second * 5,
SkipOnErr: true,
Expand Down
8 changes: 4 additions & 4 deletions _examples/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"net/http"
"time"

"github.com/hellofresh/health-go/v4"
healthHttp "github.com/hellofresh/health-go/v4/checks/http"
healthMySql "github.com/hellofresh/health-go/v4/checks/mysql"
healthPg "github.com/hellofresh/health-go/v4/checks/postgres"
"github.com/hellofresh/health-go/v5"
healthHttp "github.com/hellofresh/health-go/v5/checks/http"
healthMySql "github.com/hellofresh/health-go/v5/checks/mysql"
healthPg "github.com/hellofresh/health-go/v5/checks/postgres"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion checks/rabbitmq/aliveness_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/hellofresh/health-go/v4/checks/http"
"github.com/hellofresh/health-go/v5/checks/http"
)

const httpURLEnv = "HEALTH_GO_MQ_URL"
Expand Down
8 changes: 4 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"net/http"
"time"

"github.com/hellofresh/health-go/v4"
healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
"github.com/hellofresh/health-go/v5"
healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
Expand Down Expand Up @@ -77,8 +77,8 @@ import (
"time"

"github.com/go-chi/chi"
"github.com/hellofresh/health-go/v4"
healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
"github.com/hellofresh/health-go/v5"
healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/hellofresh/health-go/v4
module github.com/hellofresh/health-go/v5

go 1.18

Expand Down
12 changes: 2 additions & 10 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,10 @@ func (h *Health) Measure(ctx context.Context) Check {
}()

resCh := make(chan error)
defer close(resCh)

go func() {
res := c.Check(ctx)

select {
case <-resCh:
// channel is already closed
return
default:
resCh <- res
}
resCh <- c.Check(ctx)
defer close(resCh)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasmdrs , the defer should be before the Check, no? if the Check does not end, this will never get closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @grzn
Changing the order of the defer would not change anything, endless checks will always result in leaky goroutines.

The changes done here will make open channels not be closed until those goroutines are finished.
It's still not the best solution, but ensures that there's no race condition between timeouts and checks

I'll work to improve this entire logic a bit, but that will be out of the scope for the issue

}()

select {
Expand Down