Initial commit
This commit is contained in:
40
accumulator/accumulator.go
Normal file
40
accumulator/accumulator.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user