Skip to content

Commit

Permalink
rotator shant die
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Oct 21, 2021
1 parent d316157 commit 810d02b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
4 changes: 2 additions & 2 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ func (btc *ExchangeWallet) estimateSwap(lots, lotSize, feeSuggestion uint64, utx

sum, inputsSize, _, _, _, _, err := fund(utxos, enough)
if err != nil {
return nil, false, err
return nil, false, fmt.Errorf("error funding swap value %s: %w", amount(val), err)
}

reqFunds := calc.RequiredOrderFundsAlt(val, uint64(inputsSize), lots, nfo.SwapSizeBase, nfo.SwapSize, nfo.MaxFeeRate)
Expand Down Expand Up @@ -1123,7 +1123,7 @@ func (btc *ExchangeWallet) FundOrder(ord *asset.Order) (asset.Coins, []dex.Bytes

sum, size, coins, fundingCoins, redeemScripts, spents, err := fund(utxos, enough)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("error funding swap value of %s: %w", amount(ord.Value), err)
}

if btc.useSplitTx && !ord.Immediate {
Expand Down
38 changes: 8 additions & 30 deletions client/asset/btc/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"path/filepath"
"sort"
"sync"
"sync/atomic"
"time"

"decred.org/dcrdex/client/asset"
Expand Down Expand Up @@ -120,7 +121,7 @@ func createSPVWallet(privPass []byte, seed []byte, dbDir string, log dex.Logger,
netDir := filepath.Join(dbDir, net.Name)

if err := logNeutrino(netDir); err != nil {
log.Warnf("error initializing btcwallet+neutrino logging: %v", err)
return fmt.Errorf("error initializing btcwallet+neutrino logging: %v", err)
}

logDir := filepath.Join(netDir, logDirName)
Expand Down Expand Up @@ -162,14 +163,10 @@ func createSPVWallet(privPass []byte, seed []byte, dbDir string, log dex.Logger,
}

var (
// rotatorMtx protects the rotating file logger trackers.
rotatorMtx sync.Mutex
// loggerCount is a count of the file logger users. We'll Close the logger
// when the count goes to zero.
loggerCount uint32
// sharedRotator is the active rotating file logger. It must be Close'd when
// the last user is done logging.
sharedRotator *rotator.Rotator
// loggingInited will be set when the log rotator has been initilized.
// Because dcrd and dcrwallet have unsupervised goroutines that can continue
// to log after Shutdown, we cannot safely close a log rotator.
loggingInited uint32
)

// logRotator initializes a rotating file logger.
Expand All @@ -190,20 +187,14 @@ func logRotator(netDir string) (*rotator.Rotator, error) {
// The unlogNeutrino function should be invoked by callers of logNeutrino when
// they no longer need logging.
func logNeutrino(netDir string) error {
rotatorMtx.Lock()
defer rotatorMtx.Unlock()
if loggerCount > 0 {
loggerCount++
if !atomic.CompareAndSwapUint32(&loggingInited, 0, 1) {
return nil
}

logSpinner, err := logRotator(netDir)
if err != nil {
return fmt.Errorf("error initializing log rotator: %w", err)
}
loggerCount++

sharedRotator = logSpinner

backendLog := btclog.NewBackend(logWriter{logSpinner})

Expand All @@ -221,18 +212,6 @@ func logNeutrino(netDir string) error {
return nil
}

// unlogNeutrino decrements the count of rotating file logger users, and closes
// the file logger if appropriate.
func unlogNeutrino() error {
rotatorMtx.Lock()
defer rotatorMtx.Unlock()
loggerCount--
if loggerCount > 0 {
return nil
}
return sharedRotator.Close()
}

// spendingInput is added to a filterScanResult if a spending input is found.
type spendingInput struct {
txHash chainhash.Hash
Expand Down Expand Up @@ -708,7 +687,7 @@ func (w *spvWallet) sendWithSubtract(pkScript []byte, value, feeRate uint64) (*c

sum, inputsSize, _, fundingCoins, _, _, err := fund(utxos, enough)
if err != nil {
return nil, err
return nil, fmt.Errorf("error funding sendWithSubtract value of %s: %v", amount(value), err)
}

fees := (unfundedTxSize + uint64(inputsSize)) * feeRate
Expand Down Expand Up @@ -945,7 +924,6 @@ func (w *spvWallet) connect(ctx context.Context, wg *sync.WaitGroup) error {
wg.Add(1)
go func() {
defer wg.Done()
defer unlogNeutrino()
defer w.stop()

ticker := time.NewTicker(time.Minute * 20)
Expand Down

0 comments on commit 810d02b

Please sign in to comment.