add backoff
This commit is contained in:
parent
802dfc97a2
commit
6f4090fcb3
@ -4,4 +4,7 @@ go 1.25.2
|
||||
|
||||
replace pkg => ../pkg
|
||||
|
||||
require pkg v0.0.0-00010101000000-000000000000
|
||||
require (
|
||||
github.com/cenkalti/backoff/v5 v5.0.3
|
||||
pkg v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
2
service_b/go.sum
Normal file
2
service_b/go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
@ -2,11 +2,14 @@ package meteo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v5"
|
||||
)
|
||||
|
||||
type inMemory struct {
|
||||
@ -29,6 +32,7 @@ func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]
|
||||
}
|
||||
toDate := fromDate.AddDate(0, 0, params.Days-1)
|
||||
|
||||
operation := func() (*http.Response, error) {
|
||||
url := fmt.Sprintf("http://localhost:8080/data?city=%s&from=%s&to=%s",
|
||||
params.Location, params.Date, toDate.Format("2006-01-02"))
|
||||
|
||||
@ -38,10 +42,21 @@ func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// TODO add backoff
|
||||
if resp.StatusCode == http.StatusBadRequest {
|
||||
return nil, backoff.Permanent(errors.New("bad request"))
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
result, err := backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||
if err != nil {
|
||||
slog.Error("somethin happened")
|
||||
return []MeteoData{}, err
|
||||
}
|
||||
// TODO add ristretto
|
||||
|
||||
slog.Info("fetched data", "data", resp)
|
||||
slog.Info("fetched data", "data", result)
|
||||
|
||||
return []MeteoData{}, nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user