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

change restclient #123

Merged
merged 5 commits into from
Jun 2, 2017
Merged

change restclient #123

merged 5 commits into from
Jun 2, 2017

Conversation

gongweibao
Copy link
Collaborator

No description provided.

@@ -14,7 +14,15 @@ import (
// HTTPOK is ok status of http api call
const HTTPOK = "200 OK"

var httpTransport = &http.Transport{}
type RestClient struct {
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

有道理。fix了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

优势在于可以多线程并发。

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,
Copy link
Collaborator Author

@gongweibao gongweibao Jun 2, 2017

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool.

const HTTPOK = "200 OK"

var httpTransport = &http.Transport{}
type RestClient struct {
Copy link
Collaborator

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()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

httpclient的确是线程安全的。Done。

Copy link
Collaborator

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

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

LGTM

@gongweibao gongweibao merged commit 5065ed0 into PaddlePaddle:develop Jun 2, 2017
@gongweibao gongweibao deleted the restclient branch August 21, 2017 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants