In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
What is the function of Main method in NSQLookupd? aiming at this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Type NSQLookupd struct {
Options * nsqlookupdOptions / / command line arguments
TcpAddr * net.TCPAddr / / tcp port
HttpAddr * net.TCPAddr / / http port
TcpListener net.Listener
HttpListener net.Listener
WaitGroup util.WaitGroupWrapper
DB * RegistrationDB
}
This article analyzes the execution process of daemon.Main ().
1. Create a context with only one pointer to NSQLookupd inside.
Context: = & Context {l}
2. Create a listener;Listen method to support stream-oriented listening, including unix domain stream socket.
TcpListener, err: = net.Listen ("tcp", l.tcpAddr.String ())
3. Create a tcpServer struct with only one context pointer inside, so why do you need to check the NSQLookupd
TcpServer implements the Handle (net.Conn) method, indicating that the TCPHandler interface is implemented.
TcpServer: = & tcpServer {context: context}
4. Analyze l.waitGroup.Wrap (func () {util.TCPServer (tcpListener, tcpServer)})
WaitGroup is an instance of the structure util.WaitGroupWrapper, which inherits from sync.WaitGroup;WaitGroup waiting for a set of collaborations to end. The main program calls the Add method to set the number of waiting programs; func () {util.TCPServer (tcpListener, tcpServer)} this is an anonymous function.
Summary: the main program calls the Wrap method, calls Add, sets the number of waiting programs to 1, starts a new program to call anonymous functions, and finally waits for messages on the exitChan channel. If it receives an interrupt signal, it closes tcpListener and httpListner, and finally waits for the end of the program on the waitGroup. The purpose of waitGroup is to synchronize the state between the master and listener protocols.
5. The logic for listening to the execution of the protocol is as follows: simplify the version and ignore logging and error handling. Listens to the co-program loop Accept, and then assigns a new co-program to call handler to process the received clientConn. A collaborator that handles client connections, which I call a work collaborator.
Func TCPServer (listener net.Listener, handler TCPHandler) {
For {
ClientConn, err: = listener.Accept ()
Go handler.Handle (clientConn)
}
}
6. The processing logic of the work cooperation program, the final call is the Handle method of tcpServer, which is the most important method, focusing on the analysis
Func (p * tcpServer) Handle (clientConn net.Conn)
Buf: = make ([] byte, 4)
_, err: = io.ReadFull (clientConn, buf)
Read the first 4 bytes sent by the client, which contains the version number of the protocol. See, completely blocking programming, no shit-like event-driven!
After determining the version number, the IOLoop method of LookupProtocolV1 is finally called.
7. Methods to analyze func (p * LookupProtocolV1) IOLoop (conn net.Conn) error
Client: = NewClientV1 (conn) / / ClientV1 struct inherits net.Conn and encapsulates a PeerInfo
Reader: = bufio.NewReader (client) / / create a reader with buffering
For {/ / this part of the code is omitted
Line, err = reader.ReadString ('\ n') / / processed by line
Line = strings.TrimSpace (line)
Params: = strings.Split (line, "")
Response, err: = p.Exec (client, reader, params) / / execute the command
If response! = nil {
_, err = util.SendResponse (client, response) / / send response
}
}
An error in IO, or an error in executing a command, exits the loop.
8. Order dispatch
Func (p * LookupProtocolV1) Exec (client * ClientV1, reader * bufio.Reader, params [] string) ([] byte, error)
Switch params [0] {
Case "PING":
Return p.PING (client, params)
/ /.
}
Make a command distinction according to the first parameter, and call the response instruction
9. Command execution. Take the ping command as an example to see how the command is executed.
Func (p * LookupProtocolV1) PING (client * ClientV1, params [] string) ([] byte, error)
Atomic.StoreInt64 (& client.peerInfo.lastUpdate, now.UnixNano ()) / / Update the status of the client
Return [] byte ("OK"), nil / / returns "OK", which is sent to client via util.SendResponse in step 7
At this point, the processing flow of tcpServer is introduced.
10. Corresponding to step 2, httpListener and httpServer will also be created, which are mainly responsible for topic,channel-related operations.
HttpListener, err: = net.Listen ("tcp", l.httpAddr.String ())
HttpServer: = & httpServer {context: context}
Func () {util.HTTPServer (httpListener, httpServer, "HTTP")} is called for handling httpServer's co-program.
11. Analyze util.HTTPServer function
The key is the following two sentences
Server: = & http.Server {
Handler: handler
}
Err: = server.Serve (listener)
The default http handler is httpServer itself.
12. ServeHTTP method for analyzing httpServer
Err: = s.v1Router (w, req) / / routing request
13. Analyze v1Router
Switch req.URL.Path {
Case "/ ping":
/ /.
}
Dispatch according to the request path
14. Take "/ topics" as an example to see how to handle the request
Execute the function util.NegotiateAPIResponseWrapper (w, req)
Func () (interface {}, error) {return s.doTopics (req)})
The answer to the question about the role of Main method in NSQLookupd is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.