dont send duplicate updates
This commit is contained in:
parent
002844cd37
commit
1fab1e0095
31
pubsub/pt.go
31
pubsub/pt.go
@ -15,10 +15,15 @@ const (
|
||||
Done
|
||||
)
|
||||
|
||||
type subChan struct {
|
||||
c chan<- string
|
||||
lastUpdateIndex int
|
||||
}
|
||||
|
||||
type subscriberState struct {
|
||||
step Step
|
||||
m sync.Mutex
|
||||
subChans []chan<- string
|
||||
sc []subChan
|
||||
}
|
||||
|
||||
type InvalidState struct{ Step Step }
|
||||
@ -82,23 +87,25 @@ func (pt *progressTracker) Publish(id string) (chan<- string, error) {
|
||||
prodChanOpen := true
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
lastUpdateIndex := 0
|
||||
for range ticker.C {
|
||||
done := false
|
||||
for !done && prodChanOpen {
|
||||
LoopReader:
|
||||
for prodChanOpen {
|
||||
select {
|
||||
case update, prodChanOpen = <-prodChan:
|
||||
lastUpdateIndex++
|
||||
default:
|
||||
done = true
|
||||
break LoopReader
|
||||
}
|
||||
}
|
||||
var scs []chan<- string
|
||||
var scs []subChan
|
||||
func() {
|
||||
state.m.Lock()
|
||||
defer state.m.Unlock()
|
||||
scs = state.subChans
|
||||
scs = state.sc
|
||||
if !prodChanOpen {
|
||||
for _, subChan := range scs {
|
||||
close(subChan)
|
||||
close(subChan.c)
|
||||
}
|
||||
state.step = Done
|
||||
}
|
||||
@ -107,18 +114,22 @@ func (pt *progressTracker) Publish(id string) (chan<- string, error) {
|
||||
return
|
||||
}
|
||||
for _, subChan := range scs {
|
||||
if subChan.lastUpdateIndex != lastUpdateIndex {
|
||||
select {
|
||||
case subChan <- update:
|
||||
case subChan.c <- update:
|
||||
subChan.lastUpdateIndex = lastUpdateIndex
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
return prodChan, nil
|
||||
}
|
||||
|
||||
func (pt *progressTracker) Subscribe(id string) <-chan string {
|
||||
c := make(chan string, 1)
|
||||
sc := subChan{c: c}
|
||||
var state *subscriberState
|
||||
func() {
|
||||
pt.m.Lock()
|
||||
@ -127,7 +138,7 @@ func (pt *progressTracker) Subscribe(id string) <-chan string {
|
||||
if state == nil {
|
||||
pt.subscribers[id] = &subscriberState{
|
||||
step: NotStarted,
|
||||
subChans: []chan<- string{c},
|
||||
sc: []subChan{sc},
|
||||
}
|
||||
}
|
||||
}()
|
||||
@ -139,6 +150,6 @@ func (pt *progressTracker) Subscribe(id string) <-chan string {
|
||||
if state.step == Done {
|
||||
return nil
|
||||
}
|
||||
state.subChans = append(state.subChans, c)
|
||||
state.sc = append(state.sc, sc)
|
||||
return c
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user