Skip to content

Commit

Permalink
fix(crud): Fixing hard coded duration for start handler
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Dec 30, 2022
1 parent 97bfa7a commit a7e0ad8
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/crud/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func New(config Config, storageApp absto.Storage, filteredStorage absto.Storage,
}

func (a App) Start(ctx context.Context) {
if err := a.exclusiveApp.Execute(ctx, "fibr:mutex:start", func(ctx context.Context) error {
if err := a.exclusiveApp.Execute(ctx, "fibr:mutex:start", time.Hour, func(ctx context.Context) error {
a.start(ctx)
return nil
}); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions pkg/exclusive/exclusive.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/redis"
)

const SemaphoreDuration = time.Second * 10
const Duration = time.Second * 10

type App struct {
redisClient redis.App
Expand All @@ -19,13 +19,13 @@ func New(redisClient redis.App) App {
}
}

func (a App) Execute(ctx context.Context, name string, action func(context.Context) error) error {
func (a App) Execute(ctx context.Context, name string, duration time.Duration, action func(context.Context) error) error {
if !a.redisClient.Enabled() {
return action(ctx)
}

exclusive:
acquired, err := a.redisClient.Exclusive(ctx, name, SemaphoreDuration, action)
acquired, err := a.redisClient.Exclusive(ctx, name, duration, action)
if err != nil {
return err
}
Expand All @@ -38,10 +38,10 @@ exclusive:
return nil
}

func (a App) Try(ctx context.Context, name string, action func(context.Context) error) (bool, error) {
func (a App) Try(ctx context.Context, name string, duration time.Duration, action func(context.Context) error) (bool, error) {
if !a.redisClient.Enabled() {
return true, action(ctx)
}

return a.redisClient.Exclusive(ctx, name, SemaphoreDuration, action)
return a.redisClient.Exclusive(ctx, name, duration, action)
}
3 changes: 2 additions & 1 deletion pkg/exif/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

absto "github.com/ViBiOh/absto/pkg/model"
exas "github.com/ViBiOh/exas/pkg/model"
"github.com/ViBiOh/fibr/pkg/exclusive"
"github.com/ViBiOh/fibr/pkg/provider"
"github.com/ViBiOh/httputils/v4/pkg/logger"
)
Expand All @@ -31,7 +32,7 @@ func WithDescription(description string) MetadataOption {
func (a App) update(ctx context.Context, item absto.Item, opts ...MetadataOption) (provider.Metadata, error) {
var metadata provider.Metadata

return metadata, a.exclusiveApp.Execute(ctx, "fibr:mutex:"+item.ID, func(ctx context.Context) error {
return metadata, a.exclusiveApp.Execute(ctx, "fibr:mutex:"+item.ID, exclusive.Duration, func(ctx context.Context) error {
var err error

metadata, err := a.GetMetadataFor(ctx, item)
Expand Down
3 changes: 2 additions & 1 deletion pkg/search/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"

absto "github.com/ViBiOh/absto/pkg/model"
"github.com/ViBiOh/fibr/pkg/exclusive"
"github.com/ViBiOh/fibr/pkg/provider"
)

Expand Down Expand Up @@ -64,7 +65,7 @@ func (a App) Delete(ctx context.Context, item absto.Item, name string) error {
}

func (a App) update(ctx context.Context, item absto.Item, opts ...SearchesOption) error {
return a.exclusiveApp.Execute(ctx, "fibr:mutex:"+item.ID, func(ctx context.Context) error {
return a.exclusiveApp.Execute(ctx, "fibr:mutex:"+item.ID, exclusive.Duration, func(ctx context.Context) error {
searches, err := a.load(ctx, item)
if err != nil {
return fmt.Errorf("load: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/share/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (a *App) List() (output []provider.Share) {
func (a *App) Create(ctx context.Context, filepath string, edit, story bool, password string, isDir bool, duration time.Duration) (string, error) {
var id string

_, err := a.Exclusive(ctx, "create", exclusive.SemaphoreDuration, func(ctx context.Context) error {
_, err := a.Exclusive(ctx, "create", exclusive.Duration, func(ctx context.Context) error {
var err error
id, err = a.generateID()
if err != nil {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (a *App) Create(ctx context.Context, filepath string, edit, story bool, pas
}

func (a *App) Delete(ctx context.Context, id string) error {
_, err := a.Exclusive(ctx, id, exclusive.SemaphoreDuration, func(_ context.Context) error {
_, err := a.Exclusive(ctx, id, exclusive.Duration, func(_ context.Context) error {
return a.delete(ctx, id)
})

Expand Down
4 changes: 2 additions & 2 deletions pkg/share/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (a *App) EventConsumer(ctx context.Context, e provider.Event) {
}

func (a *App) renameItem(ctx context.Context, old, new absto.Item) error {
_, err := a.Exclusive(ctx, old.ID, exclusive.SemaphoreDuration, func(ctx context.Context) error {
_, err := a.Exclusive(ctx, old.ID, exclusive.Duration, func(ctx context.Context) error {
for id, share := range a.shares {
if strings.HasPrefix(share.Path, old.Pathname) {
share.Path = strings.Replace(share.Path, old.Pathname, new.Pathname, 1)
Expand All @@ -47,7 +47,7 @@ func (a *App) renameItem(ctx context.Context, old, new absto.Item) error {
}

func (a *App) deleteItem(ctx context.Context, item absto.Item) error {
_, err := a.Exclusive(ctx, item.ID, exclusive.SemaphoreDuration, func(_ context.Context) error {
_, err := a.Exclusive(ctx, item.ID, exclusive.Duration, func(_ context.Context) error {
for id, share := range a.shares {
if strings.HasPrefix(share.Path, item.Pathname) {
if err := a.delete(ctx, id); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func New(config Config, storageApp absto.Storage, amqpClient *amqp.Client, exclu
}, nil
}

func (a *App) Exclusive(ctx context.Context, name string, _ time.Duration, action func(ctx context.Context) error) (bool, error) {
return a.exclusiveApp.Try(ctx, "fibr:mutex:"+name, func(ctx context.Context) error {
func (a *App) Exclusive(ctx context.Context, name string, duration time.Duration, action func(ctx context.Context) error) (bool, error) {
return a.exclusiveApp.Try(ctx, "fibr:mutex:"+name, duration, func(ctx context.Context) error {
a.mutex.Lock()
defer a.mutex.Unlock()

Expand Down Expand Up @@ -106,7 +106,7 @@ func (a *App) Start(ctx context.Context) {
}).OnSignal(syscall.SIGUSR1)

if a.amqpClient != nil {
purgeCron.Exclusive(a, "purge", exclusive.SemaphoreDuration)
purgeCron.Exclusive(a, "purge", exclusive.Duration)
}

purgeCron.Start(ctx, a.cleanShares)
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func New(config Config, storageApp absto.Storage, prometheusRegisterer prometheu
}

func (a *App) Exclusive(ctx context.Context, name string, action func(ctx context.Context) error) error {
return a.exclusiveApp.Execute(ctx, "fibr:mutex:"+name, func(ctx context.Context) error {
return a.exclusiveApp.Execute(ctx, "fibr:mutex:"+name, exclusive.Duration, func(ctx context.Context) error {
a.mutex.Lock()
defer a.mutex.Unlock()

Expand Down

0 comments on commit a7e0ad8

Please sign in to comment.