Skip to content

Instantly share code, notes, and snippets.

@changtimwu
Created April 3, 2015 01:30
Show Gist options
  • Save changtimwu/c55ef514761b0e56188f to your computer and use it in GitHub Desktop.
Save changtimwu/c55ef514761b0e56188f to your computer and use it in GitHub Desktop.
suture example
package main
import "fmt"
import "github.com/thejerf/suture"
import "time"
type FIB struct {
last int
current int
chans chan int
stop chan bool
}
func NewFIB() *FIB {
return &FIB{1, 1, make(chan int), make(chan bool)}
}
func (i *FIB) Get() int {
return <-i.chans
}
func (i *FIB) Stop() {
fmt.Println("Stopping the service")
i.stop <- true
}
func (i *FIB) Serve() {
fmt.Println("FIB Serve starts")
buf := make([]int, 5)
bufidx := 0
i.current = 0
for {
select {
case i.chans <- i.current:
nextval := i.last + i.current
i.last = i.current
i.current = nextval
buf[bufidx] = i.current
bufidx++
//i.current += 1
case <-i.stop:
return
}
}
}
func main() {
supervisor := suture.NewSimple("Supervisor")
fiba := NewFIB()
supervisor.Add(fiba)
fibb := NewFIB()
supervisor.Add(fibb)
go supervisor.ServeBackground()
for i := 0; i < 20; i++ {
<-time.Tick(time.Second / 2)
if (i % 4) == 0 {
fmt.Println("Got from A:", fiba.Get())
} else {
fmt.Println("Got from B:", fibb.Get())
}
}
var i int
fmt.Scan(&i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment