thriftudp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2019 License: MIT Imports: 11 Imported by: 0

README

thriftudp

Thrift is one of the best IDL language for the UDP protocol, for its grammar support oneway keyword. But the official package only support TCP transport & server framework.

This package's purpose is to extend the thrift with UDP support. It provides the following features:

  • udp transport
  • udp server

For the project github.com/jaegertracing/jaeger has a very beautiful implemention in an OLD thrift version. This project just use its transport implemention with tiny modification for the NEW version support.

Quick Start

** IDL **

namespace go echo

struct Request {
    1: string message;
}

service Echo {
    oneway void Ping(1: Request request);
}

** Server **


package main

import (
	"context"
	"log"

	"github.com/x-mod/routine"
	"github.com/x-mod/thriftudp"
	"github.com/x-mod/thriftudp/example/thrift/echo"
)

type echoImpl struct{}

func (srv *echoImpl) Ping(ctx context.Context, request *echo.Request) (err error) {
	log.Println("echo Ping Request Message: ", request.Message)
	return nil
}

func main() {
	routine.Main(context.TODO(), routine.ExecutorFunc(func(ctx context.Context) error {
		srv := thriftudp.NewServer(
			thriftudp.ListenAddress("127.0.0.1:8888"),
			thriftudp.Processor(
				echo.NewEchoProcessor(&echoImpl{}),
				2),
		)
		if err := srv.Open(ctx); err != nil {
			return err
		}
		log.Println("serving at 127.0.0.1:8888 ...")
		return srv.Serv(ctx)
	}))
}

** Client **

package main

import (
	"context"
	"log"
	"os"
	"strings"

	"github.com/apache/thrift/lib/go/thrift"
	"github.com/x-mod/thriftudp/example/thrift/echo"
	"github.com/x-mod/thriftudp/transport"
)

func main() {
	tr, err := transport.NewTUDPClientTransport("127.0.0.1:8888", "")
	if err != nil {
		log.Println(err)
		return
	}
	client := echo.NewEchoClientFactory(tr, thrift.NewTCompactProtocolFactory())

	req := echo.NewRequest()
	req.Message = strings.Join(os.Args[1:], " ")

	if err := client.Ping(context.TODO(), req); err != nil {
		log.Println(err)
		return
	}
}

More details, Please check the example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ReadBuf

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

ReadBuf is a structure that holds the bytes to read into as well as the number of bytes that was read. The slice is typically pre-allocated to the max packet size and the buffers themselves are polled to avoid memory allocations for every new inbound message.

func (*ReadBuf) GetBytes

func (r *ReadBuf) GetBytes() []byte

GetBytes returns the contents of the ReadBuf as bytes

func (*ReadBuf) Read

func (r *ReadBuf) Read(p []byte) (int, error)

type Server

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

Server struct

func NewServer

func NewServer(opts ...ServerOpt) *Server

NewServer new udp server

func (*Server) Close

func (srv *Server) Close()

Close server

func (*Server) Open

func (srv *Server) Open(ctx context.Context) error

Open server

func (*Server) Serv

func (srv *Server) Serv(ctx context.Context) error

Serv server

type ServerOpt

type ServerOpt func(*serverConfig)

ServerOpt option for server

func ListenAddress

func ListenAddress(addr string) ServerOpt

ListenAddress ServerOpt | Required

func MaxPacketSize

func MaxPacketSize(sz int) ServerOpt

MaxPacketSize ServerOpt | Optional

func MaxQueueSize

func MaxQueueSize(sz int) ServerOpt

MaxQueueSize ServerOpt | Optional

func MetricsFactory

func MetricsFactory(factory metrics.Factory) ServerOpt

MetricsFactory ServerOpt | Optional

func Processor

func Processor(exec thrift.TProcessor, concurrent int) ServerOpt

Processor ServerOpt | Required

func ProtocolFactory

func ProtocolFactory(in, out thrift.TProtocolFactory) ServerOpt

ProtocolFactory ServerOpt | Optional

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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