Skip to content

Adds events/batch/singular #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions events/batcher/singuler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package batcher

import (
"sync"
"sync/atomic"
"time"
)

// Singular is a Batcher which batches events and treats them as all the same
// key, behaving like a singular batched queue.
type Singular struct {
b *Batcher[int]
closeCh chan struct{}
closed atomic.Bool
wg sync.WaitGroup
}

func NewSingular(interval time.Duration) *Singular {
return &Singular{
b: New[int](interval),
closeCh: make(chan struct{}),
}
}

func (s *Singular) Subscribe(fn func()) {
ch := make(chan struct{})
s.b.Subscribe(ch)

s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
select {
case <-ch:
case <-s.closeCh:
return
}

fn()
}
}()
}

func (s *Singular) Batch() {
s.b.Batch(0)
}

func (s *Singular) Close() {
defer s.wg.Wait()
if s.closed.CompareAndSwap(false, true) {
close(s.closeCh)
}
s.b.Close()
}
95 changes: 95 additions & 0 deletions events/batcher/singuler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package batcher

import (
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
testingclock "k8s.io/utils/clock/testing"
)

func TestSingular(t *testing.T) {
t.Parallel()

fakeClock := testingclock.NewFakeClock(time.Now())
s := NewSingular(time.Millisecond * 10)
s.b.WithClock(fakeClock)

fn := func(i *atomic.Int64) func() {
return func() {
i.Add(1)
}
}

var f1, f2, f3 atomic.Int64
s.Subscribe(fn(&f1))
s.Subscribe(fn(&f2))
s.Subscribe(fn(&f3))

s.Batch()
s.Batch()
s.Batch()
s.Batch()
s.Batch()
s.Batch()
s.Batch()
s.Batch()

assert.Eventually(t, fakeClock.HasWaiters, time.Second*5, time.Millisecond*100)

assert.Equal(t, int64(0), f1.Load())
assert.Equal(t, int64(0), f2.Load())
assert.Equal(t, int64(0), f3.Load())

fakeClock.Step(time.Millisecond * 5)

assert.Equal(t, int64(0), f1.Load())
assert.Equal(t, int64(0), f2.Load())
assert.Equal(t, int64(0), f3.Load())

fakeClock.Step(time.Millisecond * 5)

assert.Eventually(t, func() bool {
return f1.Load() == 1 && f2.Load() == 1 && f3.Load() == 1
}, time.Second*5, time.Millisecond*100)

s.Batch()
s.Batch()
s.Batch()

fakeClock.Step(time.Millisecond * 15)

assert.Eventually(t, func() bool {
return f1.Load() == 2 && f2.Load() == 2 && f3.Load() == 2
}, time.Second*5, time.Millisecond*100)

s.Close()

s.Batch()
s.Batch()
s.Batch()

fakeClock.Step(time.Millisecond * 10)

assert.Eventually(t, func() bool {
return !fakeClock.HasWaiters()
}, time.Second*5, time.Millisecond*100)

assert.Equal(t, int64(2), f1.Load())
assert.Equal(t, int64(2), f2.Load())
assert.Equal(t, int64(2), f3.Load())
}