Skip to content

Commit

Permalink
Gossip golint fixes under gossip/util
Browse files Browse the repository at this point in the history
Fixed all golint warnings under gossip/gossip/util

Change-Id: Ia68cdb4eee9e0737168b7e4a76b84e16c81f6602
Signed-off-by: Yacov Manevich <yacovm@il.ibm.com>
  • Loading branch information
yacovm committed Jan 24, 2017
1 parent 2be1717 commit 2e3083a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion gossip/comm/comm_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewCommInstanceWithServer(port int, idMapper identity.Mapper, peerIdentity
selfCertHash: certHash,
PKIID: idMapper.GetPKIidOfCert(peerIdentity),
idMapper: idMapper,
logger: util.GetLogger(util.LOGGING_COMM_MODULE, fmt.Sprintf("%d", port)),
logger: util.GetLogger(util.LoggingCommModule, fmt.Sprintf("%d", port)),
peerIdentity: peerIdentity,
opts: dialOpts,
port: port,
Expand Down
2 changes: 1 addition & 1 deletion gossip/discovery/discovery_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewDiscoveryService(bootstrapPeers []string, self NetworkMember, comm CommS
lock: &sync.RWMutex{},
toDieChan: make(chan struct{}, 1),
toDieFlag: int32(0),
logger: util.GetLogger(util.LOGGING_DISCOVERY_MODULE, self.Endpoint),
logger: util.GetLogger(util.LoggingDiscoveryModule, self.Endpoint),
}

d.logger.SetLevel(logging.WARNING)
Expand Down
2 changes: 1 addition & 1 deletion gossip/gossip/gossip_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func NewGossipService(conf *Config, s *grpc.Server, secAdvisor api.SecurityAdvis
var c comm.Comm
var err error
idMapper := identity.NewIdentityMapper(mcs)
lgr := util.GetLogger(util.LOGGING_GOSSIP_MODULE, conf.ID)
lgr := util.GetLogger(util.LoggingGossipModule, conf.ID)
if s == nil {
c, err = createCommWithServer(conf.BindPort, idMapper, selfIdentity)
} else {
Expand Down
24 changes: 17 additions & 7 deletions gossip/util/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ import (
)

const (
LOGGING_MESSAGE_BUFF_MODULE = "mbuff"
LOGGING_EMITTER_MODULE = "emitter"
LOGGING_GOSSIP_MODULE = "gossip"
LOGGING_DISCOVERY_MODULE = "discovery"
LOGGING_COMM_MODULE = "comm"
// LoggingMessageBuffModule defines the module for logging message buffer
LoggingMessageBuffModule = "mbuff"
// LoggingEmitterModule defines the module for logging emitter
LoggingEmitterModule = "emitter"
// LoggingGossipModule defines the module for logging gossip
LoggingGossipModule = "gossip"
// LoggingDiscoveryModule defines the module for logging discovery
LoggingDiscoveryModule = "discovery"
// LoggingCommModule defines the module for logging communication
LoggingCommModule = "comm"
)

var loggersByModules = make(map[string]*Logger)
Expand All @@ -48,25 +53,30 @@ func init() {
grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
}

// Logger defines a logger for gossip
type Logger struct {
logging.Logger
module string
}

// SetDefaultFormat sets the formatter for the gossip logger
func SetDefaultFormat(formatStr string) {
format = logging.MustStringFormatter(formatStr)
}

// SetDefaultLoggingLevel sets the default logging level for the gossip logger
func SetDefaultLoggingLevel(level logging.Level) {
defaultLevel = level
}

// SetLevel sets the level for the logger
func (l *Logger) SetLevel(lvl logging.Level) {
logging.SetLevel(lvl, l.module)
}

func GetLogger(module string, peerId string) *Logger {
module = module + "-" + peerId
// GetLogger returns a logger for given gossip module and peerID
func GetLogger(module string, peerID string) *Logger {
module = module + "-" + peerID
lock.Lock()
defer lock.Unlock()

Expand Down
19 changes: 17 additions & 2 deletions gossip/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"sync"
)

// Equals returns whether a and b are the same
type Equals func(a interface{}, b interface{}) bool

func init() {
rand.Seed(42)
}

// IndexInSlice returns the index of given object o in array
func IndexInSlice(array interface{}, o interface{}, equals Equals) int {
arr := reflect.ValueOf(array)
for i := 0; i < arr.Len(); i++ {
Expand All @@ -44,6 +46,8 @@ func numbericEqual(a interface{}, b interface{}) bool {
return a.(int) == b.(int)
}

// GetRandomIndices returns a slice of random indices
// from 0 to given highestIndex
func GetRandomIndices(indiceCount, highestIndex int) []int {
if highestIndex+1 < indiceCount {
return nil
Expand All @@ -67,36 +71,43 @@ func GetRandomIndices(indiceCount, highestIndex int) []int {
return indices
}

// Abs returns abs(a-b)
func Abs(a, b uint64) uint64 {
if a > b {
return a - b
} else {
return b - a
}
return b - a
}

// Set is a generic and thread-safe
// set container
type Set struct {
items map[interface{}]struct{}
lock *sync.RWMutex
}

// NewSet returns a new set
func NewSet() *Set {
return &Set{lock: &sync.RWMutex{}, items: make(map[interface{}]struct{})}
}

// Add adds given item to the set
func (s *Set) Add(item interface{}) {
s.lock.Lock()
defer s.lock.Unlock()
s.items[item] = struct{}{}
}

// Exists returns true whether given item is in the set
func (s *Set) Exists(item interface{}) bool {
s.lock.RLock()
defer s.lock.RUnlock()
_, exists := s.items[item]
return exists
}

// ToArray returns a slice with items
// at the point in time the method was invoked
func (s *Set) ToArray() []interface{} {
s.lock.RLock()
defer s.lock.RUnlock()
Expand All @@ -109,12 +120,14 @@ func (s *Set) ToArray() []interface{} {
return a
}

// Clear removes all elements from set
func (s *Set) Clear() {
s.lock.Lock()
defer s.lock.Unlock()
s.items = make(map[interface{}]struct{})
}

// Remove removes a given item from the set
func (s *Set) Remove(item interface{}) {
s.lock.Lock()
defer s.lock.Unlock()
Expand All @@ -126,6 +139,8 @@ type goroutine struct {
Stack []string
}

// PrintStackTrace prints to stdout
// all goroutines
func PrintStackTrace() {
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
Expand Down

0 comments on commit 2e3083a

Please sign in to comment.