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

[WIP] Issue36 - Eureka integration #46

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 21 additions & 4 deletions discovery/eureka.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@ package discovery
import (
"errors"
"net/http"
"io/ioutil"
"fmt"
"time"
"net"
)

//TODO: This should become a discovery interface. And Eureka just the first implementation
type EurekaClient struct {
eurekaUrl string
}

//TODO: Creating our own error type and wrapping standard net/http errors could be useful to prevent
// the original errors from being lost
var errEurekaTimesOut = errors.New("Eureka server timed out")
var errNoEurekaConnection = errors.New("Unable to reach Eureka server")
var errEurekaUnexpectedHttpResponseCode = errors.New("Eureka returned a non 200 http response code")
const eurekaClientTimeoutInSeconds = 10

func NewEurekaClient(eurekaUrl string) (ec EurekaClient, err error) {
ec.eurekaUrl = eurekaUrl
resp, err := http.Get(eurekaUrl)
if err != nil {
httpclient := http.Client{Timeout: time.Second * eurekaClientTimeoutInSeconds}
resp, err := httpclient.Get(eurekaUrl)
if serr, ok := err.(net.Error); ok && serr.Timeout() {
return ec,errEurekaTimesOut
} else if err != nil {
return ec, errNoEurekaConnection
}
defer resp.Body.Close()
Expand All @@ -30,8 +41,12 @@ var errNoIpsFound = errors.New("No IPs associated to the requested App name")

func (ec EurekaClient) GetIPs(appName string) ([]string, error) {
eurekaAppUrl := ec.eurekaUrl + "/v2/apps/" + appName
resp, err := http.Get(eurekaAppUrl)
if err != nil {
//resp, err := http.Get(eurekaAppUrl, "application/json; charset=utf-8")
httpclient := http.Client{Timeout: time.Second * eurekaClientTimeoutInSeconds}
resp, err := httpclient.Get(eurekaAppUrl)
if serr, ok := err.(net.Error); ok && serr.Timeout() {
return []string{},errEurekaTimesOut
} else if err != nil {
return []string{}, errNoEurekaConnection
}
defer resp.Body.Close()
Expand All @@ -40,5 +55,7 @@ func (ec EurekaClient) GetIPs(appName string) ([]string, error) {
} else if resp.StatusCode != 200 {
return []string{},errEurekaUnexpectedHttpResponseCode
}
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
return nil, nil
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guessed you will return the string array with the IPs here instead of nil...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WIP, WIP, WIP... ;)

}
9 changes: 8 additions & 1 deletion discovery/eureka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ func TestMain(m *testing.M) {
func TestEurekaClientNoEureka(t *testing.T) {
_, err := NewEurekaClient("http://localhost:9999/thisshouldntwork")
if err != errNoEurekaConnection {
t.Fatal("We shouldnt reach eureka if Eureka hostname/port is completely wrong")
t.Fatal("We shouldnt reach eureka if Eureka hostname/port is completely wrong. Actual err:", err)
}
}

func TestEurekaClientEurekaDoesNotReply(t *testing.T) {
_, err := NewEurekaClient("http://192.0.2.1:9999/thisshouldtimeout")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Addresses starting with "192.0.2.", "198.51.100.", or "203.0.113." are reserved for use in documentation and sample configurations. They should never be used in a live network configuration. No one has permission to use these addresses on the Internet."
I guess doing testing is no documentation...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you suggest another IP range which traffic will be dropped for sure? In fact, these networks are referenced as "TEST NETWORKs" in the RFC, so that was the best option I found...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you were right, don't change it.

if err != errEurekaTimesOut {
t.Fatal("Pointing to a destination that drops packages should fail because of timeout. Actual err:", err)
}
}

Expand Down