dont diez plz

This commit is contained in:
pigwin
2025-12-23 14:40:53 +00:00
parent c106267d76
commit 9b433cdd57

View File

@@ -3,6 +3,7 @@ package data
import ( import (
"crypto/tls" "crypto/tls"
"encoding/xml" "encoding/xml"
"fmt"
"log" "log"
"net/http" "net/http"
"time" "time"
@@ -134,16 +135,19 @@ func FetchData(timestamp string) (*Data, error) {
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12, MinVersion: tls.VersionTLS12,
}, },
MaxIdleConns: 10, MaxIdleConns: 10,
IdleConnTimeout: 90 * time.Second, MaxIdleConnsPerHost: 10,
DisableCompression: false, IdleConnTimeout: 90 * time.Second,
ForceAttemptHTTP2: false, // Disable HTTP/2 to avoid stream errors DisableCompression: false,
TLSHandshakeTimeout: 10 * time.Second, ForceAttemptHTTP2: false, // Disable HTTP/2 to avoid stream errors
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
} }
client := &http.Client{ client := &http.Client{
Transport: transport, Transport: transport,
Timeout: 120 * time.Second, // 2 minute timeout for large datasets Timeout: 180 * time.Second, // 3 minute timeout for large datasets
} }
requestorId := "ti1-" + timestamp requestorId := "ti1-" + timestamp
@@ -152,33 +156,60 @@ func FetchData(timestamp string) (*Data, error) {
// Retry logic for transient failures // Retry logic for transient failures
var resp *http.Response var resp *http.Response
var err error var err error
var data *Data
maxRetries := 3 maxRetries := 3
for attempt := 1; attempt <= maxRetries; attempt++ { for attempt := 1; attempt <= maxRetries; attempt++ {
log.Printf("Fetching data from URL (attempt %d/%d): %s", attempt, maxRetries, url) log.Printf("Fetching data from URL (attempt %d/%d): %s", attempt, maxRetries, url)
resp, err = client.Get(url) resp, err = client.Get(url)
if err == nil { if err != nil {
break log.Printf("Request failed: %v", err)
if attempt < maxRetries {
waitTime := time.Duration(attempt*2) * time.Second
log.Printf("Retrying in %v...", waitTime)
time.Sleep(waitTime)
}
continue
} }
log.Printf("Request failed: %v", err)
if attempt < maxRetries { // Check HTTP status code
waitTime := time.Duration(attempt*2) * time.Second if resp.StatusCode != http.StatusOK {
log.Printf("Retrying in %v...", waitTime) resp.Body.Close()
time.Sleep(waitTime) err = fmt.Errorf("HTTP error: %s (status code: %d)", resp.Status, resp.StatusCode)
log.Printf("%v", err)
if attempt < maxRetries {
waitTime := time.Duration(attempt*2) * time.Second
log.Printf("Retrying in %v...", waitTime)
time.Sleep(waitTime)
}
continue
} }
// Try to decode the response
data = &Data{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(data)
resp.Body.Close()
if err != nil {
log.Printf("Failed to decode XML: %v", err)
if attempt < maxRetries {
waitTime := time.Duration(attempt*2) * time.Second
log.Printf("Retrying in %v...", waitTime)
time.Sleep(waitTime)
}
continue
}
// Success!
log.Printf("Successfully fetched and decoded data")
return data, nil
} }
// All retries failed
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close() return nil, fmt.Errorf("Failed to fetch data after %d attempts", maxRetries)
data := &Data{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(data)
if err != nil {
return nil, err
}
return data, nil
} }