Skip to content

Commit

Permalink
Refactor client's example code (#1539)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Imm0bilize and erikdubbelboer committed Apr 18, 2023
1 parent 87cb886 commit cdea4fe
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions examples/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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
}

0 comments on commit cdea4fe

Please sign in to comment.