Skip to content

Commit

Permalink
GO-61: Reduntan mutex removed (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykiselev authored and Frozen committed Feb 13, 2019
1 parent ca24bab commit cd88474
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/wmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
```
Expand Down
35 changes: 6 additions & 29 deletions cmd/wmd/internal/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
)

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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()
}
}
}
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions cmd/wmd/wmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -215,7 +215,6 @@ func run() error {
return err
}
synchronizerDone = s.Done()
s.Resume()
}

if apiDone != nil {
Expand Down

0 comments on commit cd88474

Please sign in to comment.