From df5a0835259b7dc01e26508c051edf1e98271c29 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Sun, 20 Aug 2023 00:44:40 -0600 Subject: [PATCH] Code quality improvements --- .github/workflows/main.yml | 33 +++++++++++++++++++++++++++------ go.mod | 4 ++-- go.sum | 4 ++-- sfstreams.go | 12 ++++++------ 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 738c75c..d39f3c1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,16 +2,37 @@ name: Go package on: [push] jobs: build: + name: 'Go Build (1.20)' runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Setup Go - uses: actions/setup-go@v4 + - uses: actions/setup-go@v4 with: - go-version: '1.19' + go-version: '1.20' - name: Install dependencies run: go get . - - name: Build - run: go build - name: Test - run: go test \ No newline at end of file + run: go test + static: + name: 'Go Static (1.20)' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + run: 'go install honnef.co/go/tools/cmd/staticcheck@latest' + - run: 'go vet ./...' + - run: 'staticcheck ./...' + test: + name: 'Go Test (1.20)' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + - name: Install dependencies + run: go get . + - name: Test + run: go test diff --git a/go.mod b/go.mod index e294ea9..645e8ca 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,5 @@ module github.com/t2bot/go-singleflight-streams -go 1.19 +go 1.20 -require golang.org/x/sync v0.2.0 +require golang.org/x/sync v0.3.0 diff --git a/go.sum b/go.sum index 4051f4f..4c4db29 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= -golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= diff --git a/sfstreams.go b/sfstreams.go index 9e51b89..0fa1644 100644 --- a/sfstreams.go +++ b/sfstreams.go @@ -1,7 +1,6 @@ package sfstreams import ( - "errors" "fmt" "io" "sync" @@ -39,7 +38,7 @@ func (g *Group) Do(key string, fn func() (io.ReadCloser, error)) (reader io.Read if _, ok := g.calls[key]; !ok { g.calls[key] = make([]chan<- io.ReadCloser, 0) } - resCh := make(chan io.ReadCloser) + resCh := make(chan io.ReadCloser, 1) defer close(resCh) g.calls[key] = append(g.calls[key], resCh) @@ -54,7 +53,7 @@ func (g *Group) Do(key string, fn func() (io.ReadCloser, error)) (reader io.Read // // The returned channel is not closed. func (g *Group) DoChan(key string, fn func() (io.ReadCloser, error)) <-chan ReaderResult { - ch := make(chan ReaderResult) + ch := make(chan ReaderResult, 1) go func(ch chan ReaderResult, g *Group) { r, err, shared := g.Do(key, fn) ch <- ReaderResult{ @@ -87,7 +86,7 @@ func (g *Group) doWork(key string, fn func() (io.ReadCloser, error)) func() (int defer g.mu.Unlock() g.sf.Forget(key) // we won't be processing future calls, so wrap it up if chans, ok := g.calls[key]; !ok { - return nil, errors.New(fmt.Sprintf("expected to find singleflight key \"%s\", but didn't", key)) + return nil, fmt.Errorf("expected to find singleflight key \"%s\", but didn't", key) } else { skipStream := fnRes == nil writers := make([]io.Writer, 0) // they're actually PipeWriters, but the MultiWriter doesn't like that... @@ -121,8 +120,9 @@ func (g *Group) doWork(key string, fn func() (io.ReadCloser, error)) func() (int } func finishCopy(writers []io.Writer, fnRes io.ReadCloser) { - //goland:noinspection GoUnhandledErrorResult - defer fnRes.Close() + defer func(fnRes io.ReadCloser) { + _ = fnRes.Close() + }(fnRes) mw := io.MultiWriter(writers...) _, copyErr := io.Copy(mw, fnRes) for _, w := range writers {