add backoff
This commit is contained in:
parent
802dfc97a2
commit
6f4090fcb3
@ -4,4 +4,7 @@ go 1.25.2
|
|||||||
|
|
||||||
replace pkg => ../pkg
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/cenkalti/backoff/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inMemory struct {
|
type inMemory struct {
|
||||||
@ -29,19 +32,31 @@ func (s *Service) GetWeatherByCity(ctx context.Context, params GetMeteoData) ([]
|
|||||||
}
|
}
|
||||||
toDate := fromDate.AddDate(0, 0, params.Days-1)
|
toDate := fromDate.AddDate(0, 0, params.Days-1)
|
||||||
|
|
||||||
url := fmt.Sprintf("http://localhost:8080/data?city=%s&from=%s&to=%s",
|
operation := func() (*http.Response, error) {
|
||||||
params.Location, params.Date, toDate.Format("2006-01-02"))
|
url := fmt.Sprintf("http://localhost:8080/data?city=%s&from=%s&to=%s",
|
||||||
|
params.Location, params.Date, toDate.Format("2006-01-02"))
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusBadRequest {
|
||||||
|
return nil, backoff.Permanent(errors.New("bad request"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// TODO add backoff
|
result, err := backoff.Retry(ctx, operation, backoff.WithBackOff(backoff.NewExponentialBackOff()))
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("somethin happened")
|
||||||
|
return []MeteoData{}, err
|
||||||
|
}
|
||||||
// TODO add ristretto
|
// TODO add ristretto
|
||||||
|
|
||||||
slog.Info("fetched data", "data", resp)
|
slog.Info("fetched data", "data", result)
|
||||||
|
|
||||||
return []MeteoData{}, nil
|
return []MeteoData{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user