Skip to content

Commit

Permalink
re-enable ifElseChain in go critic (#881)
Browse files Browse the repository at this point in the history
* re-enable ifElseChain in go critic

* adding (previously) missing test

* bump release to 1.63.2 (for new go and x.net more than for this particular linter change)
  • Loading branch information
ldemailly committed Jan 15, 2024
1 parent 1dd6791 commit a9a6b4e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

# all available settings of specific linters
linters-settings:
gocritic:
disabled-checks:
- ifElseChain
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- 1.63.1 -->
<!-- 1.63.2 -->
# Fortio

[![Awesome Go](https://fortio.org/mentioned-badge.svg)](https://github.com/avelino/awesome-go#networking)
Expand Down Expand Up @@ -60,13 +60,13 @@ You can install from source:
The [releases](https://github.com/fortio/fortio/releases) page has binaries for many OS/architecture combinations (see assets):

```shell
curl -L https://github.com/fortio/fortio/releases/download/v1.63.1/fortio-linux_amd64-1.63.1.tgz \
curl -L https://github.com/fortio/fortio/releases/download/v1.63.2/fortio-linux_amd64-1.63.2.tgz \
| sudo tar -C / -xvzpf -
# or the debian package
wget https://github.com/fortio/fortio/releases/download/v1.63.1/fortio_1.63.1_amd64.deb
dpkg -i fortio_1.63.1_amd64.deb
wget https://github.com/fortio/fortio/releases/download/v1.63.2/fortio_1.63.2_amd64.deb
dpkg -i fortio_1.63.2_amd64.deb
# or the rpm
rpm -i https://github.com/fortio/fortio/releases/download/v1.63.1/fortio-1.63.1-1.x86_64.rpm
rpm -i https://github.com/fortio/fortio/releases/download/v1.63.2/fortio-1.63.2-1.x86_64.rpm
# and more, see assets in release page
```

Expand All @@ -76,7 +76,7 @@ On a MacOS you can also install Fortio using [Homebrew](https://brew.sh/):
brew install fortio
```

On Windows, download https://github.com/fortio/fortio/releases/download/v1.63.1/fortio_win_1.63.1.zip and extract `fortio.exe` to any location, then using the Windows Command Prompt:
On Windows, download https://github.com/fortio/fortio/releases/download/v1.63.2/fortio_win_1.63.2.zip and extract `fortio.exe` to any location, then using the Windows Command Prompt:
```
fortio.exe server
```
Expand Down Expand Up @@ -130,7 +130,7 @@ Full list of command line flags (`fortio help`):
<!-- use release/updateFlags.sh to update this section -->
<pre>
<!-- USAGE_START -->
Φορτίο 1.63.1 usage:
Φορτίο 1.63.2 usage:
fortio command [flags] target
where command is one of: load (load testing), server (starts ui, rest api,
http-echo, redirect, proxies, tcp-echo, udp-echo and grpc ping servers),
Expand Down
11 changes: 6 additions & 5 deletions cli/fortio_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func fortioNC() {
}
}

//nolint:funlen, gocognit // maybe refactor/shorten later.
//nolint:funlen // maybe refactor/shorten later.
func fortioLoad(justCurl bool, percList []float64, hook bincommon.FortioHook) {
if len(flag.Args()) != 1 {
cli.ErrUsage("Error: fortio load/curl needs a url or destination")
Expand Down Expand Up @@ -440,7 +440,8 @@ func fortioLoad(justCurl bool, percList []float64, hook bincommon.FortioHook) {
if hook != nil {
hook(httpOpts, &ro)
}
if *grpcFlag {
switch {
case *grpcFlag:
o := fgrpc.GRPCRunnerOptions{
RunnerOptions: ro,
Destination: url,
Expand All @@ -456,23 +457,23 @@ func fortioLoad(justCurl bool, percList []float64, hook bincommon.FortioHook) {
}
o.TLSOptions = httpOpts.TLSOptions
res, err = fgrpc.RunGRPCTest(&o)
} else if strings.HasPrefix(url, tcprunner.TCPURLPrefix) {
case strings.HasPrefix(url, tcprunner.TCPURLPrefix):
o := tcprunner.RunnerOptions{
RunnerOptions: ro,
}
o.ReqTimeout = httpOpts.HTTPReqTimeOut
o.Destination = url
o.Payload = httpOpts.Payload
res, err = tcprunner.RunTCPTest(&o)
} else if strings.HasPrefix(url, udprunner.UDPURLPrefix) {
case strings.HasPrefix(url, udprunner.UDPURLPrefix):
o := udprunner.RunnerOptions{
RunnerOptions: ro,
}
o.ReqTimeout = *udpTimeoutFlag
o.Destination = url
o.Payload = httpOpts.Payload
res, err = udprunner.RunUDPTest(&o)
} else {
default:
o := fhttp.HTTPRunnerOptions{
HTTPOptions: *httpOpts,
RunnerOptions: ro,
Expand Down
7 changes: 4 additions & 3 deletions fhttp/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,8 @@ func (c *FastClient) readResponse(conn net.Conn, reusedSocket bool) {
if chunkedMode {
// Next chunk:
dataStart, nextChunkLen := ParseChunkSize(c.buffer[max:c.size])
if nextChunkLen == -1 {
switch nextChunkLen {
case -1:
if c.size == max {
log.Debugf("[%d] Couldn't find next chunk size, reading more %d %d", c.id, max, c.size)
} else {
Expand All @@ -1223,15 +1224,15 @@ func (c *FastClient) readResponse(conn net.Conn, reusedSocket bool) {
log.Attr("thread", c.id), log.Attr("run", c.runID))
}
continue
} else if nextChunkLen == 0 {
case 0:
log.Debugf("[%d] Found last chunk %d %d", c.id, max+dataStart, c.size)
if c.size != max+dataStart+2 || string(c.buffer[c.size-2:c.size]) != "\r\n" {
log.S(log.Error, "Unexpected mismatch at the end",
log.Attr("size", c.size), log.Attr("expected", max+dataStart+2),
log.Attr("end-of_buffer", c.buffer[max:c.size]),
log.Attr("thread", c.id), log.Attr("run", c.runID))
}
} else {
default:
max += dataStart + nextChunkLen + 2 // extra CR LF
log.Debugf("[%d] One more chunk %d -> new max %d", c.id, nextChunkLen, max)
if max > int64(len(c.buffer)) {
Expand Down
7 changes: 4 additions & 3 deletions fhttp/http_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,12 @@ func ParseChunkSize(inp []byte) (int64, int64) {
if inDigits { //nolint:nestif
b := toUpper(inp[off])
var digit int64
if b >= 'A' && b <= 'F' {
switch {
case b >= 'A' && b <= 'F':
digit = 10 + int64(b-'A')
} else if b >= '0' && b <= '9' {
case b >= '0' && b <= '9':
digit = int64(b - '0')
} else {
default:
inDigits = false
if res == -1 {
log.Errf("Didn't find hex number %q", inp)
Expand Down
7 changes: 4 additions & 3 deletions jrpc/jrpcClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,14 @@ func Send(dest *Destination, jsonPayload []byte) (int, []byte, error) {
SetHeaderIfMissing(req.Header, "Accept", "application/json")
SetHeaderIfMissing(req.Header, UserAgentHeader, UserAgent)
var client *http.Client
if dest.Client != nil {
switch {
case dest.Client != nil:
client = dest.Client
} else if dest.TLSConfig != nil {
case dest.TLSConfig != nil:
transport := http.DefaultTransport.(*http.Transport).Clone() // Let it crash/panic if somehow DefaultTransport is not a Transport
transport.TLSClientConfig = dest.TLSConfig
client = &http.Client{Transport: transport}
} else {
default:
client = http.DefaultClient
}
var resp *http.Response
Expand Down
7 changes: 4 additions & 3 deletions periodic/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,12 @@ func (r *RunnerOptions) AddAccessLogger(filePath, format string) error {
func NewFileAccessLogger(filePath, format string) (AccessLogger, error) {
var t AccessLoggerType
fl := strings.ToLower(format)
if fl == "json" {
switch fl {
case "json":
t = AccessJSON
} else if fl == "influx" {
case "influx":
t = AccessInflux
} else {
default:
err := fmt.Errorf("invalid format %q, should be \"json\" or \"influx\"", format)
log.Errf("%v", err)
return nil, err
Expand Down
14 changes: 14 additions & 0 deletions periodic/periodic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ func TestAccessLogs(t *testing.T) {
r.Options().ReleaseRunners()
}

func TestAccessLogFileInvalidFormat(t *testing.T) {
o := RunnerOptions{}

err := o.AddAccessLogger("foo.log", "unknown")
if err == nil {
t.Fatalf("expected error for unknown format")
}
expected := `invalid format "unknown", should be "json" or "influx"`
actual := err.Error()
if actual != expected {
t.Errorf("unexpected error message: %q vs %q", actual, expected)
}
}

func TestAccessLogFile(t *testing.T) {
var count int64
var lock sync.Mutex
Expand Down
9 changes: 5 additions & 4 deletions rapi/restHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ func Run(w http.ResponseWriter, r *http.Request, jd map[string]interface{},
if hook != nil {
hook(httpopts, ro)
}
if runner == ModeGRPC { //nolint:nestif
switch {
case runner == ModeGRPC:
grpcSecure := (FormValue(r, jd, "grpc-secure") == "on")
grpcPing := (FormValue(r, jd, "ping") == "on")
grpcPingDelay, _ := time.ParseDuration(FormValue(r, jd, "grpc-ping-delay"))
Expand All @@ -382,7 +383,7 @@ func Run(w http.ResponseWriter, r *http.Request, jd map[string]interface{},
aborter = UpdateRun(&o.RunnerOptions)
// TODO: ReqTimeout: timeout
res, err = fgrpc.RunGRPCTest(&o)
} else if strings.HasPrefix(url, tcprunner.TCPURLPrefix) {
case strings.HasPrefix(url, tcprunner.TCPURLPrefix):
// TODO: copy pasta from fortio_main
o := tcprunner.RunnerOptions{
RunnerOptions: *ro,
Expand All @@ -392,7 +393,7 @@ func Run(w http.ResponseWriter, r *http.Request, jd map[string]interface{},
o.Payload = httpopts.Payload
aborter = UpdateRun(&o.RunnerOptions)
res, err = tcprunner.RunTCPTest(&o)
} else if strings.HasPrefix(url, udprunner.UDPURLPrefix) {
case strings.HasPrefix(url, udprunner.UDPURLPrefix):
// TODO: copy pasta from fortio_main
o := udprunner.RunnerOptions{
RunnerOptions: *ro,
Expand All @@ -402,7 +403,7 @@ func Run(w http.ResponseWriter, r *http.Request, jd map[string]interface{},
o.Payload = httpopts.Payload
aborter = UpdateRun(&o.RunnerOptions)
res, err = udprunner.RunUDPTest(&o)
} else {
default:
o := fhttp.HTTPRunnerOptions{
HTTPOptions: *httpopts,
RunnerOptions: *ro,
Expand Down
14 changes: 8 additions & 6 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ func (c *Counter) Record(v float64) {
func (c *Counter) RecordN(v float64, n int) {
isFirst := (c.Count == 0)
c.Count += int64(n)
if isFirst {
switch {
case isFirst:
c.Min = v
c.Max = v
} else if v < c.Min {
case v < c.Min:
c.Min = v
} else if v > c.Max {
case v > c.Max:
c.Max = v
}
s := v * float64(n)
Expand Down Expand Up @@ -277,11 +278,12 @@ func (h *Histogram) record(v float64, count int) {
// math.Ceil()-1 but that doesn't work... so back to epsilon distance check
scaledVal := (v - h.Offset) / h.Divider
var idx int
if scaledVal <= firstValue {
switch {
case scaledVal <= firstValue:
idx = 0
} else if scaledVal > lastValue {
case scaledVal > lastValue:
idx = numBuckets - 1 // last bucket is for > last value
} else {
default:
// else we look it up (with the open interval adjustment)
svInt := int(scaledVal)
delta := scaledVal - float64(svInt)
Expand Down

0 comments on commit a9a6b4e

Please sign in to comment.