Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using the worker's shutdown context for unlocking accounts #981

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Changes from 1 commit
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
50 changes: 16 additions & 34 deletions worker/rhpv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,14 @@ func withAccountLock(ctx context.Context, as AccountStore, id rhpv3.Account, hk
if err != nil {
return err
}
err = fn(acc)

defer func() {
select {
case <-ctx.Done():
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), time.Minute)
defer cancel()
default:
}
as.UnlockAccount(ctx, acc.ID, lockID)
}()
// unlock account
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
_ = as.UnlockAccount(ctx, acc.ID, lockID) // ignore error
cancel()

return fn(acc)
return nil
peterjan marked this conversation as resolved.
Show resolved Hide resolved
}

// Balance returns the account balance.
Expand Down Expand Up @@ -450,38 +445,25 @@ func (a *account) WithWithdrawal(ctx context.Context, amtFn func() (types.Curren

// execute amtFn
amt, err := amtFn()

// in case of an insufficient balance, we schedule a sync
if isBalanceInsufficient(err) {
// in case of an insufficient balance, we schedule a sync
if scheduleErr := a.scheduleSync(); scheduleErr != nil {
err = fmt.Errorf("%w; failed to set requiresSync flag on bus, error: %v", err, scheduleErr)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
err = errors.Join(err, a.as.ScheduleSync(ctx, a.id, a.host))
cancel()
}

// if an amount was returned, we withdraw it.
if withdrawErr := a.withdrawFromBalance(amt); withdrawErr != nil {
err = fmt.Errorf("%w; failed to withdraw from account, error: %v", err, withdrawErr)
// if an amount was returned, we withdraw it
if !amt.IsZero() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
err = errors.Join(err, a.as.AddBalance(ctx, a.id, a.host, new(big.Int).Neg(amt.Big())))
cancel()
}

return err
})
}

func (a *account) withdrawFromBalance(amt types.Currency) error {
if amt.IsZero() {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
return a.as.AddBalance(ctx, a.id, a.host, new(big.Int).Neg(amt.Big()))
}

func (a *account) scheduleSync() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
return a.as.ScheduleSync(ctx, a.id, a.host)
}

// deriveAccountKey derives an account plus key for a given host and worker.
// Each worker has its own account for a given host. That makes concurrency
// around keeping track of an accounts balance and refilling it a lot easier in
Expand Down
Loading