evio

package
v0.0.0-...-18ecc23 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

evio

A fast event-loop networking framework that uses the erpc API layer. (From evio)

It makes direct epoll and kqueue syscalls rather than using the standard Go net package, and works in a similar manner as libuv and libevent.

Feature

  • Fast single-threaded or multithreaded event loop
  • Built-in load balancing options
  • eRPC API
  • Low memory usage
  • Fallback for non-epoll/kqueue operating systems by simulating events with the net package
  • SO_REUSEPORT socket option
  • Support TLS
  • (TODO) Support set connection deadline

Usage

import "github.com/henrylee2cn/erpc/v6/mixer/evio"

Test
package evio_test

import (
	"testing"
	"time"

	"github.com/henrylee2cn/erpc/v6"
	"github.com/henrylee2cn/erpc/v6/mixer/evio"
)

func Test(t *testing.T) {
	// server
	srv := evio.NewServer(1, erpc.PeerConfig{ListenPort: 9090})
	// use TLS
	srv.SetTLSConfig(erpc.GenerateTLSConfigForServer())
	srv.RouteCall(new(Home))
	go srv.ListenAndServe()
	time.Sleep(1e9)

	// client
	cli := evio.NewClient(erpc.PeerConfig{})
	// use TLS
	cli.SetTLSConfig(erpc.GenerateTLSConfigForClient())
	cli.RoutePush(new(Push))
	sess, stat := cli.Dial(":9090")
	if !stat.OK() {
		t.Fatal(stat)
	}
	var result interface{}
	stat = sess.Call("/home/test",
		map[string]string{
			"author": "henrylee2cn",
		},
		&result,
		erpc.WithAddMeta("peer_id", "110"),
	).Status()
	if !stat.OK() {
		t.Fatal(stat)
	}
	t.Logf("result:%v", result)
	time.Sleep(2e9)
}

type Home struct {
	erpc.CallCtx
}

func (h *Home) Test(arg *map[string]string) (map[string]interface{}, *erpc.Status) {
	h.Session().Push("/push/test", map[string]string{
		"your_id": string(h.PeekMeta("peer_id")),
	})
	return map[string]interface{}{
		"arg": *arg,
	}, nil
}

type Push struct {
	erpc.PushCtx
}

func (p *Push) Test(arg *map[string]string) *erpc.Status {
	erpc.Infof("receive push(%s):\narg: %#v\n", p.IP(), arg)
	return nil
}

test command:

go test -v -run=Test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(cfg erpc.PeerConfig, globalLeftPlugin ...erpc.Plugin) erpc.Peer

NewClient creates a evio client, equivalent to erpc.NewPeer.

Types

type Server

type Server struct {
	erpc.Peer
	// contains filtered or unexported fields
}

Server a evio server

func NewServer

func NewServer(loops int, cfg erpc.PeerConfig, globalLeftPlugin ...erpc.Plugin) *Server

NewServer creates a evio server.

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(protoFunc ...erpc.ProtoFunc) error

ListenAndServe turns on the listening service.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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