Skip to content

Commit

Permalink
array channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaley committed Sep 26, 2016
1 parent 41b90b3 commit 1152d50
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
23 changes: 14 additions & 9 deletions rackhd/proxy/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/hashicorp/go-cleanhttp"
)

// Responses is a
type Responses []Response

// Server is the proxy server struct
type Server struct {
Address string
Expand Down Expand Up @@ -72,11 +75,9 @@ func (p *Server) HandleNodes(w http.ResponseWriter, r *http.Request) {
// GetResp makes channels for the response and errors from http.Get.
// A go func is spun up for each http.Get and the responses are fed
// into their respective channels.
func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) (chan *Response, chan error) {
func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) []Responses {
cr := make(chan *Response, len(addrs))
errs := make(chan error, len(addrs))
defer close(cr)
defer close(errs)

for entry := range addrs {
p.wg.Add(1)
Expand All @@ -85,28 +86,32 @@ func (p *Server) GetResp(r *http.Request, addrs map[string]struct{}) (chan *Resp
defer p.wg.Done()
req, err := NewRequest(r, entry)
if err != nil {
log.Printf("Error creating request => %s\n", err)
cr <- NewResposeFromError(err)
return
}
fmt.Printf("Request => \n%+v\n\n", req)
client := cleanhttp.DefaultClient()
respGet, err := client.Do(req)
if err != nil {
errs <- fmt.Errorf("Could not send HTTP Get request to %s => %s\n", req.URL.String(), err)
cr <- NewResposeFromError(err)
return
}
fmt.Printf("RESP URL => \n%+v\n\n\n", respGet.Body)
responseCopy, err := NewResponse(respGet)
responseCopy, err := NewResponse(respGet, err)
if err != nil {
log.Printf("Error copying response => %s\n", err)
cr <- NewResposeFromError(err)
return
}
cr <- responseCopy
respGet.Body.Close()
}(entry, r)
}
p.wg.Wait()
return cr, errs
var ar []Responses
for entry := range cr {
ar = append(ar, entry)
}
return ar
}

// GetAddresses decides from where to retrieve the addresses
Expand Down Expand Up @@ -155,7 +160,7 @@ func (p *Server) GetQueryAddresses(querySlice []string) map[string]struct{} {

// 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, cr chan *Response) {
func (p *Server) RespCheck(r *http.Request, w http.ResponseWriter, ar []Response) {
initialResp := <-cr
if initialResp.Body[0] != '[' {
w.Write(initialResp.Body)
Expand Down
10 changes: 10 additions & 0 deletions rackhd/proxy/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Response struct {
StatusCode int
Body []byte
RequestURL string
Error error
}

// NewResponse copies a http.Response into a proxy Response
Expand All @@ -24,10 +25,19 @@ func NewResponse(resp *http.Response) (*Response, error) {
StatusCode: resp.StatusCode,
Body: body,
RequestURL: resp.Request.URL.String(),
Error: nil,
}
return proxyResponse, err
}

// NewResposeFromError sets errors
func NewResposeFromError(err error) *Response {
proxyRespnse := &Response{
Error: err,
}
return proxyRespnse
}

// NewRequest copies a http.Request & Header and sets the new host
func NewRequest(r *http.Request, host string) (*http.Request, error) {
req, err := http.NewRequest(r.Method, "http://"+host+r.URL.Path, r.Body)
Expand Down

0 comments on commit 1152d50

Please sign in to comment.