Address data race in pkg/corredor (healthcheck)

This commit is contained in:
Denis Arh
2022-04-04 18:03:19 +02:00
parent add326fbf1
commit 194a71a12b
3 changed files with 32 additions and 4 deletions
+7 -4
View File
@@ -3,20 +3,23 @@ package corredor
import (
"context"
"fmt"
"google.golang.org/grpc/connectivity"
)
// Healtcheck for global
func Healthcheck(ctx context.Context) error {
if gCorredor == nil {
func Healthcheck(_ context.Context) error {
svc := Service()
if svc == nil {
return fmt.Errorf("uninitialized")
}
if !gCorredor.opt.Enabled {
if !svc.opt.Enabled {
return nil
}
if state := gCorredor.conn.GetState(); state != connectivity.Ready {
if state := svc.conn.GetState(); state != connectivity.Ready {
return fmt.Errorf("connection is %s", state)
}
+16
View File
@@ -0,0 +1,16 @@
package corredor
import (
"context"
"testing"
"github.com/cortezaproject/corteza-server/pkg/options"
"go.uber.org/zap"
)
// tested with
// go test -count 10 -race -run TestDataRace ./pkg/corredor/...
func TestDataRace(t *testing.T) {
go Setup(zap.NewNop(), options.CorredorOpt{})
go Healthcheck(context.Background())
}
+9
View File
@@ -119,6 +119,9 @@ var (
// Global corredor service
gCorredor *service
// Lock for accessing global service
gLock sync.RWMutex
// List of event types that can be used as iteration
// initiator
//
@@ -144,11 +147,17 @@ const (
)
func Service() *service {
gLock.RLock()
defer gLock.RUnlock()
return gCorredor
}
// Setup start connects to Corredor & initialize service
func Setup(logger *zap.Logger, opt options.CorredorOpt) (err error) {
gLock.Lock()
defer gLock.Unlock()
if gCorredor != nil {
// Prevent multiple initializations
return