Categorygithub.com/xizhibei/go-reverse-rpc
modulepackage
0.0.0-20240624093908-aac6d55a1f96
Repository: https://github.com/xizhibei/go-reverse-rpc.git
Documentation: pkg.go.dev

# README

Go reverse RPC

A remote procedure call (RPC) framework designed for connecting to devices remotely. It enables the "server" to call functions provided by the "client".

Build Status Go Report Card GoDoc

Features

  • Supports multiple communication protocols - currently implemented MQTT 3.1/3.11
  • Allows encoding data in different formats - currently supports JSON and Protobuf
  • Provides monitoring metrics for system insights
  • Implements error handling mechanisms for reliability

TODO

  • Open telemetry support
  • MQTT v5 protocol support
  • WebSocket protocol support
  • AMQP protocol support

Installation

go get github.com/xizhibei/go-reverse-rpc@latest

Usage

Server create

import (
    "github.com/xizhibei/go-reverse-rpc/mqttpb"
    "github.com/xizhibei/go-reverse-rpc/mqttadapter"
)

mqttClient, err := mqttadapter.New("tcp://localhost", "client-id-123456-server")
if err != nil {
    panic(err)
}

server := mqttpb.NewServer(
    mqttClient,
    "example-prefix",
    "device-123456",
)

Client create

import (
    "github.com/xizhibei/go-reverse-rpc/mqttpb"
    "github.com/xizhibei/go-reverse-rpc/mqttadapter"
)

mqttClient, err := mqttadapter.New("tcp://localhost", "client-id-123456-client")
if err != nil {
    panic(err)
}

client := mqttpb.New(
    mqttClient,
    "example-prefix",
    mqttpb.ContentEncoding_GZIP,
)

Register handler on server side

import (
    rrpc "github.com/xizhibei/go-reverse-rpc"
)

server.Register("example-method", &rrpc.Handler{
    Method: func(c rrpc.Context) {
        var req Req
        err := c.Bind(&req)
        if err != nil {
            c.ReplyError(rrpc.RPCStatusClientError, err)
            return
        }

        // your business logic ...

        c.ReplyOK(req)
    },
    Timeout: 5 * time.Second,
})

Call on client side

var res Req
err := client.Call(context.Background(), "device-123456", "example-method", &reqParams, &res)

Server create options

rrpc.WithServerName(name string) // Used to set the name of the server. For monitoring purposes, metrics labels will use this name.
rrpc.WithLogResponse(logResponse bool) // Used to enable or disable logging of response.
rrpc.WithLimiter(d time.Duration, count int) // Used to set the limiter duration and count for the server.
rrpc.WithLimiterReject() // Used to allow the server to reject requests when the limiter is full. This is default behavior.
rrpc.WithLimiterWait() // Used to allow the server to wait for available resources instead of rejecting requests when the limiter is full.
rrpc.WithWorkerNum(count int) // Used to set the number of workers for the server.

License

Go reverse RPC released under MIT license, refer LICENSE file.

# Packages

No description provided by the author
Package mock_reverserpc is a generated GoMock package.
Package mqtt provides options for configuring MQTT client.
No description provided by the author
No description provided by the author

# Functions

NewRequestContext creates a new instance of RequestContext with the given context and ContextInstance.
NewServer creates a new instance of the Server struct with the provided options.
WithLimiter is a function that returns a ServerOption which sets the limiter duration and count for the server.
WithLimiterReject returns a ServerOption that sets the limiterReject field of the serverOptions struct to true.
WithLimiterWait returns a ServerOption that sets the limiterReject field of the serverOptions struct to false.
WithLogResponse is a function that returns a ServerOption to enable or disable logging of response.
WithServerName is a function that returns a ServerOption to set the name of the server.
WithWorkerNum is a function that returns a ServerOption which sets the number of workers for the server.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

ErrNoReply is an error indicating an empty reply.
ErrTimeout is an error indicating a timeout occurred.
ErrTooFraquently is an error indicating that the request was made too frequently.

# Structs

AfterResponseEvent represents an event that is emitted after a response is sent.
Handler represents a reverse RPC handler.
ID represents an identifier with a numeric value and a string value.
RequestContext is the context implement for reverse RPC.
Response represents a response message.
Server represents a reverse RPC server.

# Interfaces

ChildContext represents the base interface for reverse RPC contexts.
Context represents the context for reverse RPC.

# Type aliases

OnAfterResponseCallback is a function type that represents a callback function to be executed after a response is sent.
ServerOption is a functional option for configuring the server.