Skip to content

Commit

Permalink
Configurable Stored Request Fetch Timeout (prebid#3614)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBVolcy authored Apr 30, 2024
1 parent b89fe4a commit 74fbb38
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Configuration struct {
// Note that StoredVideo refers to stored video requests, and has nothing to do with caching video creatives.
StoredVideo StoredRequests `mapstructure:"stored_video_req"`
StoredResponses StoredRequests `mapstructure:"stored_responses"`
// StoredRequestsTimeout defines the number of milliseconds before a timeout occurs with stored requests fetch
StoredRequestsTimeout int `mapstructure:"stored_requests_timeout_ms"`

MaxRequestSize int64 `mapstructure:"max_request_size"`
Analytics Analytics `mapstructure:"analytics"`
Expand Down Expand Up @@ -132,6 +134,9 @@ func (cfg *Configuration) validate(v *viper.Viper) []error {
var errs []error
errs = cfg.AuctionTimeouts.validate(errs)
errs = cfg.StoredRequests.validate(errs)
if cfg.StoredRequestsTimeout <= 0 {
errs = append(errs, fmt.Errorf("cfg.stored_requests_timeout_ms must be > 0. Got %d", cfg.StoredRequestsTimeout))
}
errs = cfg.StoredRequestsAMP.validate(errs)
errs = cfg.Accounts.validate(errs)
errs = cfg.CategoryMapping.validate(errs)
Expand Down Expand Up @@ -964,6 +969,7 @@ func SetupViper(v *viper.Viper, filename string, bidderInfos BidderInfos) {
v.SetDefault("category_mapping.filesystem.enabled", true)
v.SetDefault("category_mapping.filesystem.directorypath", "./static/category-mapping")
v.SetDefault("category_mapping.http.endpoint", "")
v.SetDefault("stored_requests_timeout_ms", 50)
v.SetDefault("stored_requests.database.connection.driver", "")
v.SetDefault("stored_requests.database.connection.dbname", "")
v.SetDefault("stored_requests.database.connection.host", "")
Expand Down
14 changes: 14 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func TestDefaults(t *testing.T) {
cmpBools(t, "adapter_buyeruid_scrubbed", true, cfg.Metrics.Disabled.AdapterBuyerUIDScrubbed)
cmpBools(t, "adapter_gdpr_request_blocked", false, cfg.Metrics.Disabled.AdapterGDPRRequestBlocked)
cmpStrings(t, "certificates_file", "", cfg.PemCertsFile)
cmpInts(t, "stored_requests_timeout_ms", 50, cfg.StoredRequestsTimeout)
cmpBools(t, "stored_requests.filesystem.enabled", false, cfg.StoredRequests.Files.Enabled)
cmpStrings(t, "stored_requests.filesystem.directorypath", "./stored_requests/data/by_id", cfg.StoredRequests.Files.Path)
cmpBools(t, "auto_gen_source_tid", true, cfg.AutoGenSourceTID)
Expand Down Expand Up @@ -400,6 +401,7 @@ external_url: http://prebid-server.prebid.org/
host: prebid-server.prebid.org
port: 1234
admin_port: 5678
stored_requests_timeout_ms: 75
compression:
request:
enable_gzip: true
Expand Down Expand Up @@ -612,6 +614,7 @@ func TestFullConfig(t *testing.T) {
cmpInts(t, "garbage_collector_threshold", 1, cfg.GarbageCollectorThreshold)
cmpInts(t, "auction_timeouts_ms.default", 50, int(cfg.AuctionTimeouts.Default))
cmpInts(t, "auction_timeouts_ms.max", 123, int(cfg.AuctionTimeouts.Max))
cmpInts(t, "stored_request_timeout_ms", 75, cfg.StoredRequestsTimeout)
cmpStrings(t, "cache.scheme", "http", cfg.CacheURL.Scheme)
cmpStrings(t, "cache.host", "prebidcache.net", cfg.CacheURL.Host)
cmpStrings(t, "cache.query", "uuid=%PBS_CACHE_UUID%", cfg.CacheURL.Query)
Expand Down Expand Up @@ -912,6 +915,7 @@ func TestValidateConfig(t *testing.T) {
Type: "none",
},
},
StoredRequestsTimeout: 50,
StoredVideo: StoredRequests{
Files: FileFetcherConfig{Enabled: true},
InMemoryCache: InMemoryCache{
Expand Down Expand Up @@ -1173,6 +1177,16 @@ func TestIsConfigInfoPresent(t *testing.T) {
}
}

func TestNegativeOrZeroStoredRequestsTimeout(t *testing.T) {
cfg, v := newDefaultConfig(t)

cfg.StoredRequestsTimeout = -1
assertOneError(t, cfg.validate(v), "cfg.stored_requests_timeout_ms must be > 0. Got -1")

cfg.StoredRequestsTimeout = 0
assertOneError(t, cfg.validate(v), "cfg.stored_requests_timeout_ms must be > 0. Got 0")
}

func TestNegativeRequestSize(t *testing.T) {
cfg, v := newDefaultConfig(t)
cfg.MaxRequestSize = -1
Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func (deps *endpointDeps) loadRequestJSONForAmp(httpRequest *http.Request) (req
return nil, nil, nil, nil, []error{err}
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(storedRequestTimeoutMillis)*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(deps.cfg.StoredRequestsTimeout)*time.Millisecond)
defer cancel()

storedRequests, _, errs := deps.storedReqFetcher.FetchRequests(ctx, []string{ampParams.StoredRequestID}, nil)
Expand Down
3 changes: 1 addition & 2 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import (
"github.com/prebid/prebid-server/v2/version"
)

const storedRequestTimeoutMillis = 50
const ampChannel = "amp"
const appChannel = "app"
const secCookieDeprecation = "Sec-Cookie-Deprecation"
Expand Down Expand Up @@ -469,7 +468,7 @@ func (deps *endpointDeps) parseRequest(httpRequest *http.Request, labels *metric
return
}

timeout := parseTimeout(requestJson, time.Duration(storedRequestTimeoutMillis)*time.Millisecond)
timeout := parseTimeout(requestJson, time.Duration(deps.cfg.StoredRequestsTimeout)*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion endpoints/openrtb2/video_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func createImpressionTemplate(imp openrtb2.Imp, video *openrtb2.Video) openrtb2.
}

func (deps *endpointDeps) loadStoredImp(storedImpId string) (openrtb2.Imp, []error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(storedRequestTimeoutMillis)*time.Millisecond)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(deps.cfg.StoredRequestsTimeout)*time.Millisecond)
defer cancel()

impr := openrtb2.Imp{}
Expand Down

0 comments on commit 74fbb38

Please sign in to comment.