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

Confused in the below code lines #1

Open
anpolychrono opened this issue Mar 15, 2022 · 4 comments
Open

Confused in the below code lines #1

anpolychrono opened this issue Mar 15, 2022 · 4 comments

Comments

@anpolychrono
Copy link

I am bit confused with the lines under the comment "wake up the function ?", why you are calling the newRequest() again and then you are calling the http.Do() twice ?

file : petstore.go

func (c *Client) do(ctx context.Context, req *http.Request, v interface{}) error {
	// Add the context to the request.
	req = req.WithContext(ctx)
	log.Printf("[DEBUG] go-petstore request: %v", req)
	
	// wake up the function?
	tempReq, _ := c.newRequest("GET","pets",nil)
	c.http.Do(tempReq)
		  
	// Execute the request and check the response.
	resp, err := c.http.Do(req)
@scottwinkler
Copy link

Hello Andreas,
thank you for reading my book. you have found some of the code i am less proud of. this was a hack i did to get the api to work on AWS lambda. See lambda has a cold start which can be a few seconds. During that time it has to first start up the gin server, and is therefore not able to process the request. Instead, it hangs for the full 30 seconds before returning nothing. What this does is make a request to "wake up" the lambda function, then the next request will surely succeed. Not great, but the best i was able to come up with. If you know of a better way, I would be happy to have your PR.

Regards,
Scott

@anpolychrono
Copy link
Author

anpolychrono commented Mar 15, 2022 via email

@scottwinkler
Copy link

Thank you for your kind words. I hope to write a version two of the book someday. If you have not already done so, I would appreciate if you could please leave a positive review, it really helps me out.

@anpolychrono
Copy link
Author

anpolychrono commented Mar 15, 2022 via email

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

No branches or pull requests

2 participants