rpc

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 9 Imported by: 0

README

RPC

Usage:

import "github.com/lalamove/gokit/rpc"

// basic usage
jr := rpc.NewJsonRPC(
    rpc.WithAppID("target-app"),
    rpc.WithHost("http://your-target-host/"),
    rpc.WithReport(func(res *http.Response, rt time.Duration) {}),
    rpc.WithLogger(log.Dummy()),
)

// advanced usage with soa & report
// it will use soa to resolve provider's addr first, if fail, fallback use the given host
jr := rpc.NewJsonRPC(
    rpc.WithAppID("target-app"),
    rpc.WithSOA(soa),
    rpc.WithHost("http://your-target-host/"),
    rpc.WithReport(reportFunc),
)

ep := jr.Endpoint("endpoint")

var req any
var res any
err := ep.Call("method", req, &res)

Documentation

Index

Constants

View Source
const VersionHeader = "x-hll-gray-version"

Variables

This section is empty.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	// contains filtered or unexported fields
}

func (*Endpoint) Call

func (ep *Endpoint) Call(method string, req interface{}, res interface{}) error

Call conducts a json-rpc call

type JsonRPC

type JsonRPC struct {
	Options
}

func NewJsonRPC

func NewJsonRPC(opts ...Option) *JsonRPC

NewJsonRPC creates a json-rpc client

func (*JsonRPC) Endpoint

func (jr *JsonRPC) Endpoint(endpoint string) *Endpoint

Endpoint prepares the endpoint

type Option

type Option func(*Options)

Option is for setting options

func WithAppID

func WithAppID(appID string) Option

WithAppID sets app id

func WithHeaders added in v0.6.0

func WithHeaders(h map[string]string) Option

WithHeaders sets headers

func WithHost

func WithHost(host string) Option

WithHost sets host

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets logger

func WithReport

func WithReport(report Reporter) Option

WithReport sets report func

func WithSOA

func WithSOA(s *soa.SOA) Option

WithSOA sets soa

func WithTimeout added in v0.6.1

func WithTimeout(timeout time.Duration) Option

WithTimeout sets timeout

func WithVersion added in v0.6.0

func WithVersion(v string) Option

WithVersion sets version

type Options

type Options struct {
	Logger  log.Logger
	Host    string
	AppID   string
	SOA     *soa.SOA
	Report  Reporter
	Headers map[string]string
	Version string
	Timeout time.Duration
}

Options is rpc options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions creates options with defaults

type Reporter

type Reporter func(res *http.Response, rt time.Duration)

type ReqWrapper

type ReqWrapper []interface{}

Jump to

Keyboard shortcuts

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