-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove unneded interfaces, extend sized group with the same params as…
… err group
- Loading branch information
Showing
10 changed files
with
157 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/go-pkgz/syncs | ||
|
||
require github.com/stretchr/testify v1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= | ||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package syncs | ||
|
||
import "context" | ||
|
||
type options struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
preLock bool | ||
termOnError bool | ||
} | ||
|
||
// GroupOption functional option type | ||
type GroupOption func(o *options) | ||
|
||
// Context passes ctx and makes it cancelable | ||
func Context(ctx context.Context) GroupOption { | ||
return func(o *options) { | ||
o.ctx, o.cancel = context.WithCancel(ctx) | ||
} | ||
} | ||
|
||
// Preemptive sets locking mode preventing spawning waiting goroutine. May cause Go call to block! | ||
func Preemptive(o *options) { | ||
o.preLock = true | ||
} | ||
|
||
// TermOnErr prevents new goroutines to start after first error | ||
func TermOnErr(o *options) { | ||
o.termOnError = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,70 @@ | ||
package syncs | ||
|
||
import "sync" | ||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// SizedGroup has the same role as WaitingGroup but adds a limit of the amount of goroutines started concurrently. | ||
// Uses similar Go() scheduling as errgrp.Group, thread safe. | ||
// SizedGroup interface enforces constructor usage and doesn't allow direct creation of sizedGroup | ||
type SizedGroup interface { | ||
Go(fn func()) | ||
Wait() | ||
} | ||
|
||
type sizedGroup struct { | ||
type SizedGroup struct { | ||
options | ||
wg sync.WaitGroup | ||
sema sync.Locker | ||
} | ||
|
||
// NewSizedGroup makes wait group with limited size alive goroutines | ||
func NewSizedGroup(size int) SizedGroup { | ||
return &sizedGroup{sema: NewSemaphore(size)} | ||
func NewSizedGroup(size int, opts ...GroupOption) *SizedGroup { | ||
res := SizedGroup{sema: NewSemaphore(size)} | ||
res.options.ctx = context.Background() | ||
for _, opt := range opts { | ||
opt(&res.options) | ||
} | ||
return &res | ||
} | ||
|
||
// Go calls the given function in a new goroutine. | ||
// Every call will be unblocked, but some goroutines may wait if semaphore locked. | ||
func (g *sizedGroup) Go(fn func()) { | ||
func (g *SizedGroup) Go(fn func(ctx context.Context)) { | ||
|
||
canceled := func() bool { | ||
select { | ||
case <-g.ctx.Done(): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
if canceled() { | ||
return | ||
} | ||
|
||
g.wg.Add(1) | ||
|
||
if g.preLock { | ||
g.sema.Lock() | ||
} | ||
|
||
go func() { | ||
defer g.wg.Done() | ||
|
||
g.sema.Lock() | ||
fn() | ||
if canceled() { | ||
return | ||
} | ||
|
||
if !g.preLock { | ||
g.sema.Lock() | ||
} | ||
|
||
fn(g.ctx) | ||
g.sema.Unlock() | ||
}() | ||
} | ||
|
||
// Wait blocks until the SizedGroup counter is zero. | ||
// See sync.WaitGroup documentation for more information. | ||
func (g *sizedGroup) Wait() { | ||
func (g *SizedGroup) Wait() { | ||
g.wg.Wait() | ||
} |
Oops, something went wrong.