36 lines
503 B
Go
36 lines
503 B
Go
package racer
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func Racer(a, b string) string {
|
|
select {
|
|
// wait until channel closed
|
|
case <-ping(a):
|
|
return a
|
|
case <-ping(b):
|
|
return b
|
|
}
|
|
}
|
|
|
|
func measureResponseTime(url string) time.Duration {
|
|
start := time.Now()
|
|
http.Get(url)
|
|
duration := time.Since(start)
|
|
fmt.Println(duration)
|
|
return duration
|
|
}
|
|
|
|
func ping(url string) chan struct{} {
|
|
ch := make(chan struct{})
|
|
go func() {
|
|
http.Get(url)
|
|
// if got, close channel
|
|
close(ch)
|
|
}()
|
|
return ch
|
|
}
|