aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2022-12-03 08:14:33 +0000
committerGopher Robot <gobot@golang.org>2022-12-05 20:45:49 +0000
commita5b10be471b92847c14fc07e0564a298c7e49a3b (patch)
treeebde4e5f9592cea39270c0848e52349e5332f00a
parentb45cb0ce863f3cf91a1847d44a9474aad2a6c592 (diff)
downloadgo-a5b10be471b92847c14fc07e0564a298c7e49a3b.tar.gz
go-a5b10be471b92847c14fc07e0564a298c7e49a3b.zip
net: support context cancellation in resSearch
As with all the stuff that call cgo from net package. Change-Id: I7c42ae44a1d47f4f949b203682217498fcdba92a GitHub-Last-Rev: 70406493bbbe10bf556a17e453623d3decf00822 GitHub-Pull-Request: golang/go#57043 Reviewed-on: https://go-review.googlesource.com/c/go/+/454697 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
-rw-r--r--src/net/cgo_unix.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/net/cgo_unix.go b/src/net/cgo_unix.go
index 209724cf1d..5b0df56eae 100644
--- a/src/net/cgo_unix.go
+++ b/src/net/cgo_unix.go
@@ -331,6 +331,33 @@ func cgoLookupCNAME(ctx context.Context, name string) (cname string, err error,
// resSearch will make a call to the 'res_nsearch' routine in the C library
// and parse the output as a slice of DNS resources.
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 {
+ res []dnsmessage.Resource
+ err error
+ }
+
+ res := make(chan result, 1)
+ go func() {
+ r, err := cgoResSearch(hostname, rtype, class)
+ res <- result{
+ res: r,
+ err: err,
+ }
+ }()
+
+ select {
+ case res := <-res:
+ return res.res, res.err
+ case <-ctx.Done():
+ return nil, mapErr(ctx.Err())
+ }
+}
+
+func cgoResSearch(hostname string, rtype, class int) ([]dnsmessage.Resource, error) {
acquireThread()
defer releaseThread()