Skip to content

Commit

Permalink
set readiness once only
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <[email protected]>
  • Loading branch information
Kavindu-Dodan committed Mar 3, 2023
1 parent a04c0b8 commit 25e8051
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
22 changes: 10 additions & 12 deletions pkg/sync/grpc/grpc_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
constantBackOffDelay = 60
)

var once msync.Once

type Sync struct {
Target string
ProviderID string
Expand Down Expand Up @@ -84,37 +86,28 @@ func (g *Sync) Init(ctx context.Context) error {
}

func (g *Sync) IsReady() bool {
g.Mux.RLock()
defer g.Mux.RUnlock()
return g.ready
}

func (g *Sync) setReady(val bool) {
g.Mux.Lock()
defer g.Mux.Unlock()
g.ready = val
}

func (g *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
// initial stream listening
g.setReady(true)
err := g.handleFlagSync(g.syncClient, dataSync)
if err == nil {
return nil
}

g.Logger.Warn(fmt.Sprintf("error with stream listener: %s", err.Error()))

// retry connection establishment
for {
g.setReady(false)
syncClient, ok := g.connectWithRetry(ctx)
if !ok {
// We shall exit
return nil
}
g.setReady(true)

err = g.handleFlagSync(syncClient, dataSync)
if err != nil {
g.setReady(false)
g.Logger.Warn(fmt.Sprintf("error with stream listener: %s", err.Error()))
continue
}
Expand Down Expand Up @@ -168,6 +161,11 @@ func (g *Sync) connectWithRetry(

// handleFlagSync wraps the stream listening and push updates through dataSync channel
func (g *Sync) handleFlagSync(stream syncv1grpc.FlagSyncService_SyncFlagsClient, dataSync chan<- sync.DataSync) error {
// Set ready state once only
once.Do(func() {
g.ready = true
})

for {
data, err := stream.Recv()
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/sync/http/http_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func (hs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
if err != nil {
return err
}

// Set ready state
hs.ready = true

_ = hs.Cron.AddFunc("*/5 * * * *", func() {
Expand Down Expand Up @@ -151,18 +153,16 @@ func (hs *Sync) generateSha(body []byte) string {

func (hs *Sync) Fetch(ctx context.Context) (string, error) {
if hs.URI == "" {
hs.ready = false
return "", errors.New("no HTTP URL string set")
}

body, err := hs.fetchBodyFromURL(ctx, hs.URI)
if err != nil {
hs.ready = false
return "", err
}
if len(body) != 0 {
hs.LastBodySHA = hs.generateSha(body)
}
hs.ready = true

return string(body), nil
}
8 changes: 0 additions & 8 deletions pkg/sync/http/http_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
bearerToken string
lastBodySHA string
handleResponse func(*testing.T, Sync, string, error)
ready bool
}{
"success": {
setup: func(t *testing.T, client *syncmock.MockClient) {
Expand All @@ -84,7 +83,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
t.Errorf("expected fetched to be: '%s', got: '%s'", expected, fetched)
}
},
ready: true,
},
"return an error if no uri": {
setup: func(t *testing.T, client *syncmock.MockClient) {},
Expand All @@ -93,7 +91,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
t.Error("expected err, got nil")
}
},
ready: false,
},
"update last body sha": {
setup: func(t *testing.T, client *syncmock.MockClient) {
Expand All @@ -115,7 +112,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
)
}
},
ready: true,
},
"authorization header": {
setup: func(t *testing.T, client *syncmock.MockClient) {
Expand All @@ -137,7 +133,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
)
}
},
ready: true,
},
}

Expand All @@ -156,9 +151,6 @@ func TestHTTPSync_Fetch(t *testing.T) {
}

fetched, err := httpSync.Fetch(context.Background())
if httpSync.IsReady() != tt.ready {
t.Errorf("expected httpSync.ready to be: '%v', got: '%v'", tt.ready, httpSync.ready)
}
tt.handleResponse(t, httpSync, fetched, err)
})
}
Expand Down

0 comments on commit 25e8051

Please sign in to comment.