func main()

in go/connection-refresh/example_program.go [26:73]


func main() {
	flag.Parse()

	// Create a new bigtable.Table that will refresh the connection periodically.
	table, err := btrefresh.NewRotatingTable(func() (*bigtable.Client, error) {
		return bigtable.NewClient(context.Background(), *project, *instance)
	}, *table, tableRefreshTime)

	if err != nil {
		log.Fatal(err)
	}

	// Watch for background errors from the rotating table.
	go func() {
		for err := range table.BackgroundErrors() {
			log.Fatal(err)
		}
	}()

	latencies := make(chan time.Duration, 1)

	// Every second, print out the largest latency
	// of any one request over that second.
	go func() {
		t := time.NewTicker(1 * time.Second)
		var maxL time.Duration
		for {
			select {
			case <-t.C:
				log.Printf("Max latency over 1s: %v", maxL)
				maxL = 0
			case l := <-latencies:
				if l > maxL {
					maxL = l
				}
			}
		}
	}()

	for {
		start := time.Now()
		_, err := table.ReadRow(context.Background(), rowKey)
		latencies <- time.Since(start)
		if err != nil {
			log.Fatal(err)
		}
	}
}