Last active
April 15, 2019 20:27
-
-
Save arschles/3dc80ec13bc3e800dba1e7aea0e540ee to your computer and use it in GitHub Desktop.
GetWithRetry
This file contains 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 ( | |
"context" | |
"fmt" | |
"time" | |
) | |
// adaptation of | |
// https://github.com/Azure/aks-engine/blob/f61205726c139d286c427b3403942a079eb1b26d/test/e2e/kubernetes/pod/pod.go#L289 | |
func doPodThings() struct{} { | |
// uncomment the below sleep to toggle the error codepath | |
// time.Sleep(3 * time.Second) | |
return struct{}{} | |
} | |
func main() { | |
ctxDur := 2 * time.Second | |
ctx, cancel := context.WithTimeout(context.Background(), ctxDur) | |
defer cancel() | |
ch := make(chan struct{}) | |
go func() { | |
for { | |
select { | |
case <-ctx.Done(): | |
// don't send error here. it'll be returned in the | |
// select in the main goroutine | |
return | |
case ch <- doPodThings(): | |
} | |
} | |
}() | |
// you return on the chan receive so you don't need a loop here | |
for { | |
select { | |
case <-ch: | |
// you would return the pod here | |
fmt.Println("yay!") | |
case <-ctx.Done(): | |
// this is where you return the error | |
fmt.Println("oh noes! timeout after", ctxDur) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment