1
0
mirror of https://github.com/golang/go synced 2024-11-14 20:20:30 -07:00

net/http: use sync.OnceFunc, sync.OnceValue

Use sync.OnceFunc and sync.OnceValue to simplify the code.

Change-Id: Ie47e0444c2b9d3260f6ef94cdc6ee8ee5bcf9f71
GitHub-Last-Rev: 520afbec2a
GitHub-Pull-Request: golang/go#69634
Reviewed-on: https://go-review.googlesource.com/c/go/+/616037
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
apocelipes 2024-09-26 08:23:13 +00:00 committed by Gopher Robot
parent 9ace960d54
commit 3f2737f5dd
2 changed files with 15 additions and 24 deletions

View File

@ -388,15 +388,12 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
}
stopTimerCh := make(chan struct{})
var once sync.Once
stopTimer = func() {
once.Do(func() {
stopTimer = sync.OnceFunc(func() {
close(stopTimerCh)
if cancelCtx != nil {
cancelCtx()
}
})
}
timer := time.NewTimer(time.Until(deadline))
var timedOut atomic.Bool

View File

@ -2586,8 +2586,8 @@ func runCancelTestTransport(t *testing.T, mode testMode, f func(t *testing.T, te
// runCancelTestChannel uses Request.Cancel.
func runCancelTestChannel(t *testing.T, mode testMode, f func(t *testing.T, test cancelTest)) {
var cancelOnce sync.Once
cancelc := make(chan struct{})
cancelOnce := sync.OnceFunc(func() { close(cancelc) })
f(t, cancelTest{
mode: mode,
newReq: func(req *Request) *Request {
@ -2595,9 +2595,7 @@ func runCancelTestChannel(t *testing.T, mode testMode, f func(t *testing.T, test
return req
},
cancel: func(tr *Transport, req *Request) {
cancelOnce.Do(func() {
close(cancelc)
})
cancelOnce()
},
checkErr: func(when string, err error) {
if !errors.Is(err, ExportErrRequestCanceled) && !errors.Is(err, ExportErrRequestCanceledConn) {
@ -5114,20 +5112,16 @@ func testTransportEventTraceTLSVerify(t *testing.T, mode testMode) {
}
}
var (
isDNSHijackedOnce sync.Once
isDNSHijacked bool
)
var isDNSHijacked = sync.OnceValue(func() bool {
addrs, _ := net.LookupHost("dns-should-not-resolve.golang")
return len(addrs) != 0
})
func skipIfDNSHijacked(t *testing.T) {
// Skip this test if the user is using a shady/ISP
// DNS server hijacking queries.
// See issues 16732, 16716.
isDNSHijackedOnce.Do(func() {
addrs, _ := net.LookupHost("dns-should-not-resolve.golang")
isDNSHijacked = len(addrs) != 0
})
if isDNSHijacked {
if isDNSHijacked() {
t.Skip("skipping; test requires non-hijacking DNS server")
}
}
@ -5463,7 +5457,7 @@ func TestTransportReturnsPeekError(t *testing.T) {
errValue := errors.New("specific error value")
wrote := make(chan struct{})
var wroteOnce sync.Once
wroteOnce := sync.OnceFunc(func() { close(wrote) })
tr := &Transport{
Dial: func(network, addr string) (net.Conn, error) {
@ -5473,7 +5467,7 @@ func TestTransportReturnsPeekError(t *testing.T) {
return 0, errValue
},
write: func(p []byte) (int, error) {
wroteOnce.Do(func() { close(wrote) })
wroteOnce()
return len(p), nil
},
}