drpc

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: MIT Imports: 10 Imported by: 2

README

DRPC

DRPC is the Go Protobuf RPC Framework

Features

  • Go client and server files compiled from Protobuf with types and services
  • Clients in pure Go and Go for gopherjs
  • Gopherjs client file without fmt package. It has a small out js file < 700KB (web)
  • Multi listening server on TCP and WS protocols at the same time (single server-multi protocols)
  • TCP and Websocket connections allow to recieve Server Notification not like RPC response

Usage

Install protoc out plugins:

go get -u gitlab.com/so_literate/drpc/protoc-gen-godrpc
go get -u gitlab.com/so_literate/drpc/protoc-gen-godrpcjs

And gopherjs compiler for WS web client

go get -u github.com/gopherjs/gopherjs

Steps

  1. Create Protobuf file with types and service
  2. Compile Go server and Go client files with 'protoc --godrpc_out=plugins=drpc'
  3. Compile gopherjs Go client in different dir with 'protoc --godrpcjs_out=plugins=drpc'
  4. Write server code with handlers from xxx.pg.go file
  5. Write client code for web in Go gopherjs with methods from xxx.pb.gopherjs.go
  6. Compile client JS code using 'gopherjs build'
  7. Create HTML with compiled JS file

Enjoy your protobuf Go WEB RPC 👍

Look simple usage simpleusage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConnectOfRequest

func GetConnectOfRequest(ctx context.Context) drpcnet.Conn

GetConnectOfRequest returns connect of the request

func GetMethodNameOfRequest

func GetMethodNameOfRequest(ctx context.Context) string

GetMethodNameOfRequest returns name of the request Use it in Middlewares or Handlers only

func HandlerContextWithValue

func HandlerContextWithValue(requestCtx context.Context, key, value interface{})

HandlerContextWithValue sets value to ctx of this connect takes context of the handler and context arguments. This context will be available in the next request Do not use it more then 3 times per request, otherwise you get deadlock ╮( ̄ω ̄;)╭ Or use it with go to avoid blocking

func SetConnectCtxChannel

func SetConnectCtxChannel(ctx context.Context, ccc ConnectCtxChannel) context.Context

SetConnectCtxChannel sets context channel of the connect to context It is not for users

func SetConnectOfRequest

func SetConnectOfRequest(ctx context.Context, conn drpcnet.Conn) context.Context

SetConnectOfRequest sets connect to connect to send it in each request It is not for users

Types

type ConnectCtxChannel

type ConnectCtxChannel chan *ConnectCtxData

ConnectCtxChannel is a context channel of the connection

func GetConnectCtxChannel

func GetConnectCtxChannel(ctx context.Context) ConnectCtxChannel

GetConnectCtxChannel returns context channel of the connect from context

type ConnectCtxData

type ConnectCtxData struct {
	Key   interface{}
	Value interface{}
}

ConnectCtxData is a type to receive context data to set in the Request connect

type FailedNotif

type FailedNotif struct {
	Conn drpcnet.Conn
	Err  error
}

FailedNotif this type contains message sending error and send connection

type Middleware

type Middleware func(context.Context) (context.Context, error)

Middleware you may added it to drpc.Service return error if you want to abort the RPC call Context will contain client connection, use GetConnectOfRequest()

type Server

type Server struct {

	// OnAcceptNewConnect is callback that is called when listener get new connect
	// return false if you want to close connect
	// Default: accepts (true)
	OnAcceptNewConnect func(context.Context, drpcnet.Conn) bool

	// OnErrorAccept is callback that is called when getting connect ends with error
	// returns false if you want to stop listener
	// Default: listener keep work (true)
	OnErrorAccept func(context.Context, error) bool

	// OnErrorRead is callback that is called when reader of the connect returned error
	// Usually occurs EOF Error this is not important information
	// returns true if you want to close this connect
	// Default: connect will be closed (true)
	OnErrorRead func(context.Context, drpcnet.Conn, error) bool

	// OnErrorWrite is callback that is called when Write operation ends with error
	// return true if you want to Close connect
	// Default: connect will be closed (true)
	OnErrorWrite func(context.Context, drpcnet.Conn, error) bool

	// OnCloseConnect is callback that is called when server or client closed connection
	// takes context of the request and connection
	OnCloseConnect func(context.Context, drpcnet.Conn)
	// contains filtered or unexported fields
}

Server side of the drpc Contains any listeners

func NewServer

func NewServer(ctx context.Context) *Server

NewServer returns new drpc server takes context that will be sent to handlers

func (*Server) AddListeners

func (s *Server) AddListeners(l ...drpcnet.Listener)

AddListeners adds new listeners to Server

func (*Server) CloseAllListeners

func (s *Server) CloseAllListeners()

CloseAllListeners closes all listeners

func (*Server) CloseConnection

func (s *Server) CloseConnection(ctx context.Context) error

CloseConnection takes context of the request closes connect and returns error of the close

func (*Server) RegisterService

func (s *Server) RegisterService(service *Service, srvType interface{})

RegisterService register service with handlers

func (*Server) SendMultiNotif

func (s *Server) SendMultiNotif(conns []drpcnet.Conn, notifName string, out proto.Message) (
	[]FailedNotif, error)

SendMultiNotif sends notification to several connection returns list of the failed and error

func (*Server) SendNotif

func (s *Server) SendNotif(ctx context.Context, notifName string, out proto.Message) error

SendNotif sends notification takes context of the request with connection

func (*Server) Serve

func (s *Server) Serve() error

Serve runs listeners to accept clients

type Service

type Service struct {
	Name     string
	Handlers *router.Router
	Notifs   *router.Router
	CtxKey   interface{}
}

Service is a service with handlers

Directories

Path Synopsis
net
generator
The code generator for the plugin for the Google protocol buffer compiler.
The code generator for the plugin for the Google protocol buffer compiler.
types
js/types
Package types is a generated protocol buffer package.
Package types is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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