Skip to content

Commit f39ef3f

Browse files
committed
fix: from review
1 parent 4b19017 commit f39ef3f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

internal/leadership/broadcaster.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ func (h *Broadcaster) Subscribe() (<-chan Leadership, func()) {
3131
defer h.mu.Unlock()
3232

3333
newChannel := make(chan Leadership, 1)
34-
index := len(h.inner)
35-
h.inner = append(h.inner, listener{
34+
l := listener{
3635
channel: newChannel,
37-
})
36+
}
37+
h.inner = append(h.inner, l)
3838
if h.t != nil {
3939
newChannel <- *h.t
4040
}
@@ -43,6 +43,14 @@ func (h *Broadcaster) Subscribe() (<-chan Leadership, func()) {
4343
h.mu.Lock()
4444
defer h.mu.Unlock()
4545

46+
index := -1
47+
for i, listener := range h.inner {
48+
if listener == l {
49+
index = i
50+
break
51+
}
52+
}
53+
4654
if index < len(h.inner)-1 {
4755
h.inner = append(h.inner[:index], h.inner[index+1:]...)
4856
} else {

internal/leadership/mutex.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leadership
2+
3+
import "sync"
4+
5+
type Mutex[T any] struct {
6+
*sync.Mutex
7+
t T
8+
}
9+
10+
func (m *Mutex[T]) Lock() (T, func()) {
11+
m.Mutex.Lock()
12+
return m.t, m.Unlock
13+
}
14+
15+
func NewMutex[T any](t T) *Mutex[T] {
16+
return &Mutex[T]{
17+
Mutex: &sync.Mutex{},
18+
t: t,
19+
}
20+
}

0 commit comments

Comments
 (0)