Skip to content

Commit

Permalink
Merge pull request #33 from mailgun/thrawn/develop
Browse files Browse the repository at this point in the history
Now pass in etcd client to  etcdutil.NewElection()
  • Loading branch information
thrawn01 authored Sep 24, 2018
2 parents 0b3969c + eff45d2 commit d68f4f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
43 changes: 43 additions & 0 deletions clock/duration.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 16 additions & 28 deletions etcdutil/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.0
2.0.0

0 comments on commit d68f4f6

Please sign in to comment.