Skip to content

Instantly share code, notes, and snippets.

View RicardoLinck's full-sized avatar

Ricardo Linck RicardoLinck

View GitHub Profile
@RicardoLinck
RicardoLinck / hedgedrequests.go
Last active May 16, 2020 19:18
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) {
@RicardoLinck
RicardoLinck / external-service.go
Created May 20, 2020 10:33
Service with hardcoded wait time for 4% of requests
package main
import (
"math/rand"
"net/http"
"time"
"github.com/gorilla/mux"
)
@RicardoLinck
RicardoLinck / fanout.go
Created May 20, 2020 10:34
Fanout implementation in go.
package main
func queryFanOut(urls []string) string {
ch := make(chan string, len(urls))
for _, url := range urls {
go func(u string) {
ch <- executeQuery(u)
}(url)
}
return <-ch
@RicardoLinck
RicardoLinck / query.go
Created May 20, 2020 10:34
Query used in other examples
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
func executeQuery(url string) string {
package main
import (
"fmt"
)
func main() {
go printSomething()
fmt.Print("1")
}
package main
import "fmt"
func main() {
c := make(chan int)
q := 10
go generateInts(c, q)
for i := range c {
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int, 3)
q := 10
type data struct {
// ...
}
func handleHTTPRequest(w http.ResponseWriter, r *http.Request) {
// ...
c := make(chan data)
defer close(c)
defer r.Body.Close()
go saveDataFromChannel(c)
type data struct {
// ...
}
func main() {
c := make(chan data)
defer close(c)
go saveDataFromChannel(c)
wg := &sync.WaitGroup{}
package main
import (
"fmt"
"io"
"net/http"
)
type externalService interface {
getData(r io.Reader) string