From e950ef8245613007093f701b4207b979b7bf6795 Mon Sep 17 00:00:00 2001 From: khorshuheng Date: Thu, 21 Jan 2021 23:54:06 +0800 Subject: [PATCH] Pregenerate requests before serving Signed-off-by: khorshuheng --- main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 94bdf02..a5a5839 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "gopkg.in/yaml.v2" "io/ioutil" "log" + "math/rand" "net/http" "sync" "time" @@ -52,12 +53,15 @@ func main() { if err != nil { log.Fatalf("Unable to instantiate request requestGenerator: %v", err) } + requestsPool := make([][]feast.OnlineFeaturesRequest, 100) + for i := 0; i < 100; i++ { + requestsPool[i] = requestGenerator.GenerateRequests() + } http.HandleFunc("/send", func(w http.ResponseWriter, r *http.Request) { - requests := requestGenerator.GenerateRequests() - ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() + requests := requestsPool[rand.Intn(len(requestsPool))] if len(requests) == 1 { resp, err := client.GetOnlineFeatures(ctx, &requests[0]) if err != nil {