Esempio n. 1
0
func (s *ChatServer) Run() {
	ticks := time.Tick(time.Second * 1)
	go muduo.ServeTcp(s.listener, s, "chat")
	for {
		select {
		case c := <-s.register:
			s.conns[c] = true
		case c := <-s.unregister:
			delete(s.conns, c)
			close(c.send)
		case m := <-s.boardcast:
			for c := range s.conns {
				select {
				case c.send <- m:
				default:
					delete(s.conns, c)
					close(c.send)
					log.Println("kick slow connection")
				}
			}
		case _ = <-ticks:
			log.Println(len(s.conns), runtime.NumGoroutine())
		}
	}
}
Esempio n. 2
0
func main() {
	flag.Parse()
	runtime.GOMAXPROCS(4)

	ticker := time.NewTicker(time.Second * 5)
	defer ticker.Stop()
	go func() {
		for _ = range ticker.C {
			fmt.Println("# goroutine", runtime.NumGoroutine())
		}
	}()

	log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
	s := NewBalancer(opt.port, opt.backends)

	go s.dispatch()
	muduo.ServeTcp(s.listener, s, "balancer")
}