From cd884747a4d5f34449fbf95825862d3902ef6448 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Wed, 13 Feb 2019 14:36:26 +0300 Subject: [PATCH] GO-61: Reduntan mutex removed (#75) --- cmd/wmd/README.md | 2 +- cmd/wmd/internal/synchronizer.go | 35 ++++++-------------------------- cmd/wmd/wmd.go | 7 +++---- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/cmd/wmd/README.md b/cmd/wmd/README.md index d98e38723..eb58302dc 100644 --- a/cmd/wmd/README.md +++ b/cmd/wmd/README.md @@ -34,7 +34,7 @@ usage: wmd [flags] -db Path to data base folder. No default value. -matcher Matcher's public key in form of Base58 string. Defaults to 7kPFrHDiGw1rCm7LPszuECwWYL3dMf6iMifLRDJQZMzy. -scheme Blockchain scheme symbol. Defaults to 'W'. - -symbols Path to file of symbols substitutions. No default value. + -symbols Path to file of symbol substitutions. No default value. -rollback The height to rollback to before importing a blockchain file or staring the synchronization. Default value is 0 (no rollback). ``` diff --git a/cmd/wmd/internal/synchronizer.go b/cmd/wmd/internal/synchronizer.go index f8fa15ffd..1a085161b 100644 --- a/cmd/wmd/internal/synchronizer.go +++ b/cmd/wmd/internal/synchronizer.go @@ -13,7 +13,6 @@ import ( "net/http" "net/url" "strings" - "sync" "time" ) @@ -25,8 +24,6 @@ type Synchronizer struct { storage *state.Storage scheme byte matcher crypto.PublicKey - mu *sync.RWMutex - active bool ticker *time.Ticker lag int } @@ -40,27 +37,11 @@ func NewSynchronizer(interrupt <-chan struct{}, log *zap.SugaredLogger, storage t := time.NewTicker(d) log.Infof("Synchronization interval set to %v", d) done := make(chan struct{}) - s := Synchronizer{interrupt: interrupt, done: done, client: c, log: log, storage: storage, scheme: scheme, matcher: matcher, mu: new(sync.RWMutex), active: false, ticker: t, lag: lag} + s := Synchronizer{interrupt: interrupt, done: done, client: c, log: log, storage: storage, scheme: scheme, matcher: matcher, ticker: t, lag: lag} go s.run() return &s, nil } -func (s *Synchronizer) Pause() { - s.mu.Lock() - if s.active { - s.active = false - } - s.mu.Unlock() -} - -func (s *Synchronizer) Resume() { - s.mu.Lock() - if !s.active { - s.active = true - } - s.mu.Unlock() -} - func (s *Synchronizer) Done() <-chan struct{} { return s.done } @@ -70,18 +51,11 @@ func (s *Synchronizer) run() { for { select { case <-s.interrupt: - s.log.Info("Shutting down synchronizer...") s.ticker.Stop() + s.log.Info("Shutting down synchronizer...") return case <-s.ticker.C: - if s.interrupted() { - return - } - s.mu.RLock() - if s.active { - s.synchronize() - } - s.mu.RUnlock() + s.synchronize() } } } @@ -107,6 +81,9 @@ func (s *Synchronizer) synchronize() { s.log.Errorf("Failed to synchronize with node: %v", err) return } + if s.interrupted() { + return + } if rh > lh { s.log.Infof("Local height %d, node height %d", lh, rh) ch, err := s.findLastCommonHeight(1, lh) diff --git a/cmd/wmd/wmd.go b/cmd/wmd/wmd.go index e5c48568b..6509645d9 100644 --- a/cmd/wmd/wmd.go +++ b/cmd/wmd/wmd.go @@ -38,7 +38,7 @@ func run() error { db = flag.String("db", "", "Path to data base folder. No default value.") matcher = flag.String("matcher", "7kPFrHDiGw1rCm7LPszuECwWYL3dMf6iMifLRDJQZMzy", "Matcher's public key in form of Base58 string.") scheme = flag.String("scheme", "W", "Blockchain scheme symbol. Defaults to 'W'.") - symbolsFile = flag.String("symbols", "", "Path to file of symbols substitutions. No default value.") + symbolsFile = flag.String("symbols", "", "Path to file of symbol substitutions. No default value.") rollback = flag.Int("rollback", 0, "The height to rollback to before importing a blockchain file or staring the synchronization. Default value is 0 (no rollback).") profilerPort = flag.Int("profiler-port", 0, "Start HTTP profiler on given port (port must be between 1024 and 65535)") cpuProfileFile = flag.String("cpu-profile", "", "Write CPU profile to the specified file") @@ -165,10 +165,10 @@ func run() error { symbols, err := data.ImportSymbols(*symbolsFile) if err != nil { - log.Errorf("Failed to load symbols substitutions: %v", err) + log.Errorf("Failed to load symbol substitutions: %v", err) return nil } - log.Infof("Imported %d of symbols substitution", symbols.Count()) + log.Infof("Imported %d of symbol substitutions", symbols.Count()) h, err := storage.Height() if err != nil { @@ -215,7 +215,6 @@ func run() error { return err } synchronizerDone = s.Done() - s.Resume() } if apiDone != nil {