Skip to content

Instantly share code, notes, and snippets.

@logrusorgru
Last active August 13, 2017 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logrusorgru/23fc49f1091d9c98832bb231b4c72a6e to your computer and use it in GitHub Desktop.
Save logrusorgru/23fc49f1091d9c98832bb231b4c72a6e to your computer and use it in GitHub Desktop.
CXO update Root
// in this example the Event is an event, that requires updates in our Root
func startHandlingSomePack(events <-chan Event, pack *skyobject.Pack,
n *node.Node, wg *sync.WaitGroup) {
go handleSomePack(n, pack, events, n.Quiting(), wg)
}
func handleSomePack(n *node.Node, pack *cxo.Pack, events <-chan Event,
quit <-chan struct{}, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case evt := <-events:
// perform changes using the evt (Event)
performCahnges(pack, evt)
perfomAllChangesPossible(quit, events, pack, n)
case <-quit:
return
}
}
}
func performAllCahgnesPossible(quit <-chan struct{}, events <-chan Event,
pack *skyobject.Pack, n *node.Node) {
tm := time.NewTimer(MinPublishInterval)
defer tm.Stop()
// perform all changes possible and publish
for {
select {
case <-quit:
return
case evt := <-events:
performCahnges(pack, evt)
case <-tm.C:
// to many changes performed but not published yet
publish(n, pack)
tm.Reset(MinPubishInterval) // start the timer again
default:
// no more events, no more changes
publish(n, pack)
return
}
}
}
func publish(n *node.Node, pack *skyobject.Pack) {
if _, err := pack.Save(); err != nil {
// fatality: handle the err
}
n.Publish(pack.Root())
}
func performCahgnes(pack *skyobject.Pack, evt Event) {
// stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment