rpc

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rpc defines a request/response RPC protocol carried over Kafka.

It contains the message types, the request pool that correlates responses to pending requests, and the Client and Server interfaces. The transport implementations live in the client and server sub-packages.

Index

Constants

View Source
const (
	ErrCodeRpcMsgNoKey            = "RPC-001"
	ErrCodeRpcMsgNoRequestId      = "RPC-002"
	ErrCodeRpcRespNoRequestInPool = "RPC-003"
	ErrCodeRpcRespInvalidBody     = "RPC-004"
	ErrCodeRpcCallNoCb            = "RPC-005"
)

Variables

View Source
var (
	ErrRpcMsgNoKey = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeRpcMsgNoKey, "key empty").C(ctx).Err()
	}
	ErrRpcMsgNoRequestId = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeRpcMsgNoRequestId, "Request id empty").C(ctx).Err()
	}
	ErrRpcCallNoCb = func(ctx context.Context) error {
		return jet.NewAppErrBuilder(ErrCodeRpcCallNoCb, "callback id empty").C(ctx).Err()
	}
	ErrRpcRespNoRequestInPool = func(ctx context.Context, rqId, key string) error {
		return jet.NewAppErrBuilder(ErrCodeRpcRespNoRequestInPool, "no Request in pool").C(ctx).F(jet.KV{"rqId": rqId, "key": key}).Err()
	}
	ErrRpcRespInvalidBody = func(cause error, ctx context.Context, rqId, key string) error {
		return jet.NewAppErrBuilder(ErrCodeRpcRespInvalidBody, "no Request in pool").Wrap(cause).C(ctx).F(jet.KV{"rqId": rqId, "key": key}).Err()
	}
)

Functions

This section is empty.

Types

type Callback

type Callback func(ctx context.Context, msg *Message) error

type Client

type Client interface {
	// Call makes a rpcClient call
	Call(ctx context.Context, msg *Message, callback ResponseCallback) error
	// ResponseHandler must be setup as a subscriber of a response topic
	ResponseHandler(msg []byte) error
	// RegisterBodyTypeProvider allows providing a type to a response body to convert to
	// otherwise raw body is returned (map[string]interface{})
	RegisterBodyTypeProvider(MessageType, MessageBodyTypeProvider)
	// SetExpirationCallback sets up a callback which is called each time when a Request timeout expires
	SetExpirationCallback(callback Callback)
	// Start starts internal background processes
	Start(ctx context.Context)
	// Close closes all internal routines
	Close(ctx context.Context)
}

type Config

type Config struct {
	// CallTimeOut call timeout
	// after timeout expires TimeoutCallback is called
	CallTimeOut time.Duration
	// ClusterSupport additionally supports internal connection list with tracing messages keys
	// Thus, handler skips messages with keys which aren't presented in connection list
	// Note, it's client's responsibility to support connection list in sync
	ClusterSupport bool
}

type Message

type Message struct {
	Type             MessageType `json:"type"`
	RequestId        string      `json:"rqId"`
	Key              string      `json:"key"`
	ResponseRequired bool        `json:"respReq"`
	Body             interface{} `json:"body"`
}

type MessageBodyTypeProvider

type MessageBodyTypeProvider func() interface{}

MessageBodyTypeProvider should return an instance of the type to which reply body is cast

type MessageType

type MessageType uint

type RawMessage

type RawMessage struct {
	Type             MessageType            `json:"type"`
	RequestId        string                 `json:"rqId"`
	Key              string                 `json:"key"`
	ResponseRequired bool                   `json:"respReq"`
	Body             map[string]interface{} `json:"body"`
}

type Request

type Request struct {
	At       time.Time
	Callback ResponseCallback
	Msg      *Message
	Ctx      context.Context
}

type RequestPool

type RequestPool struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

RequestPool manages incoming requests with ttl and execute handler as a Request expires

func NewRequestPool

func NewRequestPool(logger jet.CLoggerFunc, ttl time.Duration) *RequestPool

func (*RequestPool) Len

func (c *RequestPool) Len() int

func (*RequestPool) Queue

func (c *RequestPool) Queue(ctx context.Context, rq *Request)

func (*RequestPool) Remove

func (c *RequestPool) Remove(rqId string)

func (*RequestPool) SetExpirationCallback

func (c *RequestPool) SetExpirationCallback(cb Callback)

func (*RequestPool) Start

func (c *RequestPool) Start(ctx context.Context)

func (*RequestPool) Stop

func (c *RequestPool) Stop()

func (*RequestPool) TryDequeue

func (c *RequestPool) TryDequeue(rqId string) *Request

type ResponseCallback

type ResponseCallback func(ctx context.Context, rqMsg, rsMsg *Message) error

type Server

type Server interface {
	// RequestHandler must be setup as a subscriber of a Request topic
	RequestHandler(msg []byte) error
	// RegisterType registers message type and allows setting up a callback method for the message type
	// if a received message doesn't have a callback specified, message is ignored
	// MessageBodyTypeProvider allows providing a type to a Request body to convert to
	// otherwise raw body is returned (map[string]interface{})
	RegisterType(MessageType, Callback, MessageBodyTypeProvider)
	// Response sends a response to a client topic
	// if no Request found, error is returned
	Response(ctx context.Context, msg *Message) error
	// SetExpirationCallback sets up a callback which is called each time when a Request timeout expires
	SetExpirationCallback(callback Callback)
	// Start starts internal background processes
	Start(ctx context.Context)
	// Close closes all internal routines
	Close(ctx context.Context)
}

Directories

Path Synopsis
Package client implements rpc.Client: it sends RPC calls over Kafka and correlates incoming responses back to their pending requests.
Package client implements rpc.Client: it sends RPC calls over Kafka and correlates incoming responses back to their pending requests.
Package server implements rpc.Server: it handles incoming RPC requests from Kafka and sends responses back to the calling client.
Package server implements rpc.Server: it handles incoming RPC requests from Kafka and sends responses back to the calling client.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL