Skip to content
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
7 changes: 6 additions & 1 deletion billing/checkout/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type Service struct {
paymentMethodConfig []billing.PaymentMethodConfig

syncJob *cron.Cron
syncJobMu sync.Mutex
mu sync.Mutex
syncDelay time.Duration
}
Expand Down Expand Up @@ -148,6 +149,9 @@ func (s *Service) Init(ctx context.Context) error {
if s.syncDelay == time.Duration(0) {
return nil
}

s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
}
Expand All @@ -169,9 +173,10 @@ func (s *Service) Init(ctx context.Context) error {
}

func (s *Service) Close() error {
s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
return s.syncJob.Stop().Err()
}
return nil
}
Expand Down
35 changes: 35 additions & 0 deletions billing/checkout/service_concurrent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package checkout

import (
"context"
"io"
"log/slog"
"sync"
"testing"
"time"
)

// TestService_InitClose_Concurrent guards against an unsynchronized syncJob field.
// Two goroutines is the minimum needed to surface the race under `go test -race`.
func TestService_InitClose_Concurrent(t *testing.T) {
s := &Service{
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
syncDelay: time.Hour,
}

var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Init(context.Background()); err != nil {
t.Errorf("Init: %v", err)
}
}()
}
wg.Wait()

if err := s.Close(); err != nil {
t.Errorf("Close: %v", err)
}
}
7 changes: 6 additions & 1 deletion billing/customer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Service struct {
creditService CreditService

syncJob *cron.Cron
syncJobMu sync.Mutex
mu sync.Mutex
syncDelay time.Duration
}
Expand Down Expand Up @@ -358,6 +359,9 @@ func (s *Service) Init(ctx context.Context) error {
if s.syncDelay == time.Duration(0) {
return nil
}

s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
}
Expand All @@ -378,9 +382,10 @@ func (s *Service) Init(ctx context.Context) error {
}

func (s *Service) Close() error {
s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
return s.syncJob.Stop().Err()
}
return nil
}
Expand Down
35 changes: 35 additions & 0 deletions billing/customer/service_concurrent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package customer

import (
"context"
"io"
"log/slog"
"sync"
"testing"
"time"
)

// TestService_InitClose_Concurrent guards against an unsynchronized syncJob field.
// Two goroutines is the minimum needed to surface the race under `go test -race`.
func TestService_InitClose_Concurrent(t *testing.T) {
s := &Service{
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
syncDelay: time.Hour,
}

var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Init(context.Background()); err != nil {
t.Errorf("Init: %v", err)
}
}()
}
wg.Wait()

if err := s.Close(); err != nil {
t.Errorf("Close: %v", err)
}
}
49 changes: 31 additions & 18 deletions billing/invoice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Service struct {
locker Locker

syncJob *cron.Cron
syncJobMu sync.Mutex
mu sync.Mutex
syncDelay time.Duration

Expand Down Expand Up @@ -112,23 +113,8 @@ func NewService(logger *slog.Logger, stripeClient *client.API, invoiceRepository
}

func (s *Service) Init(ctx context.Context) error {
if s.syncDelay != time.Duration(0) {
if s.syncJob != nil {
s.syncJob.Stop()
}
s.syncJob = cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.DefaultLogger),
cron.Recover(cron.DefaultLogger),
))

if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", s.syncDelay.String()), func() {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
s.backgroundSync(ctx)
}); err != nil {
return err
}
s.syncJob.Start()
if err := s.initSyncJob(ctx); err != nil {
return err
}

if s.creditOverdraftProduct != "" {
Expand Down Expand Up @@ -156,9 +142,36 @@ func (s *Service) Init(ctx context.Context) error {
return nil
}

func (s *Service) initSyncJob(ctx context.Context) error {
if s.syncDelay == time.Duration(0) {
return nil
}

s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
}
s.syncJob = cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.DefaultLogger),
cron.Recover(cron.DefaultLogger),
))
if _, err := s.syncJob.AddFunc(fmt.Sprintf("@every %s", s.syncDelay.String()), func() {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
s.backgroundSync(ctx)
}); err != nil {
return err
}
s.syncJob.Start()
return nil
}

func (s *Service) Close() error {
s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
return s.syncJob.Stop().Err()
<-s.syncJob.Stop().Done()
}
return nil
}
Expand Down
35 changes: 35 additions & 0 deletions billing/invoice/service_concurrent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package invoice

import (
"context"
"io"
"log/slog"
"sync"
"testing"
"time"
)

// TestService_InitClose_Concurrent guards against an unsynchronized syncJob field.
// Two goroutines is the minimum needed to surface the race under `go test -race`.
func TestService_InitClose_Concurrent(t *testing.T) {
s := &Service{
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
syncDelay: time.Hour,
}

var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Init(context.Background()); err != nil {
t.Errorf("Init: %v", err)
}
}()
}
wg.Wait()

if err := s.Close(); err != nil {
t.Errorf("Close: %v", err)
}
}
13 changes: 9 additions & 4 deletions billing/subscription/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ type Service struct {
productService ProductService
creditService CreditService

syncJob *cron.Cron
mu sync.Mutex
config billing.Config
syncJob *cron.Cron
syncJobMu sync.Mutex
mu sync.Mutex
config billing.Config
}

func NewService(logger *slog.Logger, stripeClient *client.API, config billing.Config, repository Repository,
Expand Down Expand Up @@ -116,6 +117,9 @@ func (s *Service) Init(ctx context.Context) error {
if syncDelay == time.Duration(0) {
return nil
}

s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
}
Expand All @@ -136,9 +140,10 @@ func (s *Service) Init(ctx context.Context) error {
}

func (s *Service) Close() error {
s.syncJobMu.Lock()
defer s.syncJobMu.Unlock()
if s.syncJob != nil {
<-s.syncJob.Stop().Done()
return s.syncJob.Stop().Err()
}
return nil
}
Expand Down
37 changes: 37 additions & 0 deletions billing/subscription/service_concurrent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package subscription

import (
"context"
"io"
"log/slog"
"sync"
"testing"
"time"

"github.com/raystack/frontier/billing"
)

// TestService_InitClose_Concurrent guards against an unsynchronized syncJob field.
// Two goroutines is the minimum needed to surface the race under `go test -race`.
func TestService_InitClose_Concurrent(t *testing.T) {
s := &Service{
log: slog.New(slog.NewTextHandler(io.Discard, nil)),
config: billing.Config{RefreshInterval: billing.RefreshInterval{Subscription: time.Hour}},
}

var wg sync.WaitGroup
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := s.Init(context.Background()); err != nil {
t.Errorf("Init: %v", err)
}
}()
}
wg.Wait()

if err := s.Close(); err != nil {
t.Errorf("Close: %v", err)
}
}
8 changes: 5 additions & 3 deletions internal/store/blob/resources_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"context"
"fmt"
"io"
"log/slog"
"strings"
"sync"
"time"

"github.com/raystack/frontier/core/namespace"
"github.com/raystack/frontier/core/resource"

"log/slog"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
Expand Down Expand Up @@ -63,7 +62,10 @@ func (repo *ResourcesRepository) GetAll(ctx context.Context) ([]resource.YAML, e
}

err := repo.refresh(ctx)
return repo.cached, err
repo.mu.Lock()
currentCache = repo.cached
repo.mu.Unlock()
return currentCache, err
}

func (repo *ResourcesRepository) GetRelationsForNamespace(ctx context.Context, namespaceID string) (map[string]bool, error) {
Expand Down
Loading