In this article, we will explore the ServeRequest Function in net/rpc package in Go in detail, along with examples.
Introduction:
Go, also known as Golang, is a powerful programming language designed for building concurrent and networked applications. One of the standard libraries provided by Go is the ‘net/rpc’ package, which enables developers to create and use Remote Procedure Calls (RPC) for communication between different application components. In this article, we will explore the ServeRequest function, a feature of the ‘net/rpc’ package, and discuss its purpose, usage, and a practical example to better understand its functionality.
What is ServeRequest Function ?
The ServeRequest function is part of the ‘net/rpc’ package in Go, which primarily deals with creating RPC clients and servers. This function serves a single request, using the provided codec to decode the request and encode the response. It enables developers to serve individual requests without needing to implement a listener or serve a codec.
Function Signature
The function signature for ServeRequest is as follows:
func (server *Server) ServeRequest(codec ServerCodec) error
The ServeRequest function is a method of the ‘Server’ type in the ‘net/rpc’ package. It takes a ‘ServerCodec’ as input, which is an interface used for reading requests and writing responses.
Example
In this example, we will demonstrate the usage of the ServeRequest function with a custom codec, the JSON-RPC codec. To achieve this, we will use the ‘github.com/gorilla/rpc/v2/json2’ package.
package main
import (
"fmt"
"net"
"net/rpc"
"github.com/gorilla/rpc/v2/json2"
)
type Greeter struct{}
func (g *Greeter) Greet(name *string, reply *string) error {
*reply = fmt.Sprintf("Hello, %s!", *name)
return nil
}
func main() {
addr := "localhost:12345"
greeter := new(Greeter)
rpc.Register(greeter)
listener, err := net.Listen("tcp", addr)
if err != nil {
fmt.Println("Error listening:", err)
return
}
defer listener.Close()
fmt.Printf("RPC server listening on %s\n", addr)
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting connection:", err)
continue
}
codec := json2.NewCodec(conn)
err = rpc.DefaultServer.ServeRequest(codec)
if err != nil {
fmt.Println("Error serving request:", err)
}
conn.Close()
}
}
In this example, we create a simple RPC server that responds to Greet function calls. We define a ‘Greeter’ type and a ‘Greet’ method for that type. The ‘Greet’ method takes a name as input and returns a greeting message.
We then create an instance of ‘Greeter’, register it with the ‘net/rpc’ package using ‘rpc.Register()’, and create a listener using ‘net.Listen()’ on ‘localhost:12345’. Inside a for loop, we accept incoming connections, create a JSON-RPC codec using the ‘github.com/gorilla/rpc/v2/json2’ package, and serve the individual request using the ‘ServeRequest()’ function on the ‘rpc’ default server instance. We close the connection after serving the request.
Note that this example demonstrates the server-side implementation of the RPC system using a custom codec. To fully test the functionality, you would need to create a separate client-side implementation to connect to this server and call the ‘Greet’ method, using the same custom codec.
To check more Go related articles. Pls click given below link:
https://www.techieindoor.com/category/leetcode/