41 lines
545 B
Go
41 lines
545 B
Go
package accumulator
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
func NewAccumulator(in <-chan [3]int) *Accumulator {
|
|
return &Accumulator{in: in}
|
|
}
|
|
|
|
type Accumulator struct {
|
|
in <-chan [3]int
|
|
summ atomic.Int64
|
|
}
|
|
|
|
func (a *Accumulator) Start(ctx context.Context, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
|
|
case m, ok := <-a.in:
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
for _, value := range m {
|
|
a.summ.Add(int64(value))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *Accumulator) Res() int64 {
|
|
return a.summ.Load()
|
|
}
|