-
Notifications
You must be signed in to change notification settings - Fork 3
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
Private/babbara/enable proxy setting #17
base: master
Are you sure you want to change the base?
Changes from all commits
eac6785
16d8f9b
d70327a
58c7bb5
f4bdbe9
f44ea09
ed7054f
7a9efa7
09c2c90
3e9cc31
c914c24
70d1867
af7d420
1af6be0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,38 @@ | ||
package clients | ||
|
||
const HTTPMaxRetry = 5 | ||
// HTTPMaxRetry indicates the number of | ||
// retries to be carried out before giving up. | ||
const HTTPMaxRetry = 9 | ||
|
||
// Clients struct encapsulate the collection of | ||
// Client struct encapsulate the collection of | ||
// external services | ||
type Client struct { | ||
Resmgr Resmgr | ||
Keystone Keystone | ||
Qbert Qbert | ||
Executor Executor | ||
Segment Segment | ||
HTTP HTTP | ||
} | ||
|
||
// New creates the clients needed by the CLI | ||
// to interact with the external services. | ||
func New(fqdn string) (Client, error) { | ||
func New(fqdn string, proxy string) (Client, error) { | ||
|
||
http, err := NewHTTP( | ||
func(impl *HTTPImpl) { impl.Proxy = proxy }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we passing in functions ? Why don't we just pass in the value ? |
||
func(impl *HTTPImpl) { impl.Retry = HTTPMaxRetry }) | ||
|
||
if err != nil { | ||
return Client{}, err | ||
} | ||
|
||
return Client{ | ||
Resmgr: NewResmgr(fqdn), | ||
Keystone: NewKeystone(fqdn), | ||
Qbert: NewQbert(fqdn), | ||
Resmgr: NewResmgr(fqdn, http), | ||
Keystone: NewKeystone(fqdn, http), | ||
Qbert: NewQbert(fqdn, http), | ||
Executor: ExecutorImpl{}, | ||
Segment: NewSegment(fqdn), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to understand more on how we have used in different places, in general I would like to see more "events" and segment should be able to "consume" those events instead of directly using segment. |
||
HTTP: http, | ||
}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package clients | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/PuerkitoBio/rehttp" | ||
) | ||
|
||
type HTTP interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
type HTTPImpl struct { | ||
Proxy string | ||
Retry int | ||
client *http.Client | ||
ProxyURL *url.URL | ||
} | ||
|
||
func NewHTTP(options ...func(*HTTPImpl)) (*HTTPImpl, error) { | ||
resp := &HTTPImpl{} | ||
|
||
for _, option := range options { | ||
option(resp) | ||
} | ||
|
||
var transport http.RoundTripper | ||
if resp.Proxy != "" { | ||
proxyURL, err := url.Parse(resp.Proxy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should parse it when user enters the proxy and error out. ProxyURL can be used inside the code instead of passing proxy as string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fail at the input site only you mean ?! |
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp.ProxyURL = proxyURL | ||
transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)} | ||
} else { | ||
transport = http.DefaultTransport | ||
} | ||
|
||
t := rehttp.NewTransport(transport, rehttp.RetryAll( | ||
rehttp.RetryAny( | ||
rehttp.RetryTemporaryErr(), | ||
rehttp.RetryStatuses(400, 404), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about when gateway times out, I think it is 502 or 503 I can't recall There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
), | ||
rehttp.RetryMaxRetries(resp.Retry), | ||
), | ||
// rehttp.ExpJitterDelay(time.Second*time.Duration(2), time.Second*time.Duration(60)) | ||
rehttp.ConstDelay(time.Second*time.Duration(10))) | ||
|
||
resp.client = &http.Client{Transport: t} | ||
return resp, nil | ||
} | ||
|
||
// Do function simply calls the underlying client to make the request. | ||
func (c HTTPImpl) Do(req *http.Request) (*http.Response, error) { | ||
return c.client.Do(req) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,12 @@ type Keystone interface { | |
} | ||
|
||
type KeystoneImpl struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In terms of the organization, I would actually move up all of these: Keystone.go, Resmgr.go into their own packages: /pkg/keystone/keystone.go; /pkg/resmgr/resmgr.go;... |
||
fqdn string | ||
fqdn string | ||
client HTTP | ||
} | ||
|
||
func NewKeystone(fqdn string) Keystone { | ||
return KeystoneImpl{fqdn} | ||
func NewKeystone(fqdn string, client HTTP) Keystone { | ||
return KeystoneImpl{fqdn: fqdn, client: client} | ||
} | ||
|
||
func (k KeystoneImpl) GetAuth( | ||
|
@@ -65,7 +66,13 @@ func (k KeystoneImpl) GetAuth( | |
} | ||
}`, username, decodedPassword, tenant) | ||
|
||
resp, err := http.Post(url, "application/json", strings.NewReader(body)) | ||
req, err := http.NewRequest("POST", url, strings.NewReader(body)) | ||
if err != nil { | ||
return auth, nil | ||
} | ||
req.Header.Add("Content-Type", "application/json") | ||
|
||
resp, err := k.client.Do(req) | ||
if err != nil { | ||
return auth, err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call it an HTTPClient