Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72a93ad

Browse files
Initial PR tidy-up based on feedback
1 parent cb03da6 commit 72a93ad

File tree

7 files changed

+75
-93
lines changed

7 files changed

+75
-93
lines changed

‎sd/eureka/client.go‎

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,77 @@
11
package eureka
22

33
import (
4-
stdeureka "github.com/hudl/fargo"
5-
stdeurekalogging "github.com/op/go-logging"
4+
fargo "github.com/hudl/fargo"
65
)
76

8-
func init() {
9-
// Quieten Fargo's own logging
10-
stdeurekalogging.SetLevel(stdeurekalogging.ERROR, "fargo")
11-
}
12-
137
// Client is a wrapper around the Eureka API.
148
type Client interface {
159
// Register an instance with Eureka.
16-
Register(i *stdeureka.Instance) error
10+
Register(i *fargo.Instance) error
1711

1812
// Deregister an instance from Eureka.
19-
Deregister(i *stdeureka.Instance) error
13+
Deregister(i *fargo.Instance) error
2014

2115
// Send an instance heartbeat to Eureka.
22-
Heartbeat(i *stdeureka.Instance) error
16+
Heartbeat(i *fargo.Instance) error
2317

2418
// Get all instances for an app in Eureka.
25-
Instances(app string) ([]*stdeureka.Instance, error)
19+
Instances(app string) ([]*fargo.Instance, error)
2620

2721
// Receive scheduled updates about an app's instances in Eureka.
28-
ScheduleUpdates(app string, quitc chan struct{}) <-chan stdeureka.AppUpdate
22+
ScheduleUpdates(app string, quitc chan struct{}) <-chan fargo.AppUpdate
2923
}
3024

3125
type client struct {
32-
connection *stdeureka.EurekaConnection
26+
connection *fargo.EurekaConnection
3327
}
3428

3529
// NewClient returns an implementation of the Client interface, wrapping a
3630
// concrete connection to Eureka using the Fargo library.
3731
// Taking in Fargo's own connection abstraction gives the user maximum
3832
// freedom in regards to how that connection is configured.
39-
func NewClient(ec *stdeureka.EurekaConnection) Client {
33+
func NewClient(ec *fargo.EurekaConnection) Client {
4034
return &client{connection: ec}
4135
}
4236

43-
func (c *client) Register(i *stdeureka.Instance) error {
37+
func (c *client) Register(i *fargo.Instance) error {
4438
if c.instanceRegistered(i) {
4539
// Already registered. Send a heartbeat instead.
4640
return c.Heartbeat(i)
4741
}
4842
return c.connection.RegisterInstance(i)
4943
}
5044

51-
func (c *client) Deregister(i *stdeureka.Instance) error {
45+
func (c *client) Deregister(i *fargo.Instance) error {
5246
return c.connection.DeregisterInstance(i)
5347
}
5448

55-
func (c *client) Heartbeat(i *stdeureka.Instance) (err error) {
49+
func (c *client) Heartbeat(i *fargo.Instance) (err error) {
5650
if err = c.connection.HeartBeatInstance(i); err != nil && c.instanceNotFoundErr(err) {
5751
// Instance not registered. Register first before sending heartbeats.
5852
return c.Register(i)
5953
}
6054
return err
6155
}
6256

63-
func (c *client) Instances(app string) ([]*stdeureka.Instance, error) {
57+
func (c *client) Instances(app string) ([]*fargo.Instance, error) {
6458
stdApp, err := c.connection.GetApp(app)
6559
if err != nil {
6660
return nil, err
6761
}
6862
return stdApp.Instances, nil
6963
}
7064

71-
func (c *client) ScheduleUpdates(app string, quitc chan struct{}) <-chan stdeureka.AppUpdate {
65+
func (c *client) ScheduleUpdates(app string, quitc chan struct{}) <-chan fargo.AppUpdate {
7266
return c.connection.ScheduleAppUpdates(app, false, quitc)
7367
}
7468

75-
func (c *client) instanceRegistered(i *stdeureka.Instance) bool {
69+
func (c *client) instanceRegistered(i *fargo.Instance) bool {
7670
_, err := c.connection.GetInstance(i.App, i.Id())
7771
return err == nil
7872
}
7973

8074
func (c *client) instanceNotFoundErr(err error) bool {
81-
code, ok := stdeureka.HTTPResponseStatusCode(err)
75+
code, ok := fargo.HTTPResponseStatusCode(err)
8276
return ok && code == 404
8377
}

‎sd/eureka/client_test.go‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"errors"
55
"reflect"
66

7+
fargo "github.com/hudl/fargo"
8+
79
"github.com/go-kit/kit/log"
8-
stdeureka "github.com/hudl/fargo"
910
)
1011

1112
var (
1213
errTest = errors.New("kaboom")
1314
loggerTest = log.NewNopLogger()
14-
instanceTest1 = &stdeureka.Instance{
15+
instanceTest1 = &fargo.Instance{
1516
HostName: "server1.acme.org",
1617
Port: 8080,
1718
App: "go-kit",
@@ -21,11 +22,11 @@ var (
2122
HealthCheckUrl: "http://server1.acme.org:8080/healthz",
2223
StatusPageUrl: "http://server1.acme.org:8080/status",
2324
HomePageUrl: "http://server1.acme.org:8080/",
24-
Status: stdeureka.UP,
25-
DataCenterInfo: stdeureka.DataCenterInfo{Name: stdeureka.MyOwn},
26-
LeaseInfo: stdeureka.LeaseInfo{RenewalIntervalInSecs: 1},
25+
Status: fargo.UP,
26+
DataCenterInfo: fargo.DataCenterInfo{Name: fargo.MyOwn},
27+
LeaseInfo: fargo.LeaseInfo{RenewalIntervalInSecs: 1},
2728
}
28-
instanceTest2 = &stdeureka.Instance{
29+
instanceTest2 = &fargo.Instance{
2930
HostName: "server2.acme.org",
3031
Port: 8080,
3132
App: "go-kit",
@@ -35,24 +36,24 @@ var (
3536
HealthCheckUrl: "http://server2.acme.org:8080/healthz",
3637
StatusPageUrl: "http://server2.acme.org:8080/status",
3738
HomePageUrl: "http://server2.acme.org:8080/",
38-
Status: stdeureka.UP,
39-
DataCenterInfo: stdeureka.DataCenterInfo{Name: stdeureka.MyOwn},
39+
Status: fargo.UP,
40+
DataCenterInfo: fargo.DataCenterInfo{Name: fargo.MyOwn},
4041
}
41-
applicationTest = &stdeureka.Application{
42+
applicationTest = &fargo.Application{
4243
Name: "go-kit",
43-
Instances: []*stdeureka.Instance{instanceTest1, instanceTest2},
44+
Instances: []*fargo.Instance{instanceTest1, instanceTest2},
4445
}
4546
)
4647

4748
type testClient struct {
48-
instances []*stdeureka.Instance
49-
application *stdeureka.Application
49+
instances []*fargo.Instance
50+
application *fargo.Application
5051
errInstances error
5152
errApplication error
5253
errHeartbeat error
5354
}
5455

55-
func (c *testClient) Register(i *stdeureka.Instance) error {
56+
func (c *testClient) Register(i *fargo.Instance) error {
5657
for _, instance := range c.instances {
5758
if reflect.DeepEqual(*instance, *i) {
5859
return errors.New("already registered")
@@ -63,8 +64,8 @@ func (c *testClient) Register(i *stdeureka.Instance) error {
6364
return nil
6465
}
6566

66-
func (c *testClient) Deregister(i *stdeureka.Instance) error {
67-
var newInstances []*stdeureka.Instance
67+
func (c *testClient) Deregister(i *fargo.Instance) error {
68+
var newInstances []*fargo.Instance
6869
for _, instance := range c.instances {
6970
if reflect.DeepEqual(*instance, *i) {
7071
continue
@@ -79,16 +80,16 @@ func (c *testClient) Deregister(i *stdeureka.Instance) error {
7980
return nil
8081
}
8182

82-
func (c *testClient) Heartbeat(i *stdeureka.Instance) (err error) {
83+
func (c *testClient) Heartbeat(i *fargo.Instance) (err error) {
8384
return c.errHeartbeat
8485
}
8586

86-
func (c *testClient) Instances(app string) ([]*stdeureka.Instance, error) {
87+
func (c *testClient) Instances(app string) ([]*fargo.Instance, error) {
8788
return c.instances, c.errInstances
8889
}
8990

90-
func (c *testClient) ScheduleUpdates(service string, quitc chan struct{}) <-chan stdeureka.AppUpdate {
91-
updatec := make(chan stdeureka.AppUpdate, 1)
92-
updatec <- stdeureka.AppUpdate{App: c.application, Err: c.errApplication}
91+
func (c *testClient) ScheduleUpdates(service string, quitc chan struct{}) <-chan fargo.AppUpdate {
92+
updatec := make(chan fargo.AppUpdate, 1)
93+
updatec <- fargo.AppUpdate{App: c.application, Err: c.errApplication}
9394
return updatec
9495
}

‎sd/eureka/integration_test.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"testing"
99
"time"
1010

11+
fargo "github.com/hudl/fargo"
12+
1113
"github.com/go-kit/kit/endpoint"
1214
"github.com/go-kit/kit/log"
13-
stdeureka "github.com/hudl/fargo"
1415
)
1516

1617
// Package sd/eureka provides a wrapper around the Netflix Eureka service
@@ -30,12 +31,12 @@ func TestIntegration(t *testing.T) {
3031

3132
var client Client
3233
{
33-
var stdConfig stdeureka.Config
34-
stdConfig.Eureka.ServiceUrls = []string{eurekaAddr}
35-
stdConfig.Eureka.PollIntervalSeconds = 1
34+
var fargoConfig fargo.Config
35+
fargoConfig.Eureka.ServiceUrls = []string{eurekaAddr}
36+
fargoConfig.Eureka.PollIntervalSeconds = 1
3637

37-
stdConnection := stdeureka.NewConnFromConfig(stdConfig)
38-
client = NewClient(&stdConnection)
38+
fargoConnection := fargo.NewConnFromConfig(fargoConfig)
39+
client = NewClient(&fargoConnection)
3940
}
4041

4142
logger := log.NewLogfmtLogger(os.Stderr)

‎sd/eureka/registrar.go‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,28 @@ package eureka
22

33
import (
44
"fmt"
5-
"sync"
65
"time"
76

8-
stdeureka "github.com/hudl/fargo"
7+
fargo "github.com/hudl/fargo"
98

109
"github.com/go-kit/kit/log"
1110
)
1211

1312
// Registrar maintains service instance liveness information in Eureka.
1413
type Registrar struct {
1514
client Client
16-
instance *stdeureka.Instance
15+
instance *fargo.Instance
1716
logger log.Logger
18-
19-
quitmtx sync.Mutex
20-
quit chan bool
17+
quit chan bool
2118
}
2219

2320
// NewRegistrar returns an Eureka Registrar acting on behalf of the provided
2421
// Fargo instance.
25-
func NewRegistrar(client Client, i *stdeureka.Instance, l log.Logger) *Registrar {
22+
func NewRegistrar(client Client, i *fargo.Instance, logger log.Logger) *Registrar {
2623
return &Registrar{
2724
client: client,
2825
instance: i,
29-
logger: log.With(l, "service", i.App, "address", fmt.Sprintf("%s:%d", i.IPAddr, i.Port)),
26+
logger: log.With(logger, "service", i.App, "address", fmt.Sprintf("%s:%d", i.IPAddr, i.Port)),
3027
}
3128
}
3229

@@ -40,7 +37,10 @@ func (r *Registrar) Register() {
4037

4138
if r.instance.LeaseInfo.RenewalIntervalInSecs > 0 {
4239
// User has opted for heartbeat functionality in Eureka.
43-
go r.loop()
40+
if r.quit == nil {
41+
r.quit = make(chan bool)
42+
go r.loop()
43+
}
4444
}
4545
}
4646

@@ -52,22 +52,13 @@ func (r *Registrar) Deregister() {
5252
r.logger.Log("action", "deregister")
5353
}
5454

55-
r.quitmtx.Lock()
56-
defer r.quitmtx.Unlock()
5755
if r.quit != nil {
5856
r.quit <- true
57+
r.quit = nil
5958
}
6059
}
6160

6261
func (r *Registrar) loop() {
63-
r.quitmtx.Lock()
64-
if r.quit != nil {
65-
defer r.quitmtx.Unlock()
66-
return // Already running.
67-
}
68-
r.quit = make(chan bool)
69-
r.quitmtx.Unlock()
70-
7162
tick := time.NewTicker(time.Duration(r.instance.LeaseInfo.RenewalIntervalInSecs) * time.Second)
7263
defer tick.Stop()
7364
for {
@@ -77,12 +68,6 @@ func (r *Registrar) loop() {
7768
r.logger.Log("err", err)
7869
}
7970
case <-r.quit:
80-
r.quitmtx.Lock()
81-
defer r.quitmtx.Unlock()
82-
83-
close(r.quit)
84-
r.quit = nil
85-
8671
return
8772
}
8873
}

‎sd/eureka/registrar_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"testing"
55
"time"
66

7-
stdeureka "github.com/hudl/fargo"
7+
fargo "github.com/hudl/fargo"
88
)
99

1010
func TestRegistrar(t *testing.T) {
1111
client := &testClient{
12-
instances: []*stdeureka.Instance{},
12+
instances: []*fargo.Instance{},
1313
errHeartbeat: errTest,
1414
}
1515

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /