]> Cypherpunks repositories - gostls13.git/commitdiff
net: support context cancellation in resSearch
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Sat, 3 Dec 2022 08:14:33 +0000 (08:14 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 5 Dec 2022 20:45:49 +0000 (20:45 +0000)
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>
src/net/cgo_unix.go

index 209724cf1d68537951eacb14695a512bae1fd200..5b0df56eae8a57bf7266a47215813ad6aa8f37e2 100644 (file)
@@ -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()