Skip to content

Commit

Permalink
use golangci-lint (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
azazeal committed Aug 13, 2022
1 parent baeadc2 commit 71de098
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 113 deletions.
47 changes: 32 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,52 @@ on:
- opened
- synchronize

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Run linter
uses: docker://morphy/revive-action:v2
- name: Setup Go
uses: actions/setup-go@v3
- name: Checkout Code
uses: actions/checkout@v3
- name: Configure Linter
run: |
curl -O https://raw.githubusercontent.com/azazeal/workflows/master/.golangci.yml
- name: Run Linter
uses: golangci/golangci-lint-action@v3
with:
config: "./revive.toml"
path: "./..."
version: v1.48

test:
name: Test
needs: lint
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: '1.18'
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
- name: Install gotestsum
run: |
go install gotest.tools/gotestsum@v1.8.1
- name: Checkout Code
uses: actions/checkout@v3
- name: Run Test Suite
run: |
go test -race -coverpkg=./... -coverprofile=coverage.out -covermode=atomic ./...
- name: Upload coverage report
if: ${{ hashFiles('coverage.out') != '' }}
gotestsum -- -race -coverpkg=./... -coverprofile=coverprofile -covermode=atomic ./...
env:
GOTESTSUM_JSONFILE: gotestsum.json
- name: Annotate Test Suite Results
if: always()
uses: guyarb/golang-test-annotations@v0.5.1
with:
test-results: gotestsum.json
- name: Start Uploading Coverage Report
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
path-to-profile: coverprofile
36 changes: 1 addition & 35 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package app
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"html/template"
golog "log"
Expand Down Expand Up @@ -141,38 +140,13 @@ type indexViewData struct {
Failures []string
}

func broadcast(w http.ResponseWriter, r *http.Request) {
logger := log.FromContext(r.Context())

var wrapper struct {
Globally bool `json:"globally"`
Body []byte `json:"data"`
}
if err := json.NewDecoder(r.Body).Decode(&wrapper); err != nil {
logger.Warn("failed decoding message.",
zap.Error(err))

respondWith(w, http.StatusUnprocessableEntity)

return
}

logger = logger.
With(log.Data(wrapper.Body)).
With(zap.Bool("globally", wrapper.Globally))

// TODO: implement via implementing a broadcast.Broadcaster struct

respondWith(w, http.StatusNoContent)
}

func index(w http.ResponseWriter, r *http.Request) {
hc := health.FromContext(r.Context())

failures := hc.Failing(nil)
sort.Strings(failures)

indexTemplate.Execute(w, indexViewData{
_ = indexTemplate.Execute(w, indexViewData{
AppName: common.AppName,
Region: env.Region(),
Failures: failures,
Expand All @@ -199,14 +173,6 @@ func newMux(ctx context.Context) (mux *http.ServeMux) {
return
}

func matchFunc(m *http.ServeMux, fn http.HandlerFunc, path string, methods ...string) {
m.Handle(path, restrict(fn, path, methods...))
}

func match(m *http.ServeMux, h http.Handler, path string, methods ...string) {
m.Handle(path, restrict(h, path, methods...))
}

func restrict(h http.Handler, path string, methods ...string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
Expand Down
20 changes: 6 additions & 14 deletions internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@ import "sync"
// Size denotes the size of all buffers.
const Size = 1 << 14

// Dup returns a buffer which contains a copy of msg.
//
// the buffer may be released to the pool.
func Dup(msg []byte) (buf []byte) {
buf = Get()
copy(buf[:len(msg):Size], msg)

return
}
type Buffer [Size]byte

// Get returns an available packet buffer.
func Get() []byte {
return pool.Get().([]byte)
func Get() *Buffer {
return pool.Get().(*Buffer)
}

// Put releases the buffer
func Put(b []byte) {
pool.Put(b[:Size:Size])
func Put(b *Buffer) {
pool.Put(b)
}

var pool = sync.Pool{
New: func() interface{} {
return make([]byte, Size, Size)
return new(Buffer)
},
}
4 changes: 0 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ func NewContext(ctx context.Context, cfg *Config) context.Context {
const pkg = "config"

var (
errSetDefaults = exit.Wrapf(common.ECSetEnvDefaults,
"%s/%s: failed setting environment defaults",
common.AppName, pkg)

errNotOnFly = exit.Wrapf(common.ECNotOnFly,
"%s/%s: not running on fly",
common.AppName, pkg)
Expand Down
9 changes: 3 additions & 6 deletions internal/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package peer

import (
"context"
"errors"
"net"
"sync"
"time"
Expand All @@ -10,7 +11,6 @@ import (
"github.com/azazeal/health"
"go.uber.org/zap"

"github.com/azazeal/flycast/internal/buffer"
"github.com/azazeal/flycast/internal/config"
"github.com/azazeal/flycast/internal/log"
"github.com/azazeal/flycast/internal/loop"
Expand Down Expand Up @@ -113,9 +113,6 @@ func (l *List) refresh(ctx context.Context) {

// Broadcast relays the message to all of the peers in l via conn.
func (l *List) Broadcast(conn net.PacketConn, msg []byte) {
buf := buffer.Dup(msg)
defer buffer.Put(buf)

var wg sync.WaitGroup
defer wg.Wait()

Expand Down Expand Up @@ -186,6 +183,6 @@ var addrPool = sync.Pool{
}

func isNXDomain(err error) bool {
e, ok := err.(*net.DNSError)
return ok && e.IsNotFound
var w *net.DNSError
return errors.As(err, &w) && w.IsNotFound
}
18 changes: 9 additions & 9 deletions internal/wire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wire

import (
"context"
"errors"
"fmt"
"net"
"sync"
Expand Down Expand Up @@ -33,9 +34,6 @@ func Broadcast(ctx context.Context, wg *sync.WaitGroup, pl *peer.List, global bo
hcc = region.WireComponent(global)
)

buf := buffer.Get()
defer buffer.Put(buf)

go func() {
defer wg.Done()

Expand All @@ -48,6 +46,9 @@ func Broadcast(ctx context.Context, wg *sync.WaitGroup, pl *peer.List, global bo
}
hc.Pass(hcc)

buf := buffer.Get()
defer buffer.Put(buf)

run(ctx, &broadcaster{
logger: logger,
conn: conn,
Expand Down Expand Up @@ -86,7 +87,7 @@ type broadcaster struct {
logger *zap.Logger
conn net.PacketConn
pl *peer.List
buf []byte
buf *buffer.Buffer
}

func run(ctx context.Context, b *broadcaster) {
Expand Down Expand Up @@ -114,7 +115,7 @@ func run(ctx context.Context, b *broadcaster) {

for {
msg, err := b.read()
if err != nil && !canRecoverFrom(err) {
if err != nil && !isTimeout(err) {
return // terminal error
}
if len(msg) == 0 {
Expand Down Expand Up @@ -161,8 +162,7 @@ func (b *broadcaster) read() ([]byte, error) {
return b.buf[:n], err
}

func canRecoverFrom(err error) bool {
te, ok := err.(net.Error)

return ok && (te.Temporary() || te.Timeout())
func isTimeout(err error) bool {
var ne net.Error
return errors.As(err, &ne) && ne.Timeout()
}
30 changes: 0 additions & 30 deletions revive.toml

This file was deleted.

0 comments on commit 71de098

Please sign in to comment.