move data fetching to goroutines

This commit is contained in:
Gabriel Augendre 2024-09-16 14:20:59 +02:00
parent dd1a7f274c
commit af1a38efca

100
img.go
View file

@ -17,6 +17,7 @@ import (
"math" "math"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
) )
@ -35,26 +36,83 @@ func getBlack(
feteClient *fete.Client, feteClient *fete.Client,
weatherClient *weather.Client, weatherClient *weather.Client,
) (*image.RGBA, error) { ) (*image.RGBA, error) {
bus, err := transportsClient.GetTCLPassages(ctx, 290) var (
if err != nil { bus *transports.Passages
log.Println("error getting bus:", err) tram *transports.Passages
} velovRoc *transports.Station
tram, err := transportsClient.GetTCLPassages(ctx, 34068) fetes *fete.Fete
if err != nil { wthr *weather.Prevision
log.Println("error getting tram:", err) )
}
velovRoc, err := transportsClient.GetVelovStation(ctx, 10044) wg := &sync.WaitGroup{}
if err != nil { wg.Add(5)
log.Println("error getting velov:", err)
} go func() {
fetes, err := feteClient.GetFete(ctx, nowFunc()) defer wg.Done()
if err != nil {
log.Println("error getting fetes:", err) ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
} defer cancel()
wthr, err := weatherClient.GetWeather(ctx)
if err != nil { var err error
log.Println("error getting weather:", err)
} bus, err = transportsClient.GetTCLPassages(ctx, 290)
if err != nil {
log.Println("error getting bus:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
tram, err = transportsClient.GetTCLPassages(ctx, 34068)
if err != nil {
log.Println("error getting tram:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
velovRoc, err = transportsClient.GetVelovStation(ctx, 10044)
if err != nil {
log.Println("error getting velov:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
fetes, err = feteClient.GetFete(ctx, nowFunc())
if err != nil {
log.Println("error getting fetes:", err)
}
}()
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
wthr, err = weatherClient.GetWeather(ctx)
if err != nil {
log.Println("error getting weather:", err)
}
}()
quote := quotes.GetQuote(nowFunc()) quote := quotes.GetQuote(nowFunc())
img := newWhite() img := newWhite()
@ -65,6 +123,8 @@ func getBlack(
gc.SetFillColor(color.RGBA{255, 255, 255, 255}) gc.SetFillColor(color.RGBA{255, 255, 255, 255})
gc.SetStrokeColor(color.RGBA{0, 0, 0, 255}) gc.SetStrokeColor(color.RGBA{0, 0, 0, 255})
wg.Wait()
drawTCL(gc, bus, 55) drawTCL(gc, bus, 55)
drawTCL(gc, tram, 190) drawTCL(gc, tram, 190)
drawVelov(gc, velovRoc, 350) drawVelov(gc, velovRoc, 350)