Skip to content

Commit cdea4fe

Browse files
Refactor client's example code (#1539)
* 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>
1 parent 87cb886 commit cdea4fe

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

examples/client/client.go

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
78
"net/http"
@@ -72,54 +73,60 @@ func sendPostRequest() {
7273
req.Header.SetMethod(fasthttp.MethodPost)
7374
req.Header.SetContentTypeBytes(headerContentTypeJson)
7475
req.SetBodyRaw(reqEntityBytes)
76+
7577
resp := fasthttp.AcquireResponse()
7678
err := client.DoTimeout(req, resp, reqTimeout)
7779
fasthttp.ReleaseRequest(req)
78-
if err == nil {
79-
statusCode := resp.StatusCode()
80-
respBody := resp.Body()
81-
fmt.Printf("DEBUG Response: %s\n", respBody)
82-
if statusCode == http.StatusOK {
83-
respEntity := &Entity{}
84-
err = json.Unmarshal(respBody, respEntity)
85-
if err == io.EOF || err == nil {
86-
fmt.Printf("DEBUG Parsed Response: %v\n", respEntity)
87-
} else {
88-
fmt.Fprintf(os.Stderr, "ERR failed to parse response: %v\n", err)
89-
}
90-
} else {
91-
fmt.Fprintf(os.Stderr, "ERR invalid HTTP response code: %d\n", statusCode)
92-
}
93-
} else {
80+
defer fasthttp.ReleaseResponse(resp)
81+
82+
if err != nil {
9483
errName, known := httpConnError(err)
9584
if known {
9685
fmt.Fprintf(os.Stderr, "WARN conn error: %v\n", errName)
9786
} else {
9887
fmt.Fprintf(os.Stderr, "ERR conn failure: %v %v\n", errName, err)
9988
}
89+
90+
return
91+
}
92+
93+
statusCode := resp.StatusCode()
94+
respBody := resp.Body()
95+
fmt.Printf("DEBUG Response: %s\n", respBody)
96+
97+
if statusCode != http.StatusOK {
98+
fmt.Fprintf(os.Stderr, "ERR invalid HTTP response code: %d\n", statusCode)
99+
100+
return
101+
}
102+
103+
respEntity := &Entity{}
104+
err = json.Unmarshal(respBody, respEntity)
105+
if err == nil || errors.Is(err, io.EOF) {
106+
fmt.Printf("DEBUG Parsed Response: %v\n", respEntity)
107+
} else {
108+
fmt.Fprintf(os.Stderr, "ERR failed to parse response: %v\n", err)
100109
}
101-
fasthttp.ReleaseResponse(resp)
102110
}
103111

104112
func httpConnError(err error) (string, bool) {
105-
errName := ""
106-
known := false
107-
if err == fasthttp.ErrTimeout {
113+
var (
114+
errName string
115+
known = true
116+
)
117+
118+
switch {
119+
case errors.Is(err, fasthttp.ErrTimeout):
108120
errName = "timeout"
109-
known = true
110-
} else if err == fasthttp.ErrNoFreeConns {
121+
case errors.Is(err, fasthttp.ErrTimeout):
111122
errName = "conn_limit"
112-
known = true
113-
} else if err == fasthttp.ErrConnectionClosed {
123+
case errors.Is(err, fasthttp.ErrConnectionClosed):
114124
errName = "conn_close"
115-
known = true
116-
} else {
117-
errName = reflect.TypeOf(err).String()
118-
if errName == "*net.OpError" {
119-
// Write and Read errors are not so often and in fact they just mean timeout problems
120-
errName = "timeout"
121-
known = true
122-
}
125+
case reflect.TypeOf(err).String() == "*net.OpError":
126+
errName = "timeout"
127+
default:
128+
known = false
123129
}
130+
124131
return errName, known
125132
}

0 commit comments

Comments
 (0)