Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/rpcPublic

Package rpc implements a remote procedure call over TCP, UNIX, HTTP and WS. Up to 4x faster than net/rpc.

License

NotificationsYou must be signed in to change notification settings

hslam/rpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PkgGoDevBuild StatuscodecovGo Report CardLICENSE

Package rpc implements a remote procedure call over TCP, UNIX, HTTP and WS. The rpc improves throughput and reduces latency. Up to 4 times faster than net/rpc.

Feature

  • More throughput and less latency.
  • Netpoll epoll/kqueue/net
  • Network tcp/unix/http/ws
  • Codec json/code/pb
  • Multiplexing/Pipelining
  • Auto batching
  • Call/Go/RoundTrip/Ping/Stream/CallWithContext
  • Conn/Transport/Client
  • TLS

Comparison to other packages

Packagenetrpcjsonrpcrpcgrpcrpcx
Epoll/KqueueNoNoYesNoNo
MultiplexingYesYesYesYesYes
PipeliningNoNoYesNoNo
Auto BatchingNoNoYesNoNo
TransportNoNoYesNoNo
Low Concurrency

rpcrpc

High Concurrency

rpcrpc

Get started

Install

go get github.com/hslam/rpc

Import

import "github.com/hslam/rpc"

Usage

arith.proto

syntax = "proto3";package service;message ArithRequest {    int32 a = 1;    int32 b = 2;}message ArithResponse {    int32 pro = 1;}

GoGo Protobuf

protoc ./arith.proto --gogofaster_out=./

arith.go

package servicetypeArithstruct{}func (a*Arith)Multiply(req*ArithRequest,res*ArithResponse)error {res.Pro=req.A*req.Breturnnil}

server.go

package mainimport ("github.com/hslam/rpc""github.com/hslam/rpc/examples/codec/pb/service")funcmain() {rpc.Register(new(service.Arith))rpc.Listen("tcp",":9999","pb")}

conn.go

package mainimport ("fmt""github.com/hslam/rpc""github.com/hslam/rpc/examples/codec/pb/service")funcmain() {conn,err:=rpc.Dial("tcp",":9999","pb")iferr!=nil {panic(err)}deferconn.Close()req:=&service.ArithRequest{A:9,B:2}varres service.ArithResponseiferr=conn.Call("Arith.Multiply",req,&res);err!=nil {panic(err)}fmt.Printf("%d * %d = %d\n",req.A,req.B,res.Pro)}

transport.go

package mainimport ("fmt""github.com/hslam/rpc""github.com/hslam/rpc/examples/codec/pb/service")funcmain() {trans:=&rpc.Transport{MaxConnsPerHost:1,MaxIdleConnsPerHost:1,Options:&rpc.Options{Network:"tcp",Codec:"pb"},}defertrans.Close()req:=&service.ArithRequest{A:9,B:2}varres service.ArithResponseiferr:=trans.Call(":9999","Arith.Multiply",req,&res);err!=nil {panic(err)}fmt.Printf("%d * %d = %d\n",req.A,req.B,res.Pro)}

client.go

package mainimport ("fmt""github.com/hslam/rpc""github.com/hslam/rpc/examples/codec/pb/service")funcmain() {opts:=&rpc.Options{Network:"tcp",Codec:"pb"}client:=rpc.NewClient(opts,":9997",":9998",":9999")client.Scheduling=rpc.LeastTimeSchedulingdeferclient.Close()req:=&service.ArithRequest{A:9,B:2}varres service.ArithResponseiferr:=client.Call("Arith.Multiply",req,&res);err!=nil {panic(err)}fmt.Printf("%d * %d = %d\n",req.A,req.B,res.Pro)}

context.go

package mainimport ("context""fmt""github.com/hslam/rpc""github.com/hslam/rpc/examples/codec/pb/service""time")funcmain() {conn,err:=rpc.Dial("tcp",":9999","pb")iferr!=nil {panic(err)}deferconn.Close()req:=&service.ArithRequest{A:9,B:2}varres service.ArithResponseemptyCtx:=context.Background()valueCtx:=context.WithValue(emptyCtx,rpc.BufferContextKey,make([]byte,64))ctx,cancel:=context.WithTimeout(valueCtx,time.Minute)defercancel()err=conn.CallWithContext(ctx,"Arith.Multiply",req,&res)iferr!=nil {panic(err)}fmt.Printf("%d * %d = %d\n",req.A,req.B,res.Pro)}

Output

9 * 2 = 18

License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

rpc was written by Meng Huang.

About

Package rpc implements a remote procedure call over TCP, UNIX, HTTP and WS. Up to 4x faster than net/rpc.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp