ytui/pubsub/pt.go

144 lines
2.8 KiB
Go
Raw Normal View History

2022-06-26 19:30:26 -04:00
package pubsub
import (
"fmt"
"sync"
2022-06-26 23:13:57 -04:00
"time"
2022-06-26 19:30:26 -04:00
)
type Step int
const (
NotStarted Step = iota
Publishing
Done
)
2022-06-26 22:34:53 -04:00
type subscriberState struct {
step Step
m sync.Mutex
subChans []chan<- string
2022-06-26 19:30:26 -04:00
}
type InvalidState struct{ Step Step }
func (i InvalidState) Error() string {
return fmt.Sprintf("Invalid state: %v", i.Step)
}
// One thread publishes progress, one or more threads subscribes to watch the progress
// Subscribers may not get all updates. They will get the latest status when waiting on the channel
type ProgressTracker interface {
// Only one publisher sends update. Should close when done
// Error if there is/was existing publisher
Publish(id string) (chan<- string, error)
// Can subscribe even if there no publisher yet
// If already done, nil channel is returned
// channel will be closed when done
Subscribe(id string) <-chan string
}
type progressTracker struct {
2022-06-26 22:34:53 -04:00
subscribers map[string]*subscriberState
2022-06-26 19:30:26 -04:00
m sync.Mutex
}
func NewProgressTracker() ProgressTracker {
return &progressTracker{
2022-06-26 22:34:53 -04:00
subscribers: map[string]*subscriberState{},
2022-06-26 19:30:26 -04:00
}
}
func (pt *progressTracker) Publish(id string) (chan<- string, error) {
2022-06-26 22:34:53 -04:00
var state *subscriberState
2022-06-26 19:30:26 -04:00
func() {
pt.m.Lock()
defer pt.m.Unlock()
2022-06-26 22:34:53 -04:00
state = pt.subscribers[id]
if state == nil {
state = &subscriberState{step: NotStarted}
pt.subscribers[id] = state
2022-06-26 19:30:26 -04:00
}
}()
err := func() error {
2022-06-26 22:34:53 -04:00
state.m.Lock()
defer state.m.Unlock()
if state.step != NotStarted {
return InvalidState{state.step}
2022-06-26 19:30:26 -04:00
}
2022-06-26 22:34:53 -04:00
state.step = Publishing
2022-06-26 19:30:26 -04:00
return nil
}()
if err != nil {
return nil, err
}
2022-06-26 22:34:53 -04:00
prodChan := make(chan string, 100)
2022-06-26 19:30:26 -04:00
go func() {
2022-06-26 22:34:53 -04:00
var update string
prodChanOpen := true
2022-06-26 23:13:57 -04:00
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
done := false
for !done && prodChanOpen {
2022-06-26 19:30:26 -04:00
select {
2022-06-26 22:34:53 -04:00
case update, prodChanOpen = <-prodChan:
2022-06-26 19:30:26 -04:00
default:
2022-06-26 23:13:57 -04:00
done = true
2022-06-26 19:30:26 -04:00
}
}
var scs []chan<- string
func() {
2022-06-26 22:34:53 -04:00
state.m.Lock()
defer state.m.Unlock()
scs = state.subChans
if !prodChanOpen {
for _, subChan := range scs {
close(subChan)
2022-06-26 19:30:26 -04:00
}
2022-06-26 22:34:53 -04:00
state.step = Done
2022-06-26 19:30:26 -04:00
}
}()
2022-06-26 22:34:53 -04:00
if !prodChanOpen {
2022-06-26 19:30:26 -04:00
return
}
2022-06-26 22:34:53 -04:00
for _, subChan := range scs {
2022-06-26 19:30:26 -04:00
select {
2022-06-26 22:34:53 -04:00
case subChan <- update:
2022-06-26 19:30:26 -04:00
default:
}
}
}
}()
2022-06-26 22:34:53 -04:00
return prodChan, nil
2022-06-26 19:30:26 -04:00
}
func (pt *progressTracker) Subscribe(id string) <-chan string {
2022-06-26 22:34:53 -04:00
c := make(chan string, 1)
var state *subscriberState
func() {
pt.m.Lock()
defer pt.m.Unlock()
state = pt.subscribers[id]
if state == nil {
pt.subscribers[id] = &subscriberState{
step: NotStarted,
subChans: []chan<- string{c},
}
2022-06-26 19:30:26 -04:00
}
2022-06-26 22:34:53 -04:00
}()
if state == nil {
2022-06-26 19:30:26 -04:00
return c
}
2022-06-26 22:34:53 -04:00
state.m.Lock()
defer state.m.Unlock()
if state.step == Done {
2022-06-26 19:30:26 -04:00
return nil
}
2022-06-26 22:34:53 -04:00
state.subChans = append(state.subChans, c)
2022-06-26 19:30:26 -04:00
return c
}