From 355ab1c6d7c5b3925bbcd46163ba49412134823c Mon Sep 17 00:00:00 2001 From: Everlag Date: Sun, 3 Jan 2016 16:39:28 -0700 Subject: [PATCH] smarter bulk price fetches Previously, any single card for a batch would compromise the entire set of prices. This allows up to 1/3 of a bulk fetch to fail. closes #11 --- common/priceDB/bulk.go | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/common/priceDB/bulk.go b/common/priceDB/bulk.go index cd61f81..39ad65c 100755 --- a/common/priceDB/bulk.go +++ b/common/priceDB/bulk.go @@ -2,6 +2,8 @@ package priceDB import ( "github.com/jackc/pgx" + + "fmt" ) func GetBulkLatestLowest(pool *pgx.ConnPool, @@ -9,15 +11,32 @@ func GetBulkLatestLowest(pool *pgx.ConnPool, result:= make(Prices, len(names)) + failed:= 0 + for i, c:= range names { p, err:= GetCardLatestLowest(pool, c, source) if err!=nil { - return nil, err + // Record this as a failure + failed++ + + // Insert a placeholder + p = Price{ + Name: c, + Set: "Unknown", + Price: -1, + Source: source, + } } result[i] = p } + // If we fail to fetch more than 1/3 of prices for this bulk + // set, we error out + if failed >= len(names) / 3 { + return nil, fmt.Errorf("too many fetch failures") + } + return result, nil } @@ -26,14 +45,31 @@ func GetBulkLatestHighest(pool *pgx.ConnPool, result:= make(Prices, len(names)) + failed:= 0 + for i, c:= range names { p, err:= GetCardLatestHighest(pool, c, source) if err!=nil { - return nil, err + // Record this as a failure + failed++ + + // Insert a placeholder + p = Price{ + Name: c, + Set: "Unknown", + Price: -1, + Source: source, + } } result[i] = p } + // If we fail to fetch more than 1/3 of prices for this bulk + // set, we error out + if failed >= len(names) / 3 { + return nil, fmt.Errorf("too many fetch failures") + } + return result, nil } \ No newline at end of file