generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pavel Pogodaev <[email protected]>
- Loading branch information
Pavel Pogodaev
committed
Jul 6, 2024
1 parent
411b524
commit 408b363
Showing
6 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
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,31 @@ | ||
package hw05parallelexecution | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type worker struct { | ||
taskCh chan Task | ||
} | ||
|
||
func newTasksSolver(chTask chan Task) *worker { | ||
return &worker{ | ||
taskCh: chTask, | ||
} | ||
} | ||
|
||
func (w *worker) Start(wg *sync.WaitGroup, limiter *limiter) { | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
for task := range w.taskCh { | ||
if task() != nil { | ||
limiter.increment() | ||
if limiter.isLimitExceeded() { | ||
return | ||
} | ||
} | ||
} | ||
}() | ||
} |
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,64 @@ | ||
package hw05parallelexecution | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
const chanSize = 1000 | ||
|
||
type limiter struct { | ||
count int64 | ||
limit int64 | ||
} | ||
|
||
type pool struct { | ||
tasks []Task | ||
routinesCount int | ||
collector chan Task | ||
wg sync.WaitGroup | ||
limiter *limiter | ||
} | ||
|
||
func newLimiter(limit int64) *limiter { | ||
return &limiter{ | ||
limit: limit, | ||
} | ||
} | ||
|
||
func newPool(tasks []Task, rCount int, maxErrorCount int64) *pool { | ||
return &pool{ | ||
tasks: tasks, | ||
routinesCount: rCount, | ||
collector: make(chan Task, chanSize), | ||
limiter: newLimiter(maxErrorCount), | ||
} | ||
} | ||
|
||
func (l *limiter) increment() { | ||
atomic.AddInt64(&l.count, 1) | ||
} | ||
|
||
func (l *limiter) isLimitExceeded() bool { | ||
return atomic.LoadInt64(&l.count) >= atomic.LoadInt64(&l.limit) | ||
} | ||
|
||
func (p *pool) run() error { | ||
for i := 0; i < p.routinesCount; i++ { | ||
w := newTasksSolver(p.collector) | ||
w.Start(&p.wg, p.limiter) | ||
} | ||
|
||
for _, task := range p.tasks { | ||
p.collector <- task | ||
} | ||
close(p.collector) | ||
|
||
p.wg.Wait() | ||
|
||
if p.limiter.isLimitExceeded() { | ||
return ErrErrorsLimitExceeded | ||
} | ||
|
||
return nil | ||
} |