Skip to content

Commit

Permalink
ci: lint (#142)
Browse files Browse the repository at this point in the history
* add staticcheck and fix check error

* use golangci-lint
  • Loading branch information
acoshift committed May 10, 2023
1 parent 2958d3b commit e799df9
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 22 deletions.
17 changes: 16 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ name: Test
on:
push:
pull_request:
permissions:
contents: read
jobs:
runner-job:
lint:
name: lint
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.20']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
test:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
default: vet lint test

test:
go test -race ./...

vet:
go vet ./...

lint:
golangci-lint run
2 changes: 1 addition & 1 deletion pkg/authn/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (m ForwardAuthenticator) ServeHandler(h http.Handler) http.Handler {

buf := pool.Get()
defer pool.Put(buf)
io.CopyBuffer(w, resp.Body, buf)
io.CopyBuffer(w, resp.Body, *buf)
},
}.ServeHandler(h)
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/body/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -35,7 +34,7 @@ func (m RequestBufferer) ServeHandler(h http.Handler) http.Handler {
b := pool.Get()
defer pool.Put(b)

n, err := io.ReadAtLeast(r.Body, b, int(r.ContentLength))
n, err := io.ReadAtLeast(r.Body, *b, int(r.ContentLength))
if err == io.EOF {
err = nil
}
Expand All @@ -45,7 +44,7 @@ func (m RequestBufferer) ServeHandler(h http.Handler) http.Handler {
}

r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewReader(b[:n]))
r.Body = io.NopCloser(bytes.NewReader((*b)[:n]))

if r.Context().Err() == context.Canceled {
return
Expand All @@ -56,7 +55,7 @@ func (m RequestBufferer) ServeHandler(h http.Handler) http.Handler {
}

// case 3: body larger than buffer size or unknown size, then buffer to file
fp, err := ioutil.TempFile("", "request-*")
fp, err := os.CreateTemp("", "request-*")
if err != nil {
log.Println("can not create temp file;", err)

Expand All @@ -70,7 +69,7 @@ func (m RequestBufferer) ServeHandler(h http.Handler) http.Handler {
}()

b := pool.Get()
_, err = io.CopyBuffer(fp, r.Body, b)
_, err = io.CopyBuffer(fp, r.Body, *b)
pool.Put(b)
if err == io.EOF {
err = nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/compress/deflate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package compress

import (
"compress/flate"
"io/ioutil"
"io"
)

// Deflate creates new deflate compress middleware
func Deflate() *Compress {
return &Compress{
New: func() Compressor {
g, err := flate.NewWriter(ioutil.Discard, flate.DefaultCompression)
g, err := flate.NewWriter(io.Discard, flate.DefaultCompression)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/compress/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package compress

import (
"compress/gzip"
"io/ioutil"
"io"
)

// Gzip creates new gzip compress middleware
func Gzip() *Compress {
return &Compress{
New: func() Compressor {
g, err := gzip.NewWriterLevel(ioutil.Discard, gzip.DefaultCompression)
g, err := gzip.NewWriterLevel(io.Discard, gzip.DefaultCompression)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (m GCS) ServeHandler(h http.Handler) http.Handler {

b := pool.Get()
defer pool.Put(b)
io.CopyBuffer(w, reader, b)
io.CopyBuffer(w, reader, *b)
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
)

// Get gets bytes from pool
func Get() []byte {
return bytesPool.Get().([]byte)
func Get() *[]byte {
return bytesPool.Get().(*[]byte)
}

// Put puts bytes back to pool
func Put(b []byte) {
func Put(b *[]byte) {
bytesPool.Put(b)
}

Expand All @@ -22,7 +22,8 @@ func Size() int64 {
const bufferSize = 16 * 1024 // 16 KiB

var bytesPool = sync.Pool{
New: func() interface{} {
return make([]byte, bufferSize)
New: func() any {
b := make([]byte, bufferSize)
return &b
},
}
5 changes: 3 additions & 2 deletions pkg/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

Expand All @@ -15,8 +16,8 @@ var reg = prometheus.NewRegistry()
var Namespace = "parapet"

func init() {
reg.MustRegister(prometheus.NewGoCollector())
reg.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{
reg.MustRegister(collectors.NewGoCollector())
reg.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{
PidFn: func() (int, error) { return os.Getpid(), nil },
}))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/upstream/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var bytesPool = &_bytesPool{}
type _bytesPool struct{}

func (p _bytesPool) Get() []byte {
return pool.Get()
return *pool.Get()
}

func (p _bytesPool) Put(b []byte) {
pool.Put(b)
pool.Put(&b)
}
1 change: 0 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
type Server struct {
s http.Server
once sync.Once
trackState sync.Once
ms Middlewares
onShutdown []func()
modifyConn []func(conn net.Conn) net.Conn
Expand Down

0 comments on commit e799df9

Please sign in to comment.