Coder Social home page Coder Social logo

vito-go / rpcplus Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 78 KB

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: BSD 3-Clause "New" or "Revised" License

Go 100.00%
go-rpc golang-rpc rpc rpc-api rpc-client rpc-framework rpc-library rpc-server

rpcplus's Introduction

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.

rpcplus's People

Contributors

vito-go avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.