Initial commit
This commit is contained in:
51
worker/worker.go
Normal file
51
worker/worker.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func NewWorker(in <-chan [10]int, out chan<- [3]int, log *zap.Logger) *Worker {
|
||||
return &Worker{
|
||||
in: in,
|
||||
out: out,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
in <-chan [10]int
|
||||
out chan<- [3]int
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func (w *Worker) Start(ctx context.Context, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
||||
case m, ok := <-w.in:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
w.out <- w.findMax(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Worker) findMax(input [10]int) [3]int {
|
||||
sort.Ints(input[:])
|
||||
|
||||
res := [3]int(input[7:])
|
||||
|
||||
w.log.Debug("new data", zap.Ints("input", input[:]), zap.Ints("output", res[:]))
|
||||
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user