Skip to content

Commit

Permalink
[runner] Add wait group for reporter #43
Browse files Browse the repository at this point in the history
- was getting negative duration due to finialize is called before Run
is finished, another way to solve this is to add a lock inside reporter, so
calling Finalize won't work until Run exits ...
  • Loading branch information
at15 committed Mar 19, 2018
1 parent 5d704da commit f123c58
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/reporter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package reporter

import (
"context"
"encoding/json"
"fmt"
"time"

"encoding/json"
dlog "github.com/dyweb/gommon/log"
"github.com/xephonhq/xephon-b/pkg/config"
"github.com/xephonhq/xephon-b/pkg/metrics"
Expand Down
11 changes: 6 additions & 5 deletions pkg/runner/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package runner
import (
"context"
"fmt"
"sync"

"github.com/dyweb/gommon/errors"
dlog "github.com/dyweb/gommon/log"
"github.com/xephonhq/xephon-b/pkg/config"
"github.com/xephonhq/xephon-b/pkg/metrics"
"github.com/xephonhq/xephon-b/pkg/reporter"
"sync"
)

type Manager struct {
Expand Down Expand Up @@ -66,9 +65,12 @@ func (m *Manager) Run(ctx context.Context) error {
return err
}
// https://github.com/xephonhq/xephon-b/issues/43
var rwg sync.WaitGroup
repCtx, repCancel := context.WithCancel(context.Background())
rwg.Add(1)
go func() {
rep.Run(repCtx, resChan)
rwg.Done()
}()
// worker
if cfg.Worker.Num <= 0 {
Expand All @@ -93,13 +95,12 @@ func (m *Manager) Run(ctx context.Context) error {
wg.Done()
}(workers[i])
}
m.log.Info("all worker started")
wg.Wait()
m.log.Info("all worker finished")
// TODO: which one should be done first? https://github.com/xephonhq/xephon-b/issues/43
//m.log.Info("close resChan")
//close(resChan)
m.log.Info("canceling reporter context")
repCancel()
rwg.Wait()
if err := rep.Finalize(); err != nil {
return errors.Wrap(err, "can't finalize reporter")
}
Expand Down

0 comments on commit f123c58

Please sign in to comment.