Skip to content

Instantly share code, notes, and snippets.

@RicardoLinck
Last active May 16, 2020 19:18
Show Gist options
  • Save RicardoLinck/f779e22c548c46bb95c5299a26d72a79 to your computer and use it in GitHub Desktop.
Save RicardoLinck/f779e22c548c46bb95c5299a26d72a79 to your computer and use it in GitHub Desktop.
Hedged requests implementation in go
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