Skip to content

Instantly share code, notes, and snippets.

@karampok
Last active November 3, 2019 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karampok/021bb3e8745cbc76bc8efb46709b4d39 to your computer and use it in GitHub Desktop.
Save karampok/021bb3e8745cbc76bc8efb46709b4d39 to your computer and use it in GitHub Desktop.
// UnionFetcher fetches from east and west and combines the result. If one
// fetcher fails only the reply of the other fetcher is returned.
type UnionFetcher struct {
East, West PathFetcher
}
// GetPaths gets paths from both fetchers.
func (f *UnionFetcher) GetPaths(ctx context.Context, req *sciond.PathReq,
earlyReplyInterval time.Duration, logger log.Logger) (*sciond.PathReply, error) {
results:=make(chan *sciond.PathReply,2) //space for two non-blocking reads
for _, target:= {f.east,f.west}{
go func(){
r,err:= <- target.GetPaths(ctx, req, earlyReplyInterval, logger)
if err!=nil{ //logme}
results <- r // non-blocking because size 2, max ctx timeout then goroutine exits
}()
}
return mergeReplies(<-result, <-result) //max-time the ctx both goroutine have returned
}
func mergeReplies(e, w *sciond.PathReply) (*sciond.PathReply, error) {
entries := []sciond.PathReplyEntry{}
if e.ErrorCode == sciond.ErrorOk {
entries = append(entries, e.Entries...)
}
if w.ErrorCode == sciond.ErrorOk {
entries = append(entries, w.Entries...)
}
//happy path
return &sciond.PathReply{
Entries: entries,
ErrorCode: sciond.ErrorOk,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment