HAR-84: golint fix of utils/singleton

Signed-off-by: Leo Chen <leo@harmony.one>
pull/111/head
Leo Chen 6 years ago
parent 8595971083
commit 2bb8648784
  1. 6
      consensus/consensus.go
  2. 19
      utils/singleton.go
  3. 6
      utils/singleton_test.go

@ -88,7 +88,7 @@ type Consensus struct {
Log log.Logger Log log.Logger
uniqueIDInstance *utils.UniqueValidatorId uniqueIDInstance *utils.UniqueValidatorID
} }
// BlockConsensusStatus used to keep track of the consensus status of multiple blocks received so far // BlockConsensusStatus used to keep track of the consensus status of multiple blocks received so far
@ -173,7 +173,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *
} }
consensus.Log = log.New() consensus.Log = log.New()
consensus.uniqueIDInstance = utils.GetUniqueValidatorIdInstance() consensus.uniqueIDInstance = utils.GetUniqueValidatorIDInstance()
return &consensus return &consensus
} }
@ -242,7 +242,7 @@ func (consensus *Consensus) AddPeers(peers []p2p.Peer) int {
_, ok := consensus.validators.Load(utils.GetUniqueIdFromPeer(peer)) _, ok := consensus.validators.Load(utils.GetUniqueIdFromPeer(peer))
if !ok { if !ok {
if peer.ValidatorID == -1 { if peer.ValidatorID == -1 {
peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueId()) peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueID())
} }
consensus.validators.Store(utils.GetUniqueIdFromPeer(peer), peer) consensus.validators.Store(utils.GetUniqueIdFromPeer(peer), peer)
consensus.PublicKeys = append(consensus.PublicKeys, peer.PubKey) consensus.PublicKeys = append(consensus.PublicKeys, peer.PubKey)

@ -7,22 +7,25 @@ import (
"sync/atomic" "sync/atomic"
) )
type UniqueValidatorId struct { // UniqueValidatorID defines the structure of unique validator ID
uniqueId uint32 type UniqueValidatorID struct {
uniqueID uint32
} }
var instance *UniqueValidatorId var instance *UniqueValidatorID
var once sync.Once var once sync.Once
func GetUniqueValidatorIdInstance() *UniqueValidatorId { // GetUniqueValidatorIDInstance returns a singleton instance
func GetUniqueValidatorIDInstance() *UniqueValidatorID {
once.Do(func() { once.Do(func() {
instance = &UniqueValidatorId{ instance = &UniqueValidatorID{
uniqueId: 0, uniqueID: 0,
} }
}) })
return instance return instance
} }
func (s *UniqueValidatorId) GetUniqueId() uint32 { // GetUniqueID returns a unique ID and increment the internal variable
return atomic.AddUint32(&s.uniqueId, 1) func (s *UniqueValidatorID) GetUniqueID() uint32 {
return atomic.AddUint32(&s.uniqueID, 1)
} }

@ -6,10 +6,10 @@ import (
"time" "time"
) )
var NumThreads int = 20 var NumThreads = 20
func TestSingleton(t *testing.T) { func TestSingleton(t *testing.T) {
si := GetUniqueValidatorIdInstance() si := GetUniqueValidatorIDInstance()
var wg sync.WaitGroup var wg sync.WaitGroup
t.Log("unique ID provided by singleton instance") t.Log("unique ID provided by singleton instance")
@ -18,7 +18,7 @@ func TestSingleton(t *testing.T) {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
t.Logf("id:%v\n", si.GetUniqueId()) t.Logf("id:%v\n", si.GetUniqueID())
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
}() }()

Loading…
Cancel
Save