Simplify notifier code and remove unused.

This commit is contained in:
Joachim Bauch 2026-03-10 08:50:45 +01:00
commit 9c10675867
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
4 changed files with 67 additions and 289 deletions

View file

@ -26,21 +26,34 @@ import (
"sync"
)
type rootWaiter struct {
key string
ch chan struct{}
}
func (w *rootWaiter) notify() {
close(w.ch)
}
type Waiter struct {
key string
sw *SingleWaiter
ch <-chan struct{}
}
func (w *Waiter) Wait(ctx context.Context) error {
return w.sw.Wait(ctx)
select {
case <-w.ch:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
type Notifier struct {
sync.Mutex
// +checklocks:Mutex
waiters map[string]*Waiter
waiters map[string]*rootWaiter
// +checklocks:Mutex
waiterMap map[string]map[*Waiter]bool
}
@ -50,31 +63,30 @@ func (n *Notifier) NewWaiter(key string) *Waiter {
defer n.Unlock()
waiter, found := n.waiters[key]
if found {
w := &Waiter{
if !found {
waiter = &rootWaiter{
key: key,
sw: waiter.sw,
ch: make(chan struct{}),
}
if n.waiters == nil {
n.waiters = make(map[string]*rootWaiter)
}
if n.waiterMap == nil {
n.waiterMap = make(map[string]map[*Waiter]bool)
}
n.waiters[key] = waiter
if _, found := n.waiterMap[key]; !found {
n.waiterMap[key] = make(map[*Waiter]bool)
}
n.waiterMap[key][w] = true
return w
}
waiter = &Waiter{
w := &Waiter{
key: key,
sw: newSingleWaiter(),
ch: waiter.ch,
}
if n.waiters == nil {
n.waiters = make(map[string]*Waiter)
}
if n.waiterMap == nil {
n.waiterMap = make(map[string]map[*Waiter]bool)
}
n.waiters[key] = waiter
if _, found := n.waiterMap[key]; !found {
n.waiterMap[key] = make(map[*Waiter]bool)
}
n.waiterMap[key][waiter] = true
return waiter
n.waiterMap[key][w] = true
return w
}
func (n *Notifier) Reset() {
@ -82,7 +94,7 @@ func (n *Notifier) Reset() {
defer n.Unlock()
for _, w := range n.waiters {
w.sw.cancel()
w.notify()
}
n.waiters = nil
n.waiterMap = nil
@ -96,8 +108,10 @@ func (n *Notifier) Release(w *Waiter) {
if _, found := waiters[w]; found {
delete(waiters, w)
if len(waiters) == 0 {
delete(n.waiters, w.key)
w.sw.cancel()
if root, found := n.waiters[w.key]; found {
delete(n.waiters, w.key)
root.notify()
}
}
}
}
@ -108,7 +122,7 @@ func (n *Notifier) Notify(key string) {
defer n.Unlock()
if w, found := n.waiters[key]; found {
w.sw.cancel()
w.notify()
delete(n.waiters, w.key)
delete(n.waiterMap, w.key)
}

View file

@ -39,6 +39,32 @@ func TestNotifierNoWaiter(t *testing.T) {
notifier.Notify("foo")
}
func TestNotifierWaitTimeout(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
var notifier Notifier
notified := make(chan struct{})
go func() {
defer close(notified)
time.Sleep(time.Second)
notifier.Notify("foo")
}()
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
defer cancel()
waiter := notifier.NewWaiter("foo")
defer notifier.Release(waiter)
err := waiter.Wait(ctx)
assert.ErrorIs(t, err, context.DeadlineExceeded)
<-notified
assert.NoError(t, waiter.Wait(t.Context()))
})
}
func TestNotifierSimple(t *testing.T) {
t.Parallel()

View file

@ -1,128 +0,0 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2022 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package async
import (
"context"
"sync"
)
type SingleWaiter struct {
root bool
ch chan struct{}
once sync.Once
}
func newSingleWaiter() *SingleWaiter {
return &SingleWaiter{
root: true,
ch: make(chan struct{}),
}
}
func (w *SingleWaiter) subWaiter() *SingleWaiter {
return &SingleWaiter{
ch: w.ch,
}
}
func (w *SingleWaiter) Wait(ctx context.Context) error {
select {
case <-w.ch:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (w *SingleWaiter) cancel() {
if !w.root {
return
}
w.once.Do(func() {
close(w.ch)
})
}
type SingleNotifier struct {
sync.Mutex
// +checklocks:Mutex
waiter *SingleWaiter
// +checklocks:Mutex
waiters map[*SingleWaiter]bool
}
func (n *SingleNotifier) NewWaiter() *SingleWaiter {
n.Lock()
defer n.Unlock()
if n.waiter == nil {
n.waiter = newSingleWaiter()
}
if n.waiters == nil {
n.waiters = make(map[*SingleWaiter]bool)
}
w := n.waiter.subWaiter()
n.waiters[w] = true
return w
}
func (n *SingleNotifier) Reset() {
n.Lock()
defer n.Unlock()
if n.waiter != nil {
n.waiter.cancel()
n.waiter = nil
}
n.waiters = nil
}
func (n *SingleNotifier) Release(w *SingleWaiter) {
n.Lock()
defer n.Unlock()
if _, found := n.waiters[w]; found {
delete(n.waiters, w)
if len(n.waiters) == 0 {
n.waiters = nil
if n.waiter != nil {
n.waiter.cancel()
n.waiter = nil
}
}
}
}
func (n *SingleNotifier) Notify() {
n.Lock()
defer n.Unlock()
if n.waiter != nil {
n.waiter.cancel()
}
n.waiters = nil
}

View file

@ -1,134 +0,0 @@
/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2022 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package async
import (
"context"
"sync"
"testing"
"testing/synctest"
"time"
"github.com/stretchr/testify/assert"
)
func TestSingleNotifierNoWaiter(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
// Notifications can be sent even if no waiter exists.
notifier.Notify()
}
func TestSingleNotifierSimple(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
waiter := notifier.NewWaiter()
defer notifier.Release(waiter)
var wg sync.WaitGroup
wg.Go(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
assert.NoError(t, waiter.Wait(ctx))
})
notifier.Notify()
wg.Wait()
}
func TestSingleNotifierMultiNotify(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
waiter := notifier.NewWaiter()
defer notifier.Release(waiter)
notifier.Notify()
// The second notification will be ignored while the first is still pending.
notifier.Notify()
}
func TestSingleNotifierWaitClosed(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
waiter := notifier.NewWaiter()
notifier.Release(waiter)
assert.NoError(t, waiter.Wait(context.Background()))
}
func TestSingleNotifierWaitClosedMulti(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
waiter1 := notifier.NewWaiter()
waiter2 := notifier.NewWaiter()
notifier.Release(waiter1)
notifier.Release(waiter2)
assert.NoError(t, waiter1.Wait(context.Background()))
assert.NoError(t, waiter2.Wait(context.Background()))
}
func TestSingleNotifierResetWillNotify(t *testing.T) {
t.Parallel()
var notifier SingleNotifier
waiter := notifier.NewWaiter()
defer notifier.Release(waiter)
var wg sync.WaitGroup
wg.Go(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
assert.NoError(t, waiter.Wait(ctx))
})
notifier.Reset()
wg.Wait()
}
func TestSingleNotifierDuplicate(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
var notifier SingleNotifier
var done sync.WaitGroup
for range 2 {
done.Go(func() {
waiter := notifier.NewWaiter()
defer notifier.Release(waiter)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
assert.NoError(t, waiter.Wait(ctx))
})
}
synctest.Wait()
notifier.Notify()
done.Wait()
})
}