Address data race in pkg/corredor (healthcheck)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user