Exemple #1
0
func DispatchForever(subs <-chan net.Conn, pub <-chan []byte) {
	sublist := make([]net.Conn, 0)
	for {
		select {
		case sub := <-subs:
			sublist = append(sublist, sub)
		case data := <-pub:
			retch := make(chan net.Conn)
			subcount := len(sublist)
			for _, sub := range sublist {
				go func(sub net.Conn) {
					sub.SetDeadline(time.Now().Add(Timeout))
					_, err := sub.Write(data)
					if err != nil {
						log.Debug("streamz timeout writing to", sub.RemoteAddr().String(), ":", err)
						retch <- nil
					} else {
						retch <- sub
					}
				}(sub)
			}

			sublist = sublist[:0]
			for i := 0; i < subcount; i++ {
				sub := <-retch
				if sub != nil {
					sublist = append(sublist, <-retch)
				}
			}
		}
	}
}
Exemple #2
0
func Ticker(pub chan []byte) {
	t := time.Tick(2 * time.Second)
	var tocker bool = false
	for {
		<-t
		log.Debug("tick")
		if tocker {
			Write(pub, "tock", "tick")
		} else {
			Write(pub, "tick", "tock")
		}
		tocker = !tocker
	}
}