-
-
Save reusee/823b01af418404a52d38d1a57ee9727e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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