Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow BackoffStrategy to be set via flag, document BackoffStrategy.SetConfig #126

Merged
merged 1 commit into from
Mar 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type Config struct {
DefaultRequeueDelay time.Duration `opt:"default_requeue_delay" min:"0" max:"60m" default:"90s"`

// Backoff strategy, defaults to exponential backoff. Overwrite this to define alternative backoff algrithms.
BackoffStrategy BackoffStrategy
BackoffStrategy BackoffStrategy `opt:"backoff_strategy" default:"exponential"`
// Maximum amount of time to backoff when processing fails 0 == no backoff
MaxBackoffDuration time.Duration `opt:"max_backoff_duration" min:"0" max:"60m" default:"2m"`
// Unit of time for calculating consumer backoff
Expand Down Expand Up @@ -185,7 +185,7 @@ type Config struct {
func NewConfig() *Config {
c := &Config{
configHandlers: []configHandler{&structTagsConfig{}, &tlsConfig{}},
initialized: true,
initialized: true,
}
if err := c.setDefaults(); err != nil {
panic(err.Error())
Expand Down Expand Up @@ -304,6 +304,14 @@ func (h *structTagsConfig) Set(c *Config, option string, value interface{}) erro
option, coercedVal.Interface(), coercedMaxVal.Interface())
}
}
if coercedVal.Type().String() == "nsq.BackoffStrategy" {
v := coercedVal.Interface().(BackoffStrategy)
if v, ok := v.(interface {
setConfig(*Config)
}); ok {
v.setConfig(c)
}
}
dest.Set(coercedVal)
return nil
}
Expand Down Expand Up @@ -331,7 +339,6 @@ func (h *structTagsConfig) SetDefaults(c *Config) error {
log.Fatalf("ERROR: unable to get hostname %s", err.Error())
}

c.BackoffStrategy = &ExponentialStrategy{}
c.ClientID = strings.Split(hostname, ".")[0]
c.Hostname = hostname
c.UserAgent = fmt.Sprintf("go-nsq/%s", VERSION)
Expand Down Expand Up @@ -373,17 +380,6 @@ func (h *structTagsConfig) Validate(c *Config) error {
return fmt.Errorf("HeartbeatInterval %v must be less than ReadTimeout %v", c.HeartbeatInterval, c.ReadTimeout)
}

if c.BackoffStrategy == nil {
return fmt.Errorf("BackoffStrategy cannot be nil")
}

// initialize internal backoff strategies that need access to config
if v, ok := c.BackoffStrategy.(interface {
setConfig(*Config)
}); ok {
v.setConfig(c)
}

return nil
}

Expand Down Expand Up @@ -538,6 +534,8 @@ func coerce(v interface{}, typ reflect.Type) (reflect.Value, error) {
v, err = coerceDuration(v)
case "net.Addr":
v, err = coerceAddr(v)
case "nsq.BackoffStrategy":
v, err = coerceBackoffStrategy(v)
default:
v = nil
err = errors.New(fmt.Sprintf("invalid type %s", typ.String()))
Expand Down Expand Up @@ -604,6 +602,21 @@ func coerceAddr(v interface{}) (net.Addr, error) {
return nil, errors.New("invalid value type")
}

func coerceBackoffStrategy(v interface{}) (BackoffStrategy, error) {
switch v := v.(type) {
case string:
switch v {
case "", "exponential":
return &ExponentialStrategy{}, nil
case "full_jitter":
return &FullJitterStrategy{}, nil
}
case BackoffStrategy:
return v, nil
}
return nil, errors.New("invalid value type")
}

func coerceBool(v interface{}) (bool, error) {
switch v := v.(type) {
case bool:
Expand Down
10 changes: 10 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nsq
import (
"math/rand"
"net"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -43,6 +44,15 @@ func TestConfigSet(t *testing.T) {
if c.LocalAddr.String() != "1.2.3.4:27015" {
t.Error("Failed to assign `local_addr` config")
}
if reflect.ValueOf(c.BackoffStrategy).Type().String() != "*nsq.ExponentialStrategy" {
t.Error("Failed to set default `exponential` backoff strategy")
}
if err := c.Set("backoff_strategy", "full_jitter"); err != nil {
t.Errorf("Failed to assign `backoff_strategy` config: %v", err)
}
if reflect.ValueOf(c.BackoffStrategy).Type().String() != "*nsq.FullJitterStrategy" {
t.Error("Failed to set `full_jitter` backoff strategy")
}
}

func TestConfigValidate(t *testing.T) {
Expand Down