Go_ default route source analysis

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

Func main () {http. Handlefunc ("/say", say) Err: = http. Listenandserve (": 8080", nil) if err! = Nil {log. Fatal (Err. Error ())}}func say (w http. Responsewriter, R *http. Request) {//}
Above is a section of the code that starts the Web service by listening on port 8080 and registering the Say () function at startup money

If the browser input Localhost:8080/say will be the default callback say function today to analyze the general process of this

First of all in Go do request forwarding can use the system default router can also use their own custom router but must be to implement the handler interface

Type Handler Interface {servehttp (Responsewriter, *request)}
In this large number of routes involving handler this interface is more than just a custom route and a default route to implement this interface even the example in the beginning of the article say function is forced to convert the type to Handlefunc type in Server.go after registration And because Handlefunc can implement the Servehttp method, so the handler interface is implemented, so each registered function is finally treated as a handler type.

The following describes the approximate process of routing request distribution

http. Listenerandserver (Addr,nil) Listening port addr Address the second parameter sets your own implementation of the route if Nil then uses the default

Monitor the addr port via L via the returned port listener. Aceept Accept Connection Go C.serve () Create a new co-process for the connection to be handled specifically because there is a for loop so continue waiting to accept

Func (SRV *server) Serve (l net. Listener) Error {defer l.close () var tempdelay time. Duration//How long-to-sleep on accept failurefor {RW, E: = L.accept () if E! = nil {if NE, ok: = E. (NET. ERROR); Ok && ne. Temporary () {if Tempdelay = = 0 {tempdelay = 5 * Time.millisecond} else {tempdelay *= 2}if max: = 1 * time. Second; Tempdelay > Max {tempdelay = Max}log. Printf ("Http:accept error:%v; Retrying in%v ", E, Tempdelay) time. Sleep (tempdelay) Continue}return e}tempdelay = 0c, err: = Srv.newconn (rw) if err! = Nil {Continue}go c.serve ()}}


It's a more critical step that I'm going to summarize.



The client comes to the request and then receives the initiation coprocessor processing where serverhandler This step is server.go internal private structure to determine whether to choose the developer's own implementation of the route or the default route

Either route is called by the servehttp of the route and then routed within Servehttp to handle the specific distribution request itself


If it is the default route then it will eventually match the corresponding handler according to the path and then return to note that the path here is a path to the beginning of a path corresponding to a function that is a handler

When the handler is returned, the handler Servehttp is called directly inside the method (because it is a function) and then the concrete external method is implemented.



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.