Last active
November 3, 2019 19:24
-
-
Save karampok/021bb3e8745cbc76bc8efb46709b4d39 to your computer and use it in GitHub Desktop.
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
// 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