Skip to content

rpcplus is a enchanced library that builds upon the Go language's standard rpc package, offering a suite of enhancements for an improved RPC experience.

License

Notifications You must be signed in to change notification settings

vito-go/rpcplus

Repository files navigation

rpcplus

rpcplus is an enhanced library that builds upon the Go language's standard rpc package, offering a suite of enhancements for an improved RPC experience.

Key Features

  • Support for direct function registration on the RPC server for simplified service provisioning.
  • Ability to register anonymous functions, offering flexibility in programming.
  • Ability to register all suitable methods associated with a receiver, typically a struct
  • Support for functions with multiple input parameters
  • Support for passing a context.Context parameter, enabling handling of timeouts, cancellations, and other context-related operations.
  • Support for Go-style return value patterns, allowing functions to return (T, error) or just error.
  • Reserves jsonrpc support, facilitating cross-language communication

These key enhancements contribute to a versatile and powerful RPC implementation.

Usage

  • Server

      package  main
      import (
          "context"
          "github.com/vito-go/rpcplus"
          "log"
          "net"
      )
      // look at the example/example.go Stu and Add 
      func main()  {
          log.SetFlags(log.Lshortfile | log.LstdFlags)
          s := rpcplus.NewServer()
          err := s.RegisterRecv(&Stu{})
          if err != nil {
              panic(err)
          }
          err = s.RegisterFunc("Add", Add)
          if err != nil {
              panic(err)
          }
          err = s.RegisterFunc("Anonymous", func(ctx context.Context, x int, y int) (int64, error) {
              return int64(x * y), nil
          })
          if err != nil {
              panic(err)
          }
          lis, err := net.Listen("tcp", ":8081")
          if err != nil {
              panic(err)
          }
          log.Println("rpcplus: Starting server on port 8081")
          s.Serve(lis)
      }
  • Client

    package main
    import (
      "context"
      "fmt"
      "github.com/vito-go/rpcplus"
      "log"
      "net"
    )
    // look at the example/example.go UserInfo
    func main()  {
        dialer, err := net.Dial("tcp", "127.0.0.1:8081")
        if err != nil {
            panic(err)
        }
        cli := rpcplus.NewClient(dialer)
        ctx := context.Background()
        var result UserInfo
        err = cli.Call(ctx, "Stu.GetUserInfoByUserId", &result, 181)
        if err != nil {
            panic(err)
        }
        // OutPut UserInfo: {Name:Jack Age:31 ClassId:1}
        log.Println(fmt.Sprintf("UserInfo: %+v", result))
    }    

Function

RegisterFunc that satisfy the following conditions:

  • Have one or more arguments, with the first argument being of type context.Context

  • Return types: a single error or a pair with the second element an error

  • Function Example

  func add(ctx context.Context, x int,y int) (int, error)

  func GetAgeByName(ctx context.Context, name string) (int, error)

  func GetAgeByClassIdAndName(ctx context.Context,classId int,name string) (int, error)

  type UserInfo struct{
      Name string
      Age int
      ClassId int
  }
	  
  func GetUserInfoByUserId(ctx context.Context,userId int) (*UserInfo, error)

  func UpdateUserInfo(ctx context.Context, u *UserInfo) (int64, error)
  • Service Function List (for test)

  • from debugHTTP

    ServiceMethodTypeCalls
    Arith.Add func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    Arith.Div func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    Arith.Error func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    Arith.Mul func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    Arith.Scan func(context.Context, string) (*rpcplus.Reply, error) 0
    Arith.SleepMilli func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    Arith.String func(context.Context, *rpcplus.Args) (*string, error) 0
    Embed.Exported func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.Add func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.Div func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.Error func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.Mul func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.Scan func(context.Context, string) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.SleepMilli func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    net.rpcplus.Arith.String func(context.Context, *rpcplus.Args) (*string, error) 0
    newServer.Arith.Add func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    newServer.Arith.Div func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    newServer.Arith.Error func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    newServer.Arith.Mul func(context.Context, *rpcplus.Args) (*rpcplus.Reply, error) 0
    newServer.Arith.Scan func(context.Context, string) (*rpcplus.Reply, error) 0
    newServer.Arith.SleepMilli func(context.Context, rpcplus.Args) (*rpcplus.Reply, error) 0
    newServer.Arith.String func(context.Context, *rpcplus.Args) (*string, error) 0

Testing

All test cases have been executed and passed successfully, ensuring the quality and reliability of the codebase. Contributors are encouraged to run tests before making submissions to maintain project stability.

About

rpcplus is a enchanced library that builds upon the Go language's standard rpc package, offering a suite of enhancements for an improved RPC experience.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages