-
Notifications
You must be signed in to change notification settings - Fork 104
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
feat: add leadership package #661
base: main
Are you sure you want to change the base?
Changes from 2 commits
96c9f3e
8ed6d5d
0da77e0
4b19017
f39ef3f
935699b
294efc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
//go:generate gomarkdoc -o README.md --repository.default-branch main | ||
//go:generate gomarkdoc -o README.md --repository.default-branch main --repository.url https://github.com/formancehq/ledger | ||
package ledger |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,86 @@ | ||||||||||||||||||||||||
package leadership | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"sync" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type listener struct { | ||||||||||||||||||||||||
channel chan Leadership | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type Broadcaster struct { | ||||||||||||||||||||||||
mu *sync.Mutex | ||||||||||||||||||||||||
t *Leadership | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
inner []listener | ||||||||||||||||||||||||
outer chan Leadership | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (h *Broadcaster) Actual() Leadership { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if h.t == nil { | ||||||||||||||||||||||||
return Leadership{} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return *h.t | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (h *Broadcaster) Subscribe() (<-chan Leadership, func()) { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
newChannel := make(chan Leadership, 1) | ||||||||||||||||||||||||
index := len(h.inner) | ||||||||||||||||||||||||
h.inner = append(h.inner, listener{ | ||||||||||||||||||||||||
channel: newChannel, | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
if h.t != nil { | ||||||||||||||||||||||||
newChannel <- *h.t | ||||||||||||||||||||||||
paul-nicolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return newChannel, func() { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if index < len(h.inner)-1 { | ||||||||||||||||||||||||
h.inner = append(h.inner[:index], h.inner[index+1:]...) | ||||||||||||||||||||||||
gfyrag marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
h.inner = h.inner[:index] | ||||||||||||||||||||||||
gfyrag marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (h *Broadcaster) Broadcast(t Leadership) { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
h.t = &t | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
for _, inner := range h.inner { | ||||||||||||||||||||||||
inner.channel <- t | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (h *Broadcaster) Close() { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
for _, inner := range h.inner { | ||||||||||||||||||||||||
close(inner.channel) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func (h *Broadcaster) CountListeners() int { | ||||||||||||||||||||||||
h.mu.Lock() | ||||||||||||||||||||||||
defer h.mu.Unlock() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
return len(h.inner) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func NewSignal() *Broadcaster { | ||||||||||||||||||||||||
return &Broadcaster{ | ||||||||||||||||||||||||
outer: make(chan Leadership), | ||||||||||||||||||||||||
mu: &sync.Mutex{}, | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve constructor naming and initialization. The
Apply this diff to improve the constructor: -func NewSignal() *Broadcaster {
+func NewBroadcaster() *Broadcaster {
return &Broadcaster{
- outer: make(chan Leadership),
- mu: &sync.Mutex{},
+ mu: sync.Mutex{},
}
} 📝 Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use mutex by value and remove unused channel.
The
Broadcaster
struct has two issues:outer
channel is declared but never used in the implementation.Apply this diff to fix these issues:
📝 Committable suggestion