mirror of
https://github.com/golang/go
synced 2024-11-14 23:00:29 -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:
parent
9ace960d54
commit
3f2737f5dd
@ -388,15 +388,12 @@ func setRequestCancel(req *Request, rt RoundTripper, deadline time.Time) (stopTi
|
|||||||
}
|
}
|
||||||
|
|
||||||
stopTimerCh := make(chan struct{})
|
stopTimerCh := make(chan struct{})
|
||||||
var once sync.Once
|
stopTimer = sync.OnceFunc(func() {
|
||||||
stopTimer = func() {
|
|
||||||
once.Do(func() {
|
|
||||||
close(stopTimerCh)
|
close(stopTimerCh)
|
||||||
if cancelCtx != nil {
|
if cancelCtx != nil {
|
||||||
cancelCtx()
|
cancelCtx()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
timer := time.NewTimer(time.Until(deadline))
|
timer := time.NewTimer(time.Until(deadline))
|
||||||
var timedOut atomic.Bool
|
var timedOut atomic.Bool
|
||||||
|
@ -2586,8 +2586,8 @@ func runCancelTestTransport(t *testing.T, mode testMode, f func(t *testing.T, te
|
|||||||
|
|
||||||
// runCancelTestChannel uses Request.Cancel.
|
// runCancelTestChannel uses Request.Cancel.
|
||||||
func runCancelTestChannel(t *testing.T, mode testMode, f func(t *testing.T, test cancelTest)) {
|
func runCancelTestChannel(t *testing.T, mode testMode, f func(t *testing.T, test cancelTest)) {
|
||||||
var cancelOnce sync.Once
|
|
||||||
cancelc := make(chan struct{})
|
cancelc := make(chan struct{})
|
||||||
|
cancelOnce := sync.OnceFunc(func() { close(cancelc) })
|
||||||
f(t, cancelTest{
|
f(t, cancelTest{
|
||||||
mode: mode,
|
mode: mode,
|
||||||
newReq: func(req *Request) *Request {
|
newReq: func(req *Request) *Request {
|
||||||
@ -2595,9 +2595,7 @@ func runCancelTestChannel(t *testing.T, mode testMode, f func(t *testing.T, test
|
|||||||
return req
|
return req
|
||||||
},
|
},
|
||||||
cancel: func(tr *Transport, req *Request) {
|
cancel: func(tr *Transport, req *Request) {
|
||||||
cancelOnce.Do(func() {
|
cancelOnce()
|
||||||
close(cancelc)
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
checkErr: func(when string, err error) {
|
checkErr: func(when string, err error) {
|
||||||
if !errors.Is(err, ExportErrRequestCanceled) && !errors.Is(err, ExportErrRequestCanceledConn) {
|
if !errors.Is(err, ExportErrRequestCanceled) && !errors.Is(err, ExportErrRequestCanceledConn) {
|
||||||
@ -5114,20 +5112,16 @@ func testTransportEventTraceTLSVerify(t *testing.T, mode testMode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var isDNSHijacked = sync.OnceValue(func() bool {
|
||||||
isDNSHijackedOnce sync.Once
|
addrs, _ := net.LookupHost("dns-should-not-resolve.golang")
|
||||||
isDNSHijacked bool
|
return len(addrs) != 0
|
||||||
)
|
})
|
||||||
|
|
||||||
func skipIfDNSHijacked(t *testing.T) {
|
func skipIfDNSHijacked(t *testing.T) {
|
||||||
// Skip this test if the user is using a shady/ISP
|
// Skip this test if the user is using a shady/ISP
|
||||||
// DNS server hijacking queries.
|
// DNS server hijacking queries.
|
||||||
// See issues 16732, 16716.
|
// See issues 16732, 16716.
|
||||||
isDNSHijackedOnce.Do(func() {
|
if isDNSHijacked() {
|
||||||
addrs, _ := net.LookupHost("dns-should-not-resolve.golang")
|
|
||||||
isDNSHijacked = len(addrs) != 0
|
|
||||||
})
|
|
||||||
if isDNSHijacked {
|
|
||||||
t.Skip("skipping; test requires non-hijacking DNS server")
|
t.Skip("skipping; test requires non-hijacking DNS server")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5463,7 +5457,7 @@ func TestTransportReturnsPeekError(t *testing.T) {
|
|||||||
errValue := errors.New("specific error value")
|
errValue := errors.New("specific error value")
|
||||||
|
|
||||||
wrote := make(chan struct{})
|
wrote := make(chan struct{})
|
||||||
var wroteOnce sync.Once
|
wroteOnce := sync.OnceFunc(func() { close(wrote) })
|
||||||
|
|
||||||
tr := &Transport{
|
tr := &Transport{
|
||||||
Dial: func(network, addr string) (net.Conn, error) {
|
Dial: func(network, addr string) (net.Conn, error) {
|
||||||
@ -5473,7 +5467,7 @@ func TestTransportReturnsPeekError(t *testing.T) {
|
|||||||
return 0, errValue
|
return 0, errValue
|
||||||
},
|
},
|
||||||
write: func(p []byte) (int, error) {
|
write: func(p []byte) (int, error) {
|
||||||
wroteOnce.Do(func() { close(wrote) })
|
wroteOnce()
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user