Skip to content

Commit

Permalink
core/deadline: cleanup deadline.go (#811)
Browse files Browse the repository at this point in the history
Addresses comments on earlier PR: #805 

category: feature
ticket: #768
  • Loading branch information
dB2510 authored Jul 19, 2022
1 parent e8e092f commit 588181d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
16 changes: 7 additions & 9 deletions core/deadline.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,21 @@ type Deadline struct {
dutyChan chan Duty
deadlineChan chan Duty
quit chan struct{}
deadlineFunc func(Duty) time.Time
}

// NewDeadliner returns a new instance of Deadline.
// It runs a goroutine which is responsible for reading and storing duties,
// and sending the deadlined duty to receiver's deadlineChan.
func NewDeadliner(ctx context.Context, deadlineFunc func(Duty) time.Time) (*Deadline, error) {
func NewDeadliner(ctx context.Context, deadlineFunc func(Duty) time.Time) *Deadline {
d := &Deadline{
dutyChan: make(chan Duty),
deadlineChan: make(chan Duty),
quit: make(chan struct{}),
deadlineFunc: deadlineFunc,
}

go func() {
duties := make(map[Duty]bool)
currDuty, currDeadline := getCurrDuty(duties, d.deadlineFunc)
currDuty, currDeadline := getCurrDuty(duties, deadlineFunc)
currTimer := time.NewTimer(time.Until(currDeadline))

defer func() {
Expand All @@ -77,13 +75,13 @@ func NewDeadliner(ctx context.Context, deadlineFunc func(Duty) time.Time) (*Dead
}()

setCurrState := func() {
if !currDeadline.IsZero() {
currTimer.Stop()
}
currDuty, currDeadline = getCurrDuty(duties, d.deadlineFunc)
currTimer.Stop()
currDuty, currDeadline = getCurrDuty(duties, deadlineFunc)
currTimer = time.NewTimer(time.Until(currDeadline))
}

// TODO(dhruv): optimise getCurrDuty and updating current state if earlier deadline detected,
// using min heap or ordered map
for {
select {
case <-ctx.Done():
Expand All @@ -100,7 +98,7 @@ func NewDeadliner(ctx context.Context, deadlineFunc func(Duty) time.Time) (*Dead
}
}()

return d, nil
return d
}

// Add adds a duty to be notified of the deadline.
Expand Down
3 changes: 1 addition & 2 deletions core/deadline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func TestDeadliner(t *testing.T) {
return end
}

deadliner, err := core.NewDeadliner(ctx, deadlineFunc)
require.NoError(t, err)
deadliner := core.NewDeadliner(ctx, deadlineFunc)

expectedDuties := []core.Duty{
core.NewVoluntaryExit(2),
Expand Down

0 comments on commit 588181d

Please sign in to comment.