fix(bloomberg): avoid collection timeouts
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@ -50,27 +51,30 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
|
||||
payload, _ := json.Marshal(scraperRequest{Username: username, Password: password})
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, b.scraperURL+"/bloomberg/scrape", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.New("bloomberg: request construction failed")
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := b.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scraper service unreachable: %w", err)
|
||||
return nil, scraperFailure(ctx, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("scraper service HTTP %d: %s", resp.StatusCode, body)
|
||||
return nil, fmt.Errorf("bloomberg: scraper HTTP %d", resp.StatusCode)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, scraperFailure(ctx, err)
|
||||
}
|
||||
|
||||
var result scraperResponse
|
||||
if err := json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("parse scraper response: %w", err)
|
||||
return nil, errors.New("bloomberg: invalid response")
|
||||
}
|
||||
if result.Error != "" {
|
||||
return nil, fmt.Errorf("bloomberg: %s", result.Error)
|
||||
return nil, errors.New("bloomberg: scraper failure")
|
||||
}
|
||||
|
||||
var articles []scraper.Article
|
||||
@ -90,7 +94,6 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
|
||||
syms := scraper.DetectSymbols(title, symbols)
|
||||
articles = append(articles, scraper.Article{
|
||||
Title: title,
|
||||
Content: title,
|
||||
URL: url,
|
||||
PublishedAt: date,
|
||||
Symbols: syms,
|
||||
@ -99,3 +102,18 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
|
||||
fmt.Printf("bloomberg: %d articles fetched\n", len(articles))
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
// Preserve only typed timeout/cancellation identity, never the raw transport error.
|
||||
func scraperFailure(ctx context.Context, err error) error {
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled) {
|
||||
return fmt.Errorf("bloomberg: canceled: %w", context.Canceled)
|
||||
}
|
||||
var timeout interface{ Timeout() bool }
|
||||
if errors.As(err, &timeout) && timeout.Timeout() {
|
||||
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
|
||||
}
|
||||
return errors.New("bloomberg: transport failure")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user