From ac8587bef147a0b570d86404311ff7564d01643f Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 30 May 2023 20:45:13 -0700 Subject: [PATCH] Voter loop --- elect.go | 23 -------------- go.mod | 2 ++ go.sum | 8 ++++- pkg_test.go | 11 +++++++ voter.go | 57 ++++++++++++++++++++++++++++++++++ elect_test.go => voter_test.go | 6 ++-- 6 files changed, 81 insertions(+), 26 deletions(-) delete mode 100644 elect.go create mode 100644 pkg_test.go create mode 100644 voter.go rename elect_test.go => voter_test.go (65%) diff --git a/elect.go b/elect.go deleted file mode 100644 index 799577c..0000000 --- a/elect.go +++ /dev/null @@ -1,23 +0,0 @@ -package elect - -import ( - "github.com/dchest/uniuri" - "github.com/go-resty/resty/v2" -) - -type Elect struct { - client *resty.Client - instanceID string - signingKey string -} - -func New(url string, signingKey string) *Elect { - e := &Elect{ - client: resty.New(). - SetBaseURL(url), - instanceID: uniuri.New(), - signingKey: signingKey, - } - - return e -} diff --git a/go.mod b/go.mod index 8eb4cbf..0d11776 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,12 @@ require ( github.com/dchest/uniuri v1.2.0 github.com/go-resty/resty/v2 v2.7.0 github.com/stretchr/testify v1.8.4 + go.uber.org/goleak v1.2.1 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.0.0-20211029224645-99673261e6eb // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 0d85e33..170cb07 100644 --- a/go.sum +++ b/go.sum @@ -1,13 +1,19 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g= github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY= github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A= +go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= golang.org/x/net v0.0.0-20211029224645-99673261e6eb h1:pirldcYWx7rx7kE5r+9WsOXPXK0+WH5+uZ7uPmJ44uM= golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -15,7 +21,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg_test.go b/pkg_test.go new file mode 100644 index 0000000..d222415 --- /dev/null +++ b/pkg_test.go @@ -0,0 +1,11 @@ +package elect_test + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} diff --git a/voter.go b/voter.go new file mode 100644 index 0000000..dfdfbd9 --- /dev/null +++ b/voter.go @@ -0,0 +1,57 @@ +package elect + +import ( + "time" + + "github.com/dchest/uniuri" + "github.com/go-resty/resty/v2" +) + +type Voter struct { + client *resty.Client + instanceID string + signingKey string + update chan<- time.Duration +} + +func NewVoter(url string, signingKey string) *Voter { + update := make(chan time.Duration) // intentionally 0-capacity + + v := &Voter{ + client: resty.New(). + SetBaseURL(url), + instanceID: uniuri.New(), + signingKey: signingKey, + update: update, + } + + go v.loop(update) + + return v +} + +func (v *Voter) Stop() { + close(v.update) +} + +func (v *Voter) loop(update <-chan time.Duration) { + t := time.NewTicker(5 * time.Second) + defer t.Stop() + + for { + select { + case <-t.C: + v.vote() + + case period, ok := <-update: + if !ok { + return + } + + t.Reset(period) + } + } +} + +func (v *Voter) vote() { +} diff --git a/elect_test.go b/voter_test.go similarity index 65% rename from elect_test.go rename to voter_test.go index 94b83d5..362baf8 100644 --- a/elect_test.go +++ b/voter_test.go @@ -10,6 +10,8 @@ import ( func TestNew(t *testing.T) { t.Parallel() - c := elect.New("[::1]:1234", "abc123") - require.NotNil(t, c) + v := elect.NewVoter("[::1]:1234", "abc123") + require.NotNil(t, v) + + defer v.Stop() }