本文整理汇总了Golang中github.com/mailgun/vulcan/request.Request.SetBody方法的典型用法代码示例。如果您正苦于以下问题:Golang Request.SetBody方法的具体用法?Golang Request.SetBody怎么用?Golang Request.SetBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/mailgun/vulcan/request.Request
的用法示例。
在下文中一共展示了Request.SetBody方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RoundTrip
// Round trips the request to one of the endpoints and returns the response.
func (l *HttpLocation) RoundTrip(req request.Request) (*http.Response, error) {
// Get options and transport as one single read transaction.
// Options and transport may change if someone calls SetOptions
o, tr := l.GetOptionsAndTransport()
originalRequest := req.GetHttpRequest()
// Check request size first, if that exceeds the limit, we don't bother reading the request.
if l.isRequestOverLimit(req) {
return nil, errors.FromStatus(http.StatusRequestEntityTooLarge)
}
// Read the body while keeping this location's limits in mind. This reader controls the maximum bytes
// to read into memory and disk. This reader returns anerror if the total request size exceeds the
// prefefined MaxSizeBytes. This can occur if we got chunked request, in this case ContentLength would be set to -1
// and the reader would be unbounded bufio in the http.Server
body, err := netutils.NewBodyBufferWithOptions(originalRequest.Body, netutils.BodyBufferOptions{
MemBufferBytes: o.Limits.MaxMemBodyBytes,
MaxSizeBytes: o.Limits.MaxBodyBytes,
})
if err != nil {
return nil, err
}
if body == nil {
return nil, fmt.Errorf("Empty body")
}
// Set request body to buffered reader that can replay the read and execute Seek
req.SetBody(body)
// Note that we don't change the original request Body as it's handled by the http server
defer body.Close()
for {
_, err := req.GetBody().Seek(0, 0)
if err != nil {
return nil, err
}
endpoint, err := l.loadBalancer.NextEndpoint(req)
if err != nil {
log.Errorf("Load Balancer failure: %s", err)
return nil, err
}
// Adds headers, changes urls. Note that we rewrite request each time we proxy it to the
// endpoint, so that each try gets a fresh start
req.SetHttpRequest(l.copyRequest(originalRequest, req.GetBody(), endpoint))
// In case if error is not nil, we allow load balancer to choose the next endpoint
// e.g. to do request failover. Nil error means that we got proxied the request successfully.
response, err := l.proxyToEndpoint(tr, &o, endpoint, req)
if o.ShouldFailover(req) {
continue
} else {
return response, err
}
}
log.Errorf("All endpoints failed!")
return nil, fmt.Errorf("All endpoints failed")
}