Skip to content

Commit

Permalink
Refactor MinitorMaxFloat to use chan instead of lock
Browse files Browse the repository at this point in the history
  • Loading branch information
leszko committed Jan 27, 2022
1 parent cb6d94b commit 8883222
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions server/redeemer.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ func (r *Redeemer) MaxFloat(ctx context.Context, req *net.MaxFloatReq) (*net.Max
func (r *Redeemer) MonitorMaxFloat(req *net.MaxFloatReq, stream net.TicketRedeemer_MonitorMaxFloatServer) error {
sender := ethcommon.BytesToAddress(req.Sender)

ctx, cancel := context.WithCancel(stream.Context())
// first error noticed is returned
var errRes error
var errResMu sync.Mutex
errCh := make(chan error, 1)

sink := make(chan struct{}, 10)
sub := r.sm.SubscribeMaxFloatChange(sender, sink)
Expand All @@ -133,10 +130,9 @@ func (r *Redeemer) MonitorMaxFloat(req *net.MaxFloatReq, stream net.TicketRedeem
select {
case <-r.quit:
return nil
case <-ctx.Done():
errResMu.Lock()
err := errRes
errResMu.Unlock()
case <-stream.Context().Done():
return nil
case err := <-errCh:
return err
case err := <-sub.Err():
if err == nil {
Expand All @@ -151,13 +147,10 @@ func (r *Redeemer) MonitorMaxFloat(req *net.MaxFloatReq, stream net.TicketRedeem
return
}
if err := stream.Send(&net.MaxFloatUpdate{MaxFloat: maxFloat.Bytes()}); err != nil {
if err != nil {
errResMu.Lock()
if errRes == nil {
errRes = status.Error(codes.Internal, err.Error())
}
errResMu.Unlock()
cancel()
select {
case errCh <- status.Error(codes.Internal, err.Error()):
default:
// someone already sent an error first, ignore
}
}
}()
Expand Down

0 comments on commit 8883222

Please sign in to comment.