Last active
May 16, 2020 19:18
-
-
Save RicardoLinck/f779e22c548c46bb95c5299a26d72a79 to your computer and use it in GitHub Desktop.
Hedged requests implementation in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"time" | |
) | |
func queryWithHedgedRequests(urls []string) string { | |
ch := make(chan string, len(urls)) | |
for _, url := range urls { | |
go func(u string, c chan string) { | |
c <- executeQuery(u) | |
}(url, ch) | |
select { | |
case r := <-ch: | |
return r | |
case <-time.After(21 * time.Millisecond): | |
} | |
} | |
return <-ch | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment