-
Notifications
You must be signed in to change notification settings - Fork 78
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
change restclient #123
change restclient #123
Conversation
go/paddlecloud/restclient.go
Outdated
@@ -14,7 +14,15 @@ import ( | |||
// HTTPOK is ok status of http api call | |||
const HTTPOK = "200 OK" | |||
|
|||
var httpTransport = &http.Transport{} | |||
type RestClient struct { |
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.
What are the benefits of adding a struct? We can not re-use the connection to the server if we need to create a new RestClient
every time.
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.
有道理。fix了。
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.
优势在于可以多线程并发。
go/paddlecloud/restclient.go
Outdated
func NewRestClient() *RestClient { | ||
client := http.Client{Transport: &http.Transport{}} | ||
return &RestClient{client: &client} | ||
} | ||
|
||
func makeRequest(uri string, method string, body io.Reader, | ||
contentType string, query map[string]string, |
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.
之所以把query
改成string
,是因为map[string][]string
才能表达,而这样的结构体用起来太复杂,不如让用户在外边自己encode
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.
Cool.
go/paddlecloud/restclient.go
Outdated
const HTTPOK = "200 OK" | ||
|
||
var httpTransport = &http.Transport{} | ||
type RestClient struct { |
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.
A struct with only one member seems not necessary. If we want to implement multi-threading download processes, we can use a more simple function like:
func GetChunk(targetURL string, query url.Values, nThreads int) (*http.Response, error) {
req, err := makeRequest()
// use a thread pool of http.client to do multi-threading
}
Then, we can call this functions by:
resp := restclient.GetChunk()
instead of
client := NewRestClient()
resp := client.GetChunk()
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.
httpclient的确是线程安全的。Done。
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.
LGTM
No description provided.