diff --git a/clock/duration.go b/clock/duration.go new file mode 100644 index 00000000..87a53a7c --- /dev/null +++ b/clock/duration.go @@ -0,0 +1,43 @@ +package clock + +import ( + "encoding/json" + "fmt" + "time" + + "github.com/pkg/errors" +) + +type DurationJSON struct { + Duration time.Duration + str string +} + +func (d DurationJSON) MarshalJSON() ([]byte, error) { + return json.Marshal(d.Duration.String()) +} + +func (d *DurationJSON) UnmarshalJSON(b []byte) error { + var v interface{} + var err error + + if err = json.Unmarshal(b, &v); err != nil { + return err + } + + switch value := v.(type) { + case float64: + d.Duration = time.Duration(value) + d.str = fmt.Sprintf("%f", value) + case string: + d.Duration, err = time.ParseDuration(value) + d.str = value + default: + return errors.New("invalid duration") + } + return err +} + +func (d DurationJSON) String() string { + return d.str +} diff --git a/etcdutil/election.go b/etcdutil/election.go index 64884429..32ab6017 100644 --- a/etcdutil/election.go +++ b/etcdutil/election.go @@ -31,14 +31,13 @@ type Election struct { // Seconds to wait before giving up the election if leader disconnected TTL int - etcdConfig *etcd.Config - session *concurrency.Session - election *concurrency.Election - client *etcd.Client - cancel context.CancelFunc - wg holster.WaitGroup - ctx context.Context - isLeader int32 + session *concurrency.Session + election *concurrency.Election + client *etcd.Client + cancel context.CancelFunc + wg holster.WaitGroup + ctx context.Context + isLeader int32 } // Use leader election if you have several instances of a service running in production @@ -53,16 +52,16 @@ type Election struct { // if election.IsLeader() { // // Do periodic thing // } -func NewElection(election, candidate string, etcdConfig *etcd.Config) (*Election, error) { +func NewElection(election, candidate string, client *etcd.Client) (*Election, error) { log = logrus.WithField("category", "election") ctx, cancelFunc := context.WithCancel(context.Background()) e := &Election{ - Candidate: candidate, - Election: election, - TTL: 5, - etcdConfig: etcdConfig, - cancel: cancelFunc, - ctx: ctx, + Candidate: candidate, + Election: election, + TTL: 5, + cancel: cancelFunc, + ctx: ctx, + client: client, } if host, err := os.Hostname(); err == nil { @@ -72,22 +71,11 @@ func NewElection(election, candidate string, etcdConfig *etcd.Config) (*Election // Set a prefix key for elections e.Election = path.Join("/elections", e.Election) - var err error - e.etcdConfig, err = NewConfig(etcdConfig) - if err != nil { - return nil, err - } - - e.client, err = etcd.New(*e.etcdConfig) - if err != nil { - return nil, err - } - - // Test the connection + // Test the client ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() - _, err = e.client.Get(ctx, e.Election) + _, err := e.client.Get(ctx, e.Election) if err != nil { return nil, errors.Wrap(err, "while connecting to etcd") } diff --git a/version b/version index f8e233b2..227cea21 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.9.0 +2.0.0