Golang captures HTTP. Responsewriter two ways of being close (with or without context)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Golang captures HTTP. Responsewriter two ways of being close (with or without context)

Easy to be circulated, the use of Chinese, in fact, used to look at English, found that Chinese for some problems, read up around the mouth, followed by a lot of direct copy of English, the following simple introduction:
- Why the server needs to know that the HTTP connection is disconnected (client active cancel)
- The simplest way to capture
- How to capture a message when the context is used to pass it
- How to continue delivery after context capture

Demand

Spit trough oneself, the first time with Csdn's new editor is very unfamiliar

Most Web Requests is a few dozen milliseconds to process. But sometimes web apps need to leave a connection open for a longer period of time. And sometimes the remote client closes the connection before the server has had time to respond.

On a go-based webserver, you can receive notifications when the HTTP connection terminates.
This is very good, the client if it shuts down what to do, we can not sit idly by.

Catch notification of Cancel

A simple usage
Start with a HTTP handler function, and get the channel for close notifications:

Func Somehandler (resp http. Resonsewriter, req *http. Request) {
Normal Stuff
...

notify := resp.(CloseNotifier).CloseNotify()go func() {    <-notify    lock.RLock()    fmt.Println("HTTP connection just closed.")    lock.RUnlock()}()

}

When an incoming parameter is not determined whether it is RESP
First make a simple judgment, using reflect, assuming that RESP is what you think of HTTP. Responsewriter
V: = reflect. ValueOf (RESP)
v = reflect. Indirect (v)
For v.kind () = = reflect. Struct {
If FV: = V.fieldbyname ("Responsewriter"); Fv. IsValid () {
If cn, OK = FV. Interface (). (HTTP. Closenotifier); OK {
Return
}
v = reflect. Indirect (FV)
} else {
Break
}
}

Notifications are only one time and need to be passed back

This is Golang's introduction to the context, Https://godoc.org/golang.org/x/net/context

CTX, Cancel: = context. Withcancel (context. Background ())
When notified, the Cancel method is executed, and if subsequent operations also rely on this notification, then another signal is needed, CTX. Done ()

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.