Skip to content

Commit

Permalink
Remove ioutil usage
Browse files Browse the repository at this point in the history
This has been deprecated in Go for some time, in favour of the
io package.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alex@openfaas.com>
  • Loading branch information
alexellis committed Aug 22, 2023
1 parent 55776ac commit 2a88b5d
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions gateway/handlers/alerthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package handlers
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"math"
"net/http"
Expand All @@ -27,7 +27,7 @@ func MakeAlertHandler(service scaling.ServiceQuery, defaultNamespace string) htt

defer r.Body.Close()

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Unable to read alert."))
Expand Down
10 changes: 5 additions & 5 deletions gateway/handlers/forwarding_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package handlers
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"testing"
Expand All @@ -33,7 +33,7 @@ func Test_buildUpstreamRequest_Body_Method_Query(t *testing.T) {
t.Fail()
}

upstreamBytes, _ := ioutil.ReadAll(upstream.Body)
upstreamBytes, _ := io.ReadAll(upstream.Body)

if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
Expand Down Expand Up @@ -212,7 +212,7 @@ func Test_buildUpstreamRequest_WithPathNoQuery(t *testing.T) {
t.Fail()
}

upstreamBytes, _ := ioutil.ReadAll(upstream.Body)
upstreamBytes, _ := io.ReadAll(upstream.Body)

if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
Expand Down Expand Up @@ -268,7 +268,7 @@ func Test_buildUpstreamRequest_WithNoPathNoQuery(t *testing.T) {
t.Fail()
}

upstreamBytes, _ := ioutil.ReadAll(upstream.Body)
upstreamBytes, _ := io.ReadAll(upstream.Body)

if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
Expand Down Expand Up @@ -322,7 +322,7 @@ func Test_buildUpstreamRequest_WithPathAndQuery(t *testing.T) {
t.Fail()
}

upstreamBytes, _ := ioutil.ReadAll(upstream.Body)
upstreamBytes, _ := io.ReadAll(upstream.Body)

if string(upstreamBytes) != string(srcBytes) {
t.Errorf("Body - want: %s, got: %s", string(upstreamBytes), string(srcBytes))
Expand Down
6 changes: 3 additions & 3 deletions gateway/handlers/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handlers
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -52,7 +52,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenProviderClosesConnection(t *testing.
t.Fatalf("unexpected error sending log request: %s", err)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error reading the response body: %s", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func Test_logsProxyDoesNotLeakGoroutinesWhenClientClosesConnection(t *testing.T)
go func() {
defer resp.Body.Close()
defer close(errCh)
_, err := ioutil.ReadAll(resp.Body)
_, err := io.ReadAll(resp.Body)
errCh <- err
}()
cancel()
Expand Down
4 changes: 2 additions & 2 deletions gateway/handlers/queue_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package handlers

import (
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand All @@ -27,7 +27,7 @@ func MakeQueuedProxy(metrics metrics.MetricOptions, queuer ftypes.RequestQueuer,
defer r.Body.Close()

var err error
body, err = ioutil.ReadAll(r.Body)
body, err = io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
Expand Down
4 changes: 2 additions & 2 deletions gateway/metrics/add_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package metrics
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -28,7 +28,7 @@ func AddMetricsHandler(handler http.HandlerFunc, prometheusQuery PrometheusQuery
}

defer upstreamCall.Body.Close()
upstreamBody, _ := ioutil.ReadAll(upstreamCall.Body)
upstreamBody, _ := io.ReadAll(upstreamCall.Body)

if recorder.Code != http.StatusOK {
log.Printf("List functions responded with code %d, body: %s",
Expand Down
6 changes: 3 additions & 3 deletions gateway/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package metrics
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -159,7 +159,7 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types.
return services, err
}

bytesOut, readErr := ioutil.ReadAll(res.Body)
bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil {
return services, readErr
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) {
return namespaces, nil
}

bytesOut, readErr := ioutil.ReadAll(res.Body)
bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil {
return namespaces, readErr
}
Expand Down
4 changes: 2 additions & 2 deletions gateway/metrics/prometheus_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package metrics
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func (q PrometheusQuery) Fetch(query string) (*VectorQueryResponse, error) {
defer res.Body.Close()
}

bytesOut, readErr := ioutil.ReadAll(res.Body)
bytesOut, readErr := io.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
Expand Down
3 changes: 2 additions & 1 deletion gateway/scaling/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scaling
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"

Expand Down Expand Up @@ -43,7 +44,7 @@ func MakeHorizontalScalingHandler(next http.HandlerFunc) http.HandlerFunc {
return
}

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
Expand Down

0 comments on commit 2a88b5d

Please sign in to comment.