Created
December 3, 2022 06:02
-
-
Save fzdwx/3ccaefe0d543223b9e66f617a2a08a92 to your computer and use it in GitHub Desktop.
go ctx react impl
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
func resSearch(ctx context.Context, hostname string, rtype, class int) ([]dnsmessage.Resource, error) { | |
if ctx.Done() == nil { | |
return cgoResSearch(hostname, rtype, class) | |
} | |
type result struct { | |
err error | |
res []dnsmessage.Resource | |
} | |
res := make(chan result, 1) | |
go func() { | |
r, err := cgoResSearch(hostname, rtype, class) | |
res <- result{ | |
err: err, | |
res: r, | |
} | |
}() | |
select { | |
case res := <-res: | |
return res.res, res.err | |
case <-ctx.Done(): | |
return nil, mapErr(ctx.Err()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment