Skip to content

rpc.rpcprocessor #

RPCProcessor

Overview

rpcprocessor is a high-performance, scalable module designed to facilitate Remote Procedure Calls (RPCs) in distributed systems. Built with the V programming language, this module seamlessly integrates WebSocket communication and Redis queues to efficiently handle and route RPCs. Ideal for microservices architectures and asynchronous task processing, rpcprocessor ensures reliable message delivery and processing with minimal latency.

Features

  • WebSocket Integration: Leverages WebSockets for real-time, bidirectional communication.
  • Redis Queue Management: Utilizes Redis queues to maintain and manage RPC requests and responses, ensuring organized and efficient handling.
  • Scalable Architecture: Designed to scale with your application, handling increased loads with ease.
  • Simple API: Offers a straightforward and intuitive API, making it easy to integrate and use within your projects.
  • Asynchronous Processing: Supports asynchronous processing, enabling non-blocking operations and improved performance.

Requirements

  • V Programming Language
  • Redis Server

Installation

To include rpcprocessor in your project, add the following import statement:

import rpcprocessor

Ensure you have a running Redis instance for rpcprocessor to connect to. Configuration details for the Redis connection can be specified within the module's initialization parameters.

Usage

Initializing the Server

To start using rpcprocessor, initialize the server with the necessary configuration:

import rpcprocessor

fn main() {
    config := rpcprocessor.Config{
        redis_address: 'localhost:6379',
        ws_port: 8080,
    }
    server := rpcprocessor.new_server(config)
    server.start()
}

Handling RPCs

Define your RPC handlers and register them with the server. rpcprocessor will route incoming RPCs to the appropriate handler based on the configuration.

server.register_handler('my_rpc', my_rpc_handler)

fn my_rpc_handler(request rpcprocessor.Request) rpcprocessor.Response {
    // Process the request
    return rpcprocessor.Response{
        result: 'Processed result',
    }
}

Fetching Results

Once an RPC is processed, rpcprocessor fetches the result from the corresponding Redis queue and returns it to the caller.

fn get_subscriber #

fn get_subscriber() eventbus.Subscriber[string]

fn new #

fn new(config ProcessorConfig) !RPCProcessor

fn new_api #

fn new_api() API

fn new_client #

fn new_client() !Client

fn new_handler #

fn new_handler() !RPCProcessorHandler

interface IHandler #

interface IHandler {
	name          string   // name of handler
	methods       []string // list of supported methods
	description   string
	threads       int
	typ           string
	last_activity time.Time
	activity      []Activity
	rpc_no        int
	average_time  time.Duration
	success_rate  f64
mut:
	queue redisclient.RedisQueue // queue of handler
}

Handler is the structure responsible of handling a list of supported RPCs over a redis queue

fn (Client) get_handlers #

fn (c Client) get_handlers() ![]Handler

fn (Client) get_handler #

fn (mut c Client) get_handler(name string) !Handler

fn (Client) get_rpc #

fn (c Client) get_rpc(id string) !Activity

fn (Client) get_latest_activity #

fn (c Client) get_latest_activity() ![]Activity

fn (RPCProcessor) register_handler #

fn (mut p RPCProcessor) register_handler(handler IHandler)

fn (RPCProcessor) run_ui #

fn (mut p RPCProcessor) run_ui() !

fn (RPCProcessorUI) index #

fn (mut ui RPCProcessorUI) index() vweb.Result

fn (RPCProcessorUI) handler_page #

fn (mut ui RPCProcessorUI) handler_page(name string) vweb.Result

pub fn (mut ui RPCProcessorUI) handler() vweb.Result { return ui.html($tmpl('./templates/handler.html')) }

fn (RPCProcessorUI) rpc #

fn (mut ui RPCProcessorUI) rpc(id string) vweb.Result

fn (RPCProcessorUI) handler_methods #

fn (mut ui RPCProcessorUI) handler_methods(name string) vweb.Result

struct Duration #

struct Duration {
pub:
	hours int
}

struct EventMetadata #

struct EventMetadata {
pub:
	message string
}

struct Handler #

struct Handler {
pub:
	name          string   // name of handler
	methods       []string // list of supported methods
	description   string
	threads       int
	typ           string
	last_activity time.Time
	activity      []Activity
	rpc_no        int
	average_time  time.Duration
	success_rate  f64
mut:
	queue redisclient.RedisQueue // queue of handler
}

Handler is the structure responsible of handling a list of supported RPCs over a redis queue

struct ProcessorConfig #

@[params]
struct ProcessorConfig {
	redis_client redisclient.Redis
}

struct RPCProcessorHandler #

struct RPCProcessorHandler {
	Handler
mut:
	redis_client redisclient.Redis
	queue        redisclient.RedisQueue
}

fn (RPCProcessorHandler) run #

fn (mut h RPCProcessorHandler) run() !

this rpc_handler listens to the echo method's redis queue and handles echo rpc