Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Aug 20, 2023
1 parent e70afe4 commit df5a083
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
33 changes: 27 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
12 changes: 6 additions & 6 deletions sfstreams.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sfstreams

import (
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -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)

Expand All @@ -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{
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit df5a083

Please sign in to comment.