Teleport is a versatile, high-performance and flexible socket framework.
It can be used for peer-peer, rpc, gateway, micro services, push services, game services and so on.
go get -u -f github.com/henrylee2cn/teleport
import tp "github.com/henrylee2cn/teleport/v6"
- Use peer to provide the same API package for the server and client
- Provide multi-layout abstractions such as:
- peer
- session/socket
- router
- handle/context
- message
- protocol
- codec
- transfer filter
- plugin
- Support reboot and shutdown gracefully
- HTTP-compatible message format:
- Composed of two parts, the
Header
and theBody
Header
contains metadata in the same format as HTTP headerBody
supports for custom codec of Content Type-Like, already implemented:- Protobuf
- Thrift
- JSON
- XML
- Form
- Plain
- Support push, call-reply and more message types
- Composed of two parts, the
- Support custom message protocol, and provide some common implementations:
rawproto
- Default high performance binary protocoljsonproto
- JSON message protocolpbproto
- Ptotobuf message protocolthriftproto
- Thrift message protocolhttproto
- HTTP message protocol
- Support for underlying communication optimization
- Use I/O multiplexing technology
- Support setting the size of socket I/O buffer
- Support setting the size of the reading message (if exceed disconnect it)
- Support controling the connection file descriptor
- Support a variety of network types:
tcp
tcp4
tcp6
unix
unixpacket
quic
- other
- websocket
- evio
- Provide a rich plug-in point, and already implemented:
- auth
- binder
- heartbeat
- ignorecase(service method)
- overloader
- proxy(for unknown service method)
- secure
- Powerful and flexible logging system:
- Detailed log information, support print input and output details
- Support setting slow operation alarm threshold
- Support for custom implementation log component
- Client session support automatically redials after disconnection
Self Test
-
A server and a client process, running on the same machine
-
CPU: Intel Xeon E312xx (Sandy Bridge) 16 cores 2.53GHz
-
Memory: 16G
-
OS: Linux 2.6.32-696.16.1.el6.centos.plus.x86_64, CentOS 6.4
-
Go: 1.9.2
-
Message size: 581 bytes
-
Message codec: protobuf
-
Sent total 1000000 messages
-
teleport
client concurrency | mean(ms) | median(ms) | max(ms) | min(ms) | throughput(TPS) |
---|---|---|---|---|---|
100 | 1 | 0 | 16 | 0 | 75505 |
500 | 9 | 11 | 97 | 0 | 52192 |
1000 | 19 | 24 | 187 | 0 | 50040 |
2000 | 39 | 54 | 409 | 0 | 42551 |
5000 | 96 | 128 | 1148 | 0 | 46367 |
- teleport/socket
client concurrency | mean(ms) | median(ms) | max(ms) | min(ms) | throughput(TPS) |
---|---|---|---|---|---|
100 | 0 | 0 | 14 | 0 | 225682 |
500 | 2 | 1 | 24 | 0 | 212630 |
1000 | 4 | 3 | 51 | 0 | 180733 |
2000 | 8 | 6 | 64 | 0 | 183351 |
5000 | 21 | 18 | 651 | 0 | 133886 |
Comparison Test
Environment | Throughputs | Mean Latency | P99 Latency |
---|---|---|---|
- Profile torch of teleport/socket
- Heap torch of teleport/socket
package main
import (
"fmt"
"time"
tp "github.com/henrylee2cn/teleport/v6"
)
func main() {
defer tp.FlushLogger()
// graceful
go tp.GraceSignal()
// server peer
srv := tp.NewPeer(tp.PeerConfig{
CountTime: true,
ListenPort: 9090,
PrintDetail: true,
})
// srv.SetTLSConfig(tp.GenerateTLSConfigForServer())
// router
srv.RouteCall(new(Math))
// broadcast per 5s
go func() {
for {
time.Sleep(time.Second * 5)
srv.RangeSession(func(sess tp.Session) bool {
sess.Push(
"/push/status",
fmt.Sprintf("this is a broadcast, server time: %v", time.Now()),
)
return true
})
}
}()
// listen and serve
srv.ListenAndServe()
}
// Math handler
type Math struct {
tp.CallCtx
}
// Add handles addition request
func (m *Math) Add(arg *[]int) (int, *tp.Status) {
// test meta
tp.Infof("author: %s", m.PeekMeta("author"))
// add
var r int
for _, a := range *arg {
r += a
}
// response
return r, nil
}
package main
import (
"time"
tp "github.com/henrylee2cn/teleport/v6"
)
func main() {
defer tp.SetLoggerLevel("ERROR")()
cli := tp.NewPeer(tp.PeerConfig{})
defer cli.Close()
// cli.SetTLSConfig(&tls.Config{InsecureSkipVerify: true})
cli.RoutePush(new(Push))
sess, stat := cli.Dial(":9090")
if !stat.OK() {
tp.Fatalf("%v", stat)
}
var result int
stat = sess.Call("/math/add",
[]int{1, 2, 3, 4, 5},
&result,
tp.WithAddMeta("author", "henrylee2cn"),
).Status()
if !stat.OK() {
tp.Fatalf("%v", stat)
}
tp.Printf("result: %d", result)
tp.Printf("Wait 10 seconds to receive the push...")
time.Sleep(time.Second * 10)
}
// Push push handler
type Push struct {
tp.PushCtx
}
// Push handles '/push/status' message
func (p *Push) Status(arg *string) *tp.Status {
tp.Printf("%s", *arg)
return nil
}
// Start a server
var peer1 = tp.NewPeer(tp.PeerConfig{
ListenPort: 9090, // for server role
})
peer1.Listen()
...
// Start a client
var peer2 = tp.NewPeer(tp.PeerConfig{})
var sess, err = peer2.Dial("127.0.0.1:8080")
type Aaa struct {
tp.CallCtx
}
func (x *Aaa) XxZz(arg *<T>) (<T>, *tp.Status) {
...
return r, nil
}
- register it to root router:
// register the call route
// HTTP mapping: /aaa/xx_zz
// RPC mapping: Aaa.XxZz
peer.RouteCall(new(Aaa))
// or register the call route
// HTTP mapping: /xx_zz
// RPC mapping: XxZz
peer.RouteCallFunc((*Aaa).XxZz)
-
The default mapping(HTTPServiceMethodMapper) of struct(func) name to service methods:
AaBb
->/aa_bb
ABcXYz
->/abc_xyz
Aa__Bb
->/aa_bb
aa__bb
->/aa_bb
ABC__XYZ
->/abc_xyz
Aa_Bb
->/aa/bb
aa_bb
->/aa/bb
ABC_XYZ
->/abc/xyz
tp.SetServiceMethodMapper(tp.HTTPServiceMethodMapper)
-
The mapping(RPCServiceMethodMapper) of struct(func) name to service methods:
AaBb
->AaBb
ABcXYz
->ABcXYz
Aa__Bb
->Aa_Bb
aa__bb
->aa_bb
ABC__XYZ
->ABC_XYZ
Aa_Bb
->Aa.Bb
aa_bb
->aa.bb
ABC_XYZ
->ABC.XYZ
tp.SetServiceMethodMapper(tp.RPCServiceMethodMapper)
func XxZz(ctx tp.CallCtx, arg *<T>) (<T>, *tp.Status) {
...
return r, nil
}
- register it to root router:
// register the call route
// HTTP mapping: /xx_zz
// RPC mapping: XxZz
peer.RouteCallFunc(XxZz)
type Bbb struct {
tp.PushCtx
}
func (b *Bbb) YyZz(arg *<T>) *tp.Status {
...
return nil
}
- register it to root router:
// register the push handler
// HTTP mapping: /bbb/yy_zz
// RPC mapping: Bbb.YyZz
peer.RoutePush(new(Bbb))
// or register the push handler
// HTTP mapping: /yy_zz
// RPC mapping: YyZz
peer.RoutePushFunc((*Bbb).YyZz)
// YyZz register the handler
func YyZz(ctx tp.PushCtx, arg *<T>) *tp.Status {
...
return nil
}
- register it to root router:
// register the push handler
// HTTP mapping: /yy_zz
// RPC mapping: YyZz
peer.RoutePushFunc(YyZz)
func XxxUnknownCall (ctx tp.UnknownCallCtx) (interface{}, *tp.Status) {
...
return r, nil
}
- register it to root router:
// register the unknown call route: /*
peer.SetUnknownCall(XxxUnknownCall)
func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Status {
...
return nil
}
- register it to root router:
// register the unknown push route: /*
peer.SetUnknownPush(XxxUnknownPush)
// NewIgnoreCase Returns a ignoreCase plugin.
func NewIgnoreCase() *ignoreCase {
return &ignoreCase{}
}
type ignoreCase struct{}
var (
_ tp.PostReadCallHeaderPlugin = new(ignoreCase)
_ tp.PostReadPushHeaderPlugin = new(ignoreCase)
)
func (i *ignoreCase) Name() string {
return "ignoreCase"
}
func (i *ignoreCase) PostReadCallHeader(ctx tp.ReadCtx) *tp.Status {
// Dynamic transformation path is lowercase
ctx.UriObject().Path = strings.ToLower(ctx.UriObject().Path)
return nil
}
func (i *ignoreCase) PostReadPushHeader(ctx tp.ReadCtx) *tp.Status {
// Dynamic transformation path is lowercase
ctx.UriObject().Path = strings.ToLower(ctx.UriObject().Path)
return nil
}
// add router group
group := peer.SubRoute("test")
// register to test group
group.RouteCall(new(Aaa), NewIgnoreCase())
peer.RouteCallFunc(XxZz, NewIgnoreCase())
group.RoutePush(new(Bbb))
peer.RoutePushFunc(YyZz)
peer.SetUnknownCall(XxxUnknownCall)
peer.SetUnknownPush(XxxUnknownPush)
type PeerConfig struct {
Network string `yaml:"network" ini:"network" comment:"Network; tcp, tcp4, tcp6, unix, unixpacket or quic"`
LocalIP string `yaml:"local_ip" ini:"local_ip" comment:"Local IP"`
ListenPort uint16 `yaml:"listen_port" ini:"listen_port" comment:"Listen port; for server role"`
DialTimeout time.Duration `yaml:"dial_timeout" ini:"dial_timeout" comment:"Default maximum duration for dialing; for client role; ns,µs,ms,s,m,h"`
RedialTimes int32 `yaml:"redial_times" ini:"redial_times" comment:"The maximum times of attempts to redial, after the connection has been unexpectedly broken; Unlimited when <0; for client role"`
RedialInterval time.Duration `yaml:"redial_interval" ini:"redial_interval" comment:"Interval of redialing each time, default 100ms; for client role; ns,µs,ms,s,m,h"`
DefaultBodyCodec string `yaml:"default_body_codec" ini:"default_body_codec" comment:"Default body codec type id"`
DefaultSessionAge time.Duration `yaml:"default_session_age" ini:"default_session_age" comment:"Default session max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
DefaultContextAge time.Duration `yaml:"default_context_age" ini:"default_context_age" comment:"Default CALL or PUSH context max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
SlowCometDuration time.Duration `yaml:"slow_comet_duration" ini:"slow_comet_duration" comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
PrintDetail bool `yaml:"print_detail" ini:"print_detail" comment:"Is print body and metadata or not"`
CountTime bool `yaml:"count_time" ini:"count_time" comment:"Is count cost time or not"`
}
-
SetMessageSizeLimit sets max message size. If maxSize<=0, set it to max uint32.
func SetMessageSizeLimit(maxMessageSize uint32)
-
SetSocketKeepAlive sets whether the operating system should send keepalive messages on the connection.
func SetSocketKeepAlive(keepalive bool)
-
SetSocketKeepAlivePeriod sets period between keep alives.
func SetSocketKeepAlivePeriod(d time.Duration)
-
SetSocketNoDelay controls whether the operating system should delay message transmission in hopes of sending fewer messages (Nagle's algorithm). The default is true (no delay), meaning that data is sent as soon as possible after a Write.
func SetSocketNoDelay(_noDelay bool)
-
SetSocketReadBuffer sets the size of the operating system's receive buffer associated with the connection.
func SetSocketReadBuffer(bytes int)
-
SetSocketWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.
func SetSocketWriteBuffer(bytes int)
package | import | description |
---|---|---|
json | "github.com/henrylee2cn/teleport/v6/codec" |
JSON codec(teleport own) |
protobuf | "github.com/henrylee2cn/teleport/v6/codec" |
Protobuf codec(teleport own) |
thrift | "github.com/henrylee2cn/teleport/v6/codec" |
Form(url encode) codec(teleport own) |
xml | "github.com/henrylee2cn/teleport/v6/codec" |
Form(url encode) codec(teleport own) |
plain | "github.com/henrylee2cn/teleport/v6/codec" |
Plain text codec(teleport own) |
form | "github.com/henrylee2cn/teleport/v6/codec" |
Form(url encode) codec(teleport own) |
package | import | description |
---|---|---|
auth | "github.com/henrylee2cn/teleport/v6/plugin/auth" |
An auth plugin for verifying peer at the first time |
binder | "github.com/henrylee2cn/teleport/v6/plugin/binder" |
Parameter Binding Verification for Struct Handler |
heartbeat | "github.com/henrylee2cn/teleport/v6/plugin/heartbeat" |
A generic timing heartbeat plugin |
proxy | "github.com/henrylee2cn/teleport/v6/plugin/proxy" |
A proxy plugin for handling unknown calling or pushing |
secure | "github.com/henrylee2cn/teleport/v6/plugin/secure" |
Encrypting/decrypting the message body |
overloader | "github.com/henrylee2cn/teleport/v6/plugin/overloader" |
A plugin to protect teleport from overload |
package | import | description |
---|---|---|
rawproto | "github.com/henrylee2cn/teleport/v6/proto/rawproto |
A fast socket communication protocol(teleport default protocol) |
jsonproto | "github.com/henrylee2cn/teleport/v6/proto/jsonproto" |
A JSON socket communication protocol |
pbproto | "github.com/henrylee2cn/teleport/v6/proto/pbproto" |
A Protobuf socket communication protocol |
thriftproto | "github.com/henrylee2cn/teleport/v6/proto/thriftproto" |
A Thrift communication protocol |
httproto | "github.com/henrylee2cn/teleport/v6/proto/httproto" |
A HTTP style socket communication protocol |
package | import | description |
---|---|---|
gzip | "github.com/henrylee2cn/teleport/v6/xfer/gzip" |
Gzip(teleport own) |
md5 | "github.com/henrylee2cn/teleport/v6/xfer/md5" |
Provides a integrity check transfer filter |
package | import | description |
---|---|---|
multiclient | "github.com/henrylee2cn/teleport/v6/mixer/multiclient" |
Higher throughput client connection pool when transferring large messages (such as downloading files) |
websocket | "github.com/henrylee2cn/teleport/v6/mixer/websocket" |
Makes the Teleport framework compatible with websocket protocol as specified in RFC 6455 |
evio | "github.com/henrylee2cn/teleport/v6/mixer/evio" |
A fast event-loop networking framework that uses the teleport API layer |
html | html "github.com/xiaoenai/tp-micro/helper/mod-html" |
HTML render for http client |
project | description |
---|---|
TP-Micro | TP-Micro is a simple, powerful micro service framework based on Teleport |
Pholcus | Pholcus is a distributed, high concurrency and powerful web crawler software |
Teleport is under Apache v2 License. See the LICENSE file for the full license text