aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGravatar Nikolay Vedernikov <nick.vedernikow@yandex.ru> 2023-04-18 08:52:18 +0300
committerGravatar GitHub <noreply@github.com> 2023-04-18 07:52:18 +0200
commitcdea4fe88e9d26f8e2403b65147a3a8a1fd8ebb4 (patch)
tree7bada93dc71749477250ebb16b3699ffee555f8e /examples
parentclient:Fix DoTimeout timeout failure by setting temporary dial (#1535) (diff)
downloadfasthttp-cdea4fe88e9d26f8e2403b65147a3a8a1fd8ebb4.tar.gz
fasthttp-cdea4fe88e9d26f8e2403b65147a3a8a1fd8ebb4.tar.bz2
fasthttp-cdea4fe88e9d26f8e2403b65147a3a8a1fd8ebb4.zip
Refactor client's example code (#1539)v1.46.0
* Refactor client's example code * Update examples/client/client.go Remove closure Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com> --------- Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/client/client.go71
1 files changed, 39 insertions, 32 deletions
diff --git a/examples/client/client.go b/examples/client/client.go
index 8402754..10bfa20 100644
--- a/examples/client/client.go
+++ b/examples/client/client.go
@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
+ "errors"
"fmt"
"io"
"net/http"
@@ -72,54 +73,60 @@ func sendPostRequest() {
req.Header.SetMethod(fasthttp.MethodPost)
req.Header.SetContentTypeBytes(headerContentTypeJson)
req.SetBodyRaw(reqEntityBytes)
+
resp := fasthttp.AcquireResponse()
err := client.DoTimeout(req, resp, reqTimeout)
fasthttp.ReleaseRequest(req)
- if err == nil {
- statusCode := resp.StatusCode()
- respBody := resp.Body()
- fmt.Printf("DEBUG Response: %s\n", respBody)
- if statusCode == http.StatusOK {
- respEntity := &Entity{}
- err = json.Unmarshal(respBody, respEntity)
- if err == io.EOF || err == nil {
- fmt.Printf("DEBUG Parsed Response: %v\n", respEntity)
- } else {
- fmt.Fprintf(os.Stderr, "ERR failed to parse response: %v\n", err)
- }
- } else {
- fmt.Fprintf(os.Stderr, "ERR invalid HTTP response code: %d\n", statusCode)
- }
- } else {
+ defer fasthttp.ReleaseResponse(resp)
+
+ if err != nil {
errName, known := httpConnError(err)
if known {
fmt.Fprintf(os.Stderr, "WARN conn error: %v\n", errName)
} else {
fmt.Fprintf(os.Stderr, "ERR conn failure: %v %v\n", errName, err)
}
+
+ return
+ }
+
+ statusCode := resp.StatusCode()
+ respBody := resp.Body()
+ fmt.Printf("DEBUG Response: %s\n", respBody)
+
+ if statusCode != http.StatusOK {
+ fmt.Fprintf(os.Stderr, "ERR invalid HTTP response code: %d\n", statusCode)
+
+ return
+ }
+
+ respEntity := &Entity{}
+ err = json.Unmarshal(respBody, respEntity)
+ if err == nil || errors.Is(err, io.EOF) {
+ fmt.Printf("DEBUG Parsed Response: %v\n", respEntity)
+ } else {
+ fmt.Fprintf(os.Stderr, "ERR failed to parse response: %v\n", err)
}
- fasthttp.ReleaseResponse(resp)
}
func httpConnError(err error) (string, bool) {
- errName := ""
- known := false
- if err == fasthttp.ErrTimeout {
+ var (
+ errName string
+ known = true
+ )
+
+ switch {
+ case errors.Is(err, fasthttp.ErrTimeout):
errName = "timeout"
- known = true
- } else if err == fasthttp.ErrNoFreeConns {
+ case errors.Is(err, fasthttp.ErrTimeout):
errName = "conn_limit"
- known = true
- } else if err == fasthttp.ErrConnectionClosed {
+ case errors.Is(err, fasthttp.ErrConnectionClosed):
errName = "conn_close"
- known = true
- } else {
- errName = reflect.TypeOf(err).String()
- if errName == "*net.OpError" {
- // Write and Read errors are not so often and in fact they just mean timeout problems
- errName = "timeout"
- known = true
- }
+ case reflect.TypeOf(err).String() == "*net.OpError":
+ errName = "timeout"
+ default:
+ known = false
}
+
return errName, known
}