Skip to content

Commit

Permalink
fixies
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Mar 22, 2015
1 parent 1901d0b commit 14ba867
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
20 changes: 12 additions & 8 deletions config.go
Expand Up @@ -42,7 +42,7 @@ type ExponentialStrategy struct {
}

// Calculate returns a duration of time: 2 ^ attempt
func (s ExponentialStrategy) Calculate(attempt int) time.Duration {
func (s *ExponentialStrategy) Calculate(attempt int) time.Duration {
backoffDuration := s.cfg.BackoffMultiplier *
time.Duration(math.Pow(2, float64(attempt)))
if backoffDuration > s.cfg.MaxBackoffDuration {
Expand All @@ -51,7 +51,7 @@ func (s ExponentialStrategy) Calculate(attempt int) time.Duration {
return backoffDuration
}

func (s ExponentialStrategy) setConfig(cfg *Config) {
func (s *ExponentialStrategy) setConfig(cfg *Config) {
s.cfg = cfg
}

Expand All @@ -64,9 +64,12 @@ type FullJitterStrategy struct {
}

// Calculate returns a random duration of time [0, 2 ^ attempt]
func (s FullJitterStrategy) Calculate(attempt int) time.Duration {
func (s *FullJitterStrategy) Calculate(attempt int) time.Duration {
// lazily initialize the RNG
s.rngOnce.Do(func() {
if s.rng != nil {
return
}
s.rng = rand.New(rand.NewSource(time.Now().UnixNano()))
})

Expand All @@ -78,7 +81,7 @@ func (s FullJitterStrategy) Calculate(attempt int) time.Duration {
return time.Duration(s.rng.Intn(int(backoffDuration)))
}

func (s FullJitterStrategy) setConfig(cfg *Config) {
func (s *FullJitterStrategy) setConfig(cfg *Config) {
s.cfg = cfg
}

Expand Down Expand Up @@ -180,9 +183,10 @@ type Config struct {
//
// This must be used to initialize Config structs. Values can be set directly, or through Config.Set()
func NewConfig() *Config {
c := &Config{}
c.configHandlers = append(c.configHandlers, &structTagsConfig{}, &tlsConfig{})
c.initialized = true
c := &Config{
configHandlers: []configHandler{&structTagsConfig{}, &tlsConfig{}},
initialized: true,
}
if err := c.setDefaults(); err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -327,7 +331,7 @@ func (h *structTagsConfig) SetDefaults(c *Config) error {
log.Fatalf("ERROR: unable to get hostname %s", err.Error())
}

c.BackoffStrategy = ExponentialStrategy{}
c.BackoffStrategy = &ExponentialStrategy{}
c.ClientID = strings.Split(hostname, ".")[0]
c.Hostname = hostname
c.UserAgent = fmt.Sprintf("go-nsq/%s", VERSION)
Expand Down
5 changes: 3 additions & 2 deletions config_test.go
@@ -1,6 +1,7 @@
package nsq

import (
"math/rand"
"net"
"testing"
"time"
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestExponentialBackoff(t *testing.T) {
32 * time.Second,
}
backoffTest(t, expected, func(c *Config) BackoffStrategy {
return ExponentialStrategy{cfg: c}
return &ExponentialStrategy{cfg: c}
})
}

Expand All @@ -75,7 +76,7 @@ func TestFullJitterBackoff(t *testing.T) {
21467499218 * time.Nanosecond,
}
backoffTest(t, expected, func(c *Config) BackoffStrategy {
return FullJitterStrategy{cfg: c}
return &FullJitterStrategy{cfg: c, rng: rand.New(rand.NewSource(99))}
})
}

Expand Down
2 changes: 1 addition & 1 deletion consumer_test.go
Expand Up @@ -141,7 +141,7 @@ func TestConsumerTLSClientCertViaSet(t *testing.T) {

func consumerTest(t *testing.T, cb func(c *Config)) {
config := NewConfig()
laddr := "127.0.0.2"
laddr := "127.0.0.1"
// so that the test can simulate binding consumer to specified address
config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
// so that the test can simulate reaching max requeues and a call to LogFailedMessage
Expand Down

0 comments on commit 14ba867

Please sign in to comment.