talks

git clone git://code.dwrz.net/talks
Log | Files | Refs

csv.go (587B)


      1 package main
      2 
      3 import (
      4 	"encoding/csv"
      5 	"io"
      6 	"os"
      7 	"sync"
      8 	"time"
      9 )
     10 
     11 // The free tier of the OpenWeatherMap API allows for 60 requests per minute.
     12 // ∴ we limit our requests to 1 per second.
     13 var interval = 1 * time.Second
     14 
     15 func processCSV(
     16 	file *os.File, ztchan chan *ZipTemp,
     17 ) {
     18 	var (
     19 		limiter = time.Tick(interval)
     20 		wg      sync.WaitGroup
     21 	)
     22 
     23 	reader := csv.NewReader(file)
     24 	for {
     25 		record, err := reader.Read()
     26 		if err == io.EOF {
     27 			break
     28 		}
     29 		if err != nil {
     30 			panic(err)
     31 		}
     32 
     33 		<-limiter
     34 
     35 		wg.Add(1)
     36 		go getZipTemp(&wg, record, ztchan)
     37 	}
     38 
     39 	wg.Wait()
     40 	close(ztchan)
     41 }