示例#1
0
文件: main.go 项目: warmfusion/cache
func serve(l net.Listener) {
	var wg sync.WaitGroup
	ch := make(chan *filter.MatchedMetric, 10)
	wg.Add(1)
	go func() {
		defer wg.Done()
		cache.Save(ch, func(buffer []*filter.MatchedMetric) {
			if err := cache.SavePoints(buffer, db); err != nil {
				log.Printf("failed to save value in cache: %s", err)
			}
		})
	}()
	go func() {
		for {
			filter.UpdateProcessingMetrics()
			time.Sleep(time.Second)
		}
	}()
	for {
		conn, err := l.Accept()
		if err != nil {
			if goagain.IsErrClosing(err) {
				break
			}
			log.Printf("failed to accept connection: %s", err.Error())
			continue
		}

		go handleConnection(conn, ch)
	}
	close(ch)
	wg.Wait()
}
示例#2
0
// A very rude server that says hello and then closes your connection.
func serve(l net.Listener, ch chan struct{}, wg *sync.WaitGroup) {
	defer wg.Done()
	for {

		// Break out of the accept loop on the next iteration after the
		// process was signaled and our channel was closed.
		select {
		case <-ch:
			return
		default:
		}

		// Set a deadline so Accept doesn't block forever, which gives
		// us an opportunity to stop gracefully.
		l.(*net.TCPListener).SetDeadline(time.Now().Add(100e6))

		c, err := l.Accept()
		if nil != err {
			if goagain.IsErrClosing(err) {
				return
			}
			if err.(*net.OpError).Timeout() {
				continue
			}
			log.Fatalln(err)
		}
		c.Write([]byte("Hello, world!\n"))
		c.Close()
	}
}
示例#3
0
func serve(l net.Listener) {
	for {
		c, err := l.Accept()
		if nil != err {
			if goagain.IsErrClosing(err) {
				break
			}
			log.Fatalln(err)
		}
		c.Write([]byte("Hello, world!\n"))
		c.Close()
	}
}