Skip to content

Instantly share code, notes, and snippets.

@reusee
Created June 22, 2020 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reusee/823b01af418404a52d38d1a57ee9727e to your computer and use it in GitHub Desktop.
Save reusee/823b01af418404a52d38d1a57ee9727e to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"sync"
"time"
)
func main() {
req, err := http.NewRequest("GET", "http://qq.com", nil)
check(err)
ch := make(chan struct{})
req.Cancel = ch
var closeOnce sync.Once
cancel := func() {
closeOnce.Do(func() {
close(ch)
})
}
timer := time.AfterFunc(time.Second*20, func() {
cancel()
})
timer.Reset(time.Second * 30) // 重设超时
cancel() // 手工取消
}
func check(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment