move data fetching to goroutines

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

70
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 (
bus *transports.Passages
tram *transports.Passages
velovRoc *transports.Station
fetes *fete.Fete
wthr *weather.Prevision
)
wg := &sync.WaitGroup{}
wg.Add(5)
go func() {
defer wg.Done()
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
var err error
bus, err = transportsClient.GetTCLPassages(ctx, 290)
if err != nil { if err != nil {
log.Println("error getting bus:", err) log.Println("error getting bus:", err)
} }
tram, err := transportsClient.GetTCLPassages(ctx, 34068) }()
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 { if err != nil {
log.Println("error getting tram:", err) log.Println("error getting tram:", err)
} }
velovRoc, err := transportsClient.GetVelovStation(ctx, 10044) }()
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 { if err != nil {
log.Println("error getting velov:", err) log.Println("error getting velov:", err)
} }
fetes, err := feteClient.GetFete(ctx, nowFunc()) }()
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 { if err != nil {
log.Println("error getting fetes:", err) log.Println("error getting fetes:", err)
} }
wthr, err := weatherClient.GetWeather(ctx) }()
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 { if err != nil {
log.Println("error getting weather:", err) 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)