Skip to content

Commit

Permalink
Modify the proxy logic when dealing with the http response (#24)
Browse files Browse the repository at this point in the history
* Modify the proxy logic when dealing with the http
response.

1. edit function which inspects response for body type
2. fix a nil pointer error when ranging over a channel
3. make datacenter a flag to better support deployment environments
4. simplify http ResponseWriter logic and handling time
5. fix small errors in docker-compose

* Dockerize:
1. Add the address to the test api objects
2. re order docker-compose so that it is in order of dependency
3. add a second endpoint to test against
4. fix node names registered in consul to have a more ture random number
5. small json formatting cleanup
  • Loading branch information
Ethan Kaley authored and jfrey committed Aug 25, 2016
1 parent 5077640 commit 7c21452
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 73 deletions.
5 changes: 3 additions & 2 deletions rackhd/cmd/rackhd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

var binaryName, buildDate, buildUser, commitHash, goVersion, osArch, releaseVersion string
var backendAddr, proxyAddr, serviceName string
var backendAddr, proxyAddr, serviceName, datacenter string

// init takes in configurable flags
func init() {
flag.StringVar(&backendAddr, "backend-address", "127.0.0.1:8500", "address:port of the backend store")
flag.StringVar(&proxyAddr, "proxy-address", "http://0.0.0.0:10001", "http://address:port of the proxy server")
flag.StringVar(&serviceName, "service-name", "RackHD-service:api:2.0", "The service being proxied")
flag.StringVar(&datacenter, "datacenter", "dc1", "The consul datacenter string")
}

// extractIPPort splits the Address flag into an ip string anf port int
Expand Down Expand Up @@ -62,7 +63,7 @@ func main() {
}

// Proxy server configuration
h, err := proxy.NewServer(proxyIP, serviceName, "dc-docker", backendAddr, registry.CONSUL, proxyPort)
h, err := proxy.NewServer(proxyIP, serviceName, datacenter, backendAddr, registry.CONSUL, proxyPort)
if err != nil {
log.Fatalf("Proxy server configuration failed: %s\n", err)
}
Expand Down
12 changes: 6 additions & 6 deletions rackhd/cmd/utils/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func NewServer(endpointIP, serviceName, datacenter, backendAddr string, backend
s.Port = endpointPort

for i := 0; i < 5; i++ {
_, err = s.Store.Leader()
if err == nil {
leader, err := s.Store.Leader()
if err == nil && leader != "" {
return s, nil
}
time.Sleep(5 * time.Second)
Expand All @@ -78,7 +78,7 @@ func (e *Server) HandleServeArray(w http.ResponseWriter, r *http.Request) {
node := NodeObject{
Name: fmt.Sprintf("TESTNODE: %+v", rand.Intn(100)),
Thing1: "arrays for days",
Thing2: "moar things go here",
Thing2: e.Address,
Numbers: i,
}
nodes = append(nodes, node)
Expand All @@ -94,16 +94,16 @@ func (e *Server) HandleServeObject(w http.ResponseWriter, r *http.Request) {
object := NodeObject{
Name: name,
Thing1: "objects FTW",
Thing2: "moar things go here :)",
Thing2: e.Address,
Numbers: rand.Intn(90000),
}

json.NewEncoder(w).Encode(object)
}

// Register is...
func (e *Server) Register(datacenter, serviceName string) {
n := fmt.Sprintf("%d", rand.Int())
rGen := rand.New(rand.NewSource(time.Now().UnixNano()))
n := fmt.Sprintf("%d", rGen.Int())
err := e.Store.Register(&regStore.CatalogRegistration{
Node: n,
Address: e.Address,
Expand Down
7 changes: 4 additions & 3 deletions rackhd/cmd/utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

var binaryName, buildDate, buildUser, commitHash, goVersion, osArch, releaseVersion string
var backendAddr, endpointAddr, serviceName string
var backendAddr, endpointAddr, serviceName, datacenter string

// init takes in configurable flags
func init() {
flag.StringVar(&backendAddr, "backend-address", "127.0.0.1:8500", "address:port of the backend store")
flag.StringVar(&endpointAddr, "endpoint-address", "http://0.0.0.0:10002", "http://address:port of the endpoint server")
flag.StringVar(&serviceName, "service-name", "RackHD-service:api:2.0:TEST", "The service being spoofed")
flag.StringVar(&datacenter, "datacenter", "dc1", "The consul datacenter string")
}

// extractIPPort splits the Address flag into an ip string anf port int
Expand Down Expand Up @@ -54,12 +55,12 @@ func main() {
}

// Proxy server configuration
h, err := api.NewServer(endpointIP, serviceName, "dc-docker", backendAddr, registry.CONSUL, endpointPort)
h, err := api.NewServer(endpointIP, serviceName, datacenter, backendAddr, registry.CONSUL, endpointPort)
if err != nil {
log.Fatalf("Endpoint server configuration failed: %s\n", err)
}

h.Register("dc-docker", serviceName)
h.Register(datacenter, serviceName)

fmt.Printf("Endpoint is served on => %s:%d\n", h.Address, h.Port)

Expand Down
81 changes: 42 additions & 39 deletions rackhd/docker-compose-rackhd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,26 @@ services:
- "8600"
- "8600/udp"
command: "agent -config-dir /etc/consul.d/server.json -bootstrap"
nm-rackhd-proxy:
consulclient:
image: "rackhd/consul:client"
container_name: "consulclient"
hostname: "consulclient"
ports:
- "8500:8500"
expose:
- "8300"
- "8301"
- "8301/udp"
- "8302"
- "8302/udp"
- "8400"
- "8500"
- "8500/udp"
- "8600"
- "8600/udp"
depends_on:
- consulserver
rackhd:
build:
context: .
dockerfile: Dockerfile-proxy
Expand All @@ -28,67 +47,51 @@ services:
hostname: "rackhd"
links:
- consulclient
- endpoint1
- endpoint2
ports:
- "10001:10001"
expose:
- "10001"
- "10002"
- "8300"
- "8301"
- "8301/udp"
- "8302"
- "8302/udp"
- "8400"
- "10003"
- "8500"
- "8500/udp"
- "8600"
- "8600/udp"
command: "-proxy-address=http://0.0.0.0:10001 -backend-address=consulclient:8500 -service-name=RackHD-service:api:2.0:TEST"
command: "-proxy-address=http://rackhd:10001 -backend-address=consulclient:8500 -service-name=RackHD-service:api:2.0:TEST -datacenter=dc-docker"
depends_on:
- consulclient
endpoint:
endpoint1:
build:
context: .
dockerfile: Dockerfile-endpoint
image: "rackhd/endpoint:latest"
container_name: "endpoint"
hostname: "endpoint"
container_name: "endpoint1"
hostname: "endpoint1"
links:
- consulclient
ports:
- "10002:10002"
expose:
- "10001"
- "10002"
- "8300"
- "8301"
- "8301/udp"
- "8302"
- "8302/udp"
- "8400"
- "8500"
- "8500/udp"
- "8600"
- "8600/udp"
command: "-endpoint-address=http://0.0.0.0:10002 -backend-address=consulclient:8500"
command: "-endpoint-address=http://endpoint1:10002 -backend-address=consulclient:8500 -datacenter=dc-docker"
depends_on:
- consulclient
consulclient:
image: "rackhd/consul:client"
container_name: "consulclient"
hostname: "consulclient"
endpoint2:
build:
context: .
dockerfile: Dockerfile-endpoint
image: "rackhd/endpoint:latest"
container_name: "endpoint2"
hostname: "endpoint2"
links:
- consulclient
ports:
- "8500:8500"
- "10003:10003"
expose:
- "8300"
- "8301"
- "8301/udp"
- "8302"
- "8302/udp"
- "8400"
- "10001"
- "10003"
- "8500"
- "8500/udp"
- "8600"
- "8600/udp"
command: "-endpoint-address=http://endpoint2:10003 -backend-address=consulclient:8500 -datacenter=dc-docker"
depends_on:
- consulserver
- consulclient
30 changes: 7 additions & 23 deletions rackhd/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ func (p *Server) HandleNodes(w http.ResponseWriter, r *http.Request) {
log.Printf("Did not get IP List ==> %s\n", err)
}
cr, _ := p.GetResp(r, addresses)

for range cr {
p.RespCheck(w, cr)
}
p.RespCheck(w, cr)
return
}

Expand All @@ -80,7 +77,6 @@ func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) (chan *Resp
r.URL.Scheme = "http"
fmt.Printf("url string %s\n", r.URL.String())
respGet, err := http.Get(r.URL.String())
fmt.Printf("%+v\n\n", respGet)
if err != nil {
errs <- fmt.Errorf("Could not send any HTTP Get requests: %s\n", err)
return
Expand All @@ -99,28 +95,16 @@ func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) (chan *Resp

// RespCheck identifies the type of initialResp.Body and calls the appropriate
// helper function to write to the ResponseWriter.
func (p *Server) RespCheck(w http.ResponseWriter, c chan *Response) {
initialResp := <-c
w.WriteHeader(initialResp.StatusCode)
if initialResp.Body[0] == '[' {
p.CombineResp(c, initialResp, w)
} else {
p.PassResp(initialResp, w)
func (p *Server) RespCheck(w http.ResponseWriter, cr chan *Response) {
initialResp := <-cr
if initialResp.Body[0] != '[' {
w.Write(initialResp.Body)
return
}
}

// CombineResp combines many response.Body objects and formats them correctly
// with json structuring.
func (p *Server) CombineResp(cr chan *Response, initialResp *Response, w http.ResponseWriter) {
w.Write(initialResp.Body[0 : len(initialResp.Body)-1])
w.Write(initialResp.Body[0 : len(initialResp.Body)-2])
for r := range cr {
w.Write([]byte(","))
w.Write(r.Body[1 : len(r.Body)-2])
}
w.Write([]byte("]"))
}

// PassResp takes a response.Body object and writes it to the ResponseWriter
func (p *Server) PassResp(initialResp *Response, w http.ResponseWriter) {
w.Write(initialResp.Body)
}

0 comments on commit 7c21452

Please sign in to comment.