socket

package
v4.1.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2018 License: Apache-2.0, Apache-2.0 Imports: 20 Imported by: 0

README

Socket

A concise, powerful and high-performance connection socket.

Feature

  • The server and client are peer-to-peer interfaces
  • Support set the size of socket I/O buffer
  • Support custom communication protocol
  • Support custom transfer filter pipe (Such as gzip, encrypt, verify...)
  • Message contains both Header and Body
  • Supports custom encoding types, e.g JSON Protobuf
  • Header contains the status code and its description text
  • Each socket is assigned an id
  • Provides Socket Hub, Socket pool and *Message stack
  • Support setting the size of the reading message (if exceed disconnect it)
  • Provide an operating interface to control the connection file descriptor

Benchmark

Test Case

  • 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

Test Results

  • 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

test code

  • Profile torch of teleport/socket

tp_socket_profile_torch

svg file

  • Heap torch of teleport/socket

tp_socket_heap_torch

svg file

Example

server.go
package main

import (
	"log"
	"net"

	"github.com/henrylee2cn/teleport/socket"
	"github.com/henrylee2cn/teleport/socket/example/pb"
)

func main() {
	socket.SetMessageSizeLimit(512)
	lis, err := net.Listen("tcp", "0.0.0.0:8000")
	if err != nil {
		log.Fatalf("[SVR] listen err: %v", err)
	}
	log.Printf("listen tcp 0.0.0.0:8000")
	for {
		conn, err := lis.Accept()
		if err != nil {
			log.Fatalf("[SVR] accept err: %v", err)
		}
		go func(s socket.Socket) {
			log.Printf("accept %s", s.Id())
			defer s.Close()
			var pbTest = new(pb.PbTest)
			for {
				// read request
				var message = socket.GetMessage(socket.WithNewBody(
					func(header socket.Header) interface{} {
						*pbTest = pb.PbTest{}
						return pbTest
					}),
				)
				err = s.ReadMessage(message)
				if err != nil {
					log.Printf("[SVR] read request err: %v", err)
					return
				} else {
					// log.Printf("[SVR] read request: %v", message)
				}

				// write response
				pbTest.A = pbTest.A + pbTest.B
				pbTest.B = pbTest.A - pbTest.B*2
				message.SetBody(pbTest)

				err = s.WriteMessage(message)
				if err != nil {
					log.Printf("[SVR] write response err: %v", err)
				} else {
					// log.Printf("[SVR] write response: %v", message)
				}
				socket.PutMessage(message)
			}
		}(socket.GetSocket(conn))
	}
}
client.go
package main

import (
	"log"
	"net"

	"github.com/henrylee2cn/teleport/codec"
	"github.com/henrylee2cn/teleport/socket"

	"github.com/henrylee2cn/teleport/socket/example/pb"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:8000")
	if err != nil {
		log.Fatalf("[CLI] dial err: %v", err)
	}
	s := socket.GetSocket(conn)
	defer s.Close()
	var message = socket.GetMessage()
	defer socket.PutMessage(message)
	for i := uint64(0); i < 1; i++ {
		// write request
		message.Reset()
		message.SetMtype(0)
		message.SetBodyCodec(codec.ID_JSON)
		message.SetSeq(i)
		message.SetUri("/a/b")
		message.SetBody(&pb.PbTest{A: 10, B: 2})
		err = s.WriteMessage(message)
		if err != nil {
			log.Printf("[CLI] write request err: %v", err)
			continue
		}
		log.Printf("[CLI] write request: %v", message)

		// read response
		message.Reset(socket.WithNewBody(
			func(header socket.Header) interface{} {
				return new(pb.PbTest)
			}),
		)
		err = s.ReadMessage(message)
		if err != nil {
			log.Printf("[CLI] read response err: %v", err)
		} else {
			log.Printf("[CLI] read response: %v", message)
		}
	}
}

More Examples

Keyworks

  • Message: The corresponding structure of the data package
  • Proto: The protocol interface of message pack/unpack
  • Codec: Serialization interface for Message.Body
  • XferPipe: A series of pipelines to handle message data before transfer
  • XferFilter: A interface to handle message data before transfer

Message

The contents of every one message:

// in .../teleport/socket package
type (
	type Message struct {
		// Has unexported fields.
	}
	    Message a socket data message.
	
	func GetMessage(settings ...MessageSetting) *Message
	func NewMessage(settings ...MessageSetting) *Message
	func (m *Message) Body() interface{}
	func (m *Message) BodyCodec() byte
	func (m *Message) Context() context.Context
	func (m *Message) MarshalBody() ([]byte, error)
	func (m *Message) Meta() *utils.Args
	func (m *Message) Mtype() byte
	func (m *Message) Reset(settings ...MessageSetting)
	func (m *Message) Seq() string
	func (m *Message) SetBody(body interface{})
	func (m *Message) SetBodyCodec(bodyCodec byte)
	func (m *Message) SetNewBody(newBodyFunc NewBodyFunc)
	func (m *Message) SetMtype(mtype byte)
	func (m *Message) SetSeq(seq string)
	func (m *Message) SetSize(size uint32) error
	func (m *Message) SetUri(uri string)
	func (m *Message) SetUriObject(uriObject *url.URL)
	func (m *Message) Size() uint32
	func (m *Message) String() string
	func (m *Message) UnmarshalBody(bodyBytes []byte) error
	func (m *Message) Uri() string
	func (m *Message) UriObject() *url.URL
	func (m *Message) XferPipe() *xfer.XferPipe

	// NewBodyFunc creates a new body by header.
	NewBodyFunc func(Header) interface{}
)

// in .../teleport/xfer package
type (
	// XferPipe transfer filter pipe, handlers from outer-most to inner-most.
	// Note: the length can not be bigger than 255!
	XferPipe struct {
		filters []XferFilter
	}
	// XferFilter handles byte stream of message when transfer.
	XferFilter interface {
		Id() byte
		OnPack([]byte) ([]byte, error)
		OnUnpack([]byte) ([]byte, error)
	}
)

Protocol

You can customize your own communication protocol by implementing the interface:

type (
	// Proto pack/unpack protocol scheme of socket message.
	Proto interface {
		// Version returns the protocol's id and name.
		Version() (byte, string)
		// Pack writes the Message into the connection.
		// Note: Make sure to write only once or there will be package contamination!
		Pack(*Message) error
		// Unpack reads bytes from the connection to the Message.
		// Note: Concurrent unsafe!
		Unpack(*Message) error
	}
	ProtoFunc func(io.ReadWriter) Proto
)

Next, you can specify the communication protocol in the following ways:

func SetDefaultProtoFunc(ProtoFunc)
func GetSocket(net.Conn, ...ProtoFunc) Socket
func NewSocket(net.Conn, ...ProtoFunc) Socket

Default protocol RawProto(Big Endian):

{4 bytes message length}
{1 byte protocol version}
{1 byte transfer pipe length}
{transfer pipe IDs}
# The following is handled data by transfer pipe
{2 bytes sequence length}
{sequence}
{1 byte message type} # e.g. CALL:1; REPLY:2; PUSH:3
{2 bytes URI length}
{URI}
{2 bytes metadata length}
{metadata(urlencoded)}
{1 byte body codec id}
{body}

Optimize

  • SetMessageSizeLimit sets max message size. If maxSize<=0, set it to max uint32.

    func SetMessageSizeLimit(maxMessageSize uint32)
    
  • SetKeepAlive sets whether the operating system should send keepalive messages on the connection.

    func SetKeepAlive(keepalive bool)
    
  • SetKeepAlivePeriod sets period between keep alives.

    func SetKeepAlivePeriod(d time.Duration)
    
  • SetNoDelay 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 SetNoDelay(_noDelay bool)
    
  • SetReadBuffer sets the size of the operating system's receive buffer associated with the connection.

    func SetReadBuffer(bytes int)
    
  • SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection.

    func SetWriteBuffer(bytes int)
    

Documentation

Overview

Package socket provides a concise, powerful and high-performance TCP.

Copyright 2017 HenryLee. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrExceedMessageSizeLimit error
	ErrExceedMessageSizeLimit = errors.New("Size of package exceeds limit.")
)
View Source
var ErrProactivelyCloseSocket = errors.New("socket is closed proactively")

ErrProactivelyCloseSocket proactively close the socket error.

View Source
var NewRawProtoFunc = func(rw io.ReadWriter) Proto {
	return &rawProto{
		id:   'r',
		name: "raw",
		r:    rw,
		w:    rw,
	}
}

NewRawProtoFunc is creation function of fast socket protocol. NOTE: it is the default protocol.

Functions

func MessageSizeLimit

func MessageSizeLimit() uint32

MessageSizeLimit gets the message size upper limit of reading.

func PutMessage

func PutMessage(m *Message)

PutMessage puts a *Message to message stack.

func ReadBuffer

func ReadBuffer() (bytes int, isDefault bool)

ReadBuffer returns the size of the operating system's receive buffer associated with the connection. Note: if using the system default value, bytes=-1 and isDefault=true.

func SetDefaultProtoFunc

func SetDefaultProtoFunc(protoFunc ProtoFunc)

SetDefaultProtoFunc sets the default builder of socket communication protocol

func SetKeepAlive

func SetKeepAlive(keepalive bool)

SetKeepAlive sets whether the operating system should send keepalive messages on the connection. Note: If have not called the function, the system defaults are used.

func SetKeepAlivePeriod

func SetKeepAlivePeriod(d time.Duration)

SetKeepAlivePeriod sets period between keep alives. Note: if d<0, don't change the value.

func SetMessageSizeLimit

func SetMessageSizeLimit(maxMessageSize uint32)

SetMessageSizeLimit sets max message size. If maxSize<=0, set it to max uint32.

func SetNoDelay

func SetNoDelay(_noDelay bool)

SetNoDelay 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 SetReadBuffer

func SetReadBuffer(bytes int)

SetReadBuffer sets the size of the operating system's receive buffer associated with the connection. Note: if bytes<0, don't change the value.

func SetWriteBuffer

func SetWriteBuffer(bytes int)

SetWriteBuffer sets the size of the operating system's transmit buffer associated with the connection. Note: if bytes<0, don't change the value.

func WriteBuffer

func WriteBuffer() (bytes int, isDefault bool)

WriteBuffer returns the size of the operating system's transmit buffer associated with the connection. Note: if using the system default value, bytes=-1 and isDefault=true.

Types

type Body

type Body interface {
	// BodyCodec returns the body codec type id
	BodyCodec() byte
	// SetBodyCodec sets the body codec type id
	SetBodyCodec(bodyCodec byte)
	// Body returns the body object
	Body() interface{}
	// SetBody sets the body object
	SetBody(body interface{})
	// SetNewBody resets the function of geting body.
	SetNewBody(newBodyFunc NewBodyFunc)
	// MarshalBody returns the encoding of body.
	// NOTE: when the body is a stream of bytes, no marshalling is done.
	MarshalBody() ([]byte, error)
	// UnmarshalBody unmarshals the encoded data to the body.
	// NOTE:
	//  seq, mtype, uri must be setted already;
	//  if body=nil, try to use newBodyFunc to create a new one;
	//  when the body is a stream of bytes, no unmarshalling is done.
	UnmarshalBody(bodyBytes []byte) error
}

Body message body interface

type Header interface {
	// Mtype returns the message sequence
	Seq() string
	// SetSeq sets the message sequence
	// NOTE: max len ≤ 65535!
	SetSeq(string)
	// Mtype returns the message type, such as CALL, PUSH, REPLY
	Mtype() byte
	// Mtype sets the message type
	SetMtype(byte)
	// Uri returns the URI string
	Uri() string
	// UriObject returns the URI object
	UriObject() *url.URL
	// SetUri sets the message URI
	// NOTE: max len ≤ 65535!
	SetUri(string)
	// SetUriObject sets the message URI
	// NOTE: urlencoded URI max len ≤ 65535!
	SetUriObject(uriObject *url.URL)
	// Meta returns the metadata
	// NOTE: urlencoded string max len ≤ 65535!
	Meta() *utils.Args
}

Header message header interface

type Message

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

Message a socket message data.

func GetMessage

func GetMessage(settings ...MessageSetting) *Message

GetMessage gets a *Message form message stack. NOTE:

newBodyFunc is only for reading form connection;
settings are only for writing to connection.

func NewMessage

func NewMessage(settings ...MessageSetting) *Message

NewMessage creates a new *Message. NOTE:

NewBody is only for reading form connection;
settings are only for writing to connection.

func (*Message) Body

func (m *Message) Body() interface{}

Body returns the body object

func (*Message) BodyCodec

func (m *Message) BodyCodec() byte

BodyCodec returns the body codec type id

func (*Message) Context

func (m *Message) Context() context.Context

Context returns the message handling context.

func (*Message) MarshalBody

func (m *Message) MarshalBody() ([]byte, error)

MarshalBody returns the encoding of body. NOTE: when the body is a stream of bytes, no marshalling is done.

func (*Message) Meta

func (m *Message) Meta() *utils.Args

Meta returns the metadata. When the package is reset, it will be reset. NOTE: urlencoded string max len ≤ 65535!

func (*Message) Mtype

func (m *Message) Mtype() byte

Mtype returns the message type, such as CALL, PUSH, REPLY

func (*Message) Reset

func (m *Message) Reset(settings ...MessageSetting)

Reset resets itself. NOTE:

newBodyFunc is only for reading form connection;
settings are only for writing to connection.

func (*Message) Seq

func (m *Message) Seq() string

Seq returns the message sequence

func (*Message) SetBody

func (m *Message) SetBody(body interface{})

SetBody sets the body object

func (*Message) SetBodyCodec

func (m *Message) SetBodyCodec(bodyCodec byte)

SetBodyCodec sets the body codec type id

func (*Message) SetMtype

func (m *Message) SetMtype(mtype byte)

SetMtype sets the message type

func (*Message) SetNewBody

func (m *Message) SetNewBody(newBodyFunc NewBodyFunc)

SetNewBody resets the function of geting body.

func (*Message) SetSeq

func (m *Message) SetSeq(seq string)

SetSeq sets the message sequence NOTE: max len ≤ 65535!

func (*Message) SetSize

func (m *Message) SetSize(size uint32) error

SetSize sets the size of message. If the size is too big, returns error.

func (*Message) SetUri

func (m *Message) SetUri(uri string)

SetUri sets the message URI NOTE: max len ≤ 65535!

func (*Message) SetUriObject

func (m *Message) SetUriObject(uriObject *url.URL)

SetUriObject sets the message URI NOTE: urlencoded URI max len ≤ 65535!

func (*Message) Size

func (m *Message) Size() uint32

Size returns the size of message.

func (*Message) String

func (m *Message) String() string

String returns printing text.

func (*Message) UnmarshalBody

func (m *Message) UnmarshalBody(bodyBytes []byte) error

UnmarshalBody unmarshals the encoded data to the body. NOTE:

seq, mtype, uri must be setted already;
if body=nil, try to use newBodyFunc to create a new one;
when the body is a stream of bytes, no unmarshalling is done.

func (*Message) Uri

func (m *Message) Uri() string

Uri returns the URI string

func (*Message) UriObject

func (m *Message) UriObject() *url.URL

UriObject returns the URI object

func (*Message) XferPipe

func (m *Message) XferPipe() *xfer.XferPipe

XferPipe returns transfer filter pipe, handlers from outer-most to inner-most. NOTE: the length can not be bigger than 255!

type MessageSetting

type MessageSetting func(*Message)

MessageSetting is a pipe function type for setting message.

func WithAddMeta

func WithAddMeta(key, value string) MessageSetting

WithAddMeta adds 'key=value' metadata argument. Multiple values for the same key may be added. NOTE: urlencoded string max len ≤ 65535!

func WithBody

func WithBody(body interface{}) MessageSetting

WithBody sets the body object.

func WithBodyCodec

func WithBodyCodec(bodyCodec byte) MessageSetting

WithBodyCodec sets the body codec.

func WithContext

func WithContext(ctx context.Context) MessageSetting

WithContext sets the message handling context.

func WithMtype

func WithMtype(mtype byte) MessageSetting

WithMtype sets the message type.

func WithNewBody

func WithNewBody(newBodyFunc NewBodyFunc) MessageSetting

WithNewBody resets the function of geting body.

func WithQuery

func WithQuery(key, value string) MessageSetting

WithQuery sets the message URI query parameter. NOTE: urlencoded URI max len ≤ 65535!

func WithSeq

func WithSeq(seq string) MessageSetting

WithSeq sets the message sequence. NOTE: max len ≤ 65535!

func WithSetMeta

func WithSetMeta(key, value string) MessageSetting

WithSetMeta sets 'key=value' metadata argument. NOTE: urlencoded string max len ≤ 65535!

func WithUri

func WithUri(uri string) MessageSetting

WithUri sets the message URI string. NOTE: max len ≤ 65535!

func WithUriObject

func WithUriObject(uriObject *url.URL) MessageSetting

WithUriObject sets the message URI object. NOTE: urlencoded URI max len ≤ 65535!

func WithXferPipe

func WithXferPipe(filterId ...byte) MessageSetting

WithXferPipe sets transfer filter pipe. NOTE:

panic if the filterId is not registered

type NewBodyFunc

type NewBodyFunc func(Header) interface{}

NewBodyFunc creates a new body by header.

type Proto

type Proto interface {
	// Version returns the protocol's id and name.
	Version() (byte, string)
	// Pack writes the Message into the connection.
	// Note: Make sure to write only once or there will be package contamination!
	Pack(*Message) error
	// Unpack reads bytes from the connection to the Message.
	// Note: Concurrent unsafe!
	Unpack(*Message) error
}

Proto pack/unpack protocol scheme of socket message.

type ProtoFunc

type ProtoFunc func(io.ReadWriter) Proto

ProtoFunc function used to create a custom Proto interface.

func DefaultProtoFunc

func DefaultProtoFunc() ProtoFunc

DefaultProtoFunc gets the default builder of socket communication protocol

type RawConn

type RawConn interface {
	// Raw returns the raw net.Conn
	Raw() net.Conn
}

RawConn raw conn

type Socket

type Socket interface {
	// ControlFD invokes f on the underlying connection's file
	// descriptor or handle.
	// The file descriptor fd is guaranteed to remain valid while
	// f executes but not after f returns.
	ControlFD(f func(fd uintptr)) error
	// LocalAddr returns the local network address.
	LocalAddr() net.Addr
	// RemoteAddr returns the remote network address.
	RemoteAddr() net.Addr
	// SetDeadline sets the read and write deadlines associated
	// with the connection. It is equivalent to calling both
	// SetReadDeadline and SetWriteDeadline.
	//
	// A deadline is an absolute time after which I/O operations
	// fail with a timeout (see type Error) instead of
	// blocking. The deadline applies to all future and pending
	// I/O, not just the immediately following call to Read or
	// Write. After a deadline has been exceeded, the connection
	// can be refreshed by setting a deadline in the future.
	//
	// An idle timeout can be implemented by repeatedly extending
	// the deadline after successful Read or Write calls.
	//
	// A zero value for t means I/O operations will not time out.
	SetDeadline(t time.Time) error
	// SetReadDeadline sets the deadline for future Read calls
	// and any currently-blocked Read call.
	// A zero value for t means Read will not time out.
	SetReadDeadline(t time.Time) error
	// SetWriteDeadline sets the deadline for future Write calls
	// and any currently-blocked Write call.
	// Even if write times out, it may return n > 0, indicating that
	// some of the data was successfully written.
	// A zero value for t means Write will not time out.
	SetWriteDeadline(t time.Time) error
	// WriteMessage writes header and body to the connection.
	// Note: must be safe for concurrent use by multiple goroutines.
	WriteMessage(message *Message) error
	// ReadMessage reads header and body from the connection.
	// Note: must be safe for concurrent use by multiple goroutines.
	ReadMessage(message *Message) error
	// Read reads data from the connection.
	// Read can be made to time out and return an Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetReadDeadline.
	Read(b []byte) (n int, err error)
	// Write writes data to the connection.
	// Write can be made to time out and return an Error with Timeout() == true
	// after a fixed time limit; see SetDeadline and SetWriteDeadline.
	Write(b []byte) (n int, err error)
	// Close closes the connection socket.
	// Any blocked Read or Write operations will be unblocked and return errors.
	Close() error
	// Swap returns custom data swap of the socket.
	Swap() goutil.Map
	// SwapLen returns the amount of custom data of the socket.
	SwapLen() int
	// Id returns the socket id.
	Id() string
	// SetId sets the socket id.
	SetId(string)
	// Reset reset net.Conn and ProtoFunc.
	Reset(netConn net.Conn, protoFunc ...ProtoFunc)
}

Socket is a generic stream-oriented network connection.

Multiple goroutines may invoke methods on a Socket simultaneously.

func GetSocket

func GetSocket(c net.Conn, protoFunc ...ProtoFunc) Socket

GetSocket gets a Socket from pool, and reset it.

func NewSocket

func NewSocket(c net.Conn, protoFunc ...ProtoFunc) Socket

NewSocket wraps a net.Conn as a Socket.

type SocketHub

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

SocketHub sockets hub

func NewSocketHub

func NewSocketHub() *SocketHub

NewSocketHub creates a new sockets hub.

func (*SocketHub) ChangeId

func (sh *SocketHub) ChangeId(newId string, socket Socket)

ChangeId changes the socket id. Note: if the old id is remoteAddr, won't delete the index from socketHub.

func (*SocketHub) Delete

func (sh *SocketHub) Delete(id string)

Delete deletes the Socket for a id.

func (*SocketHub) Get

func (sh *SocketHub) Get(id string) (Socket, bool)

Get gets Socket by id. If second returned arg is false, mean the Socket is not found.

func (*SocketHub) Len

func (sh *SocketHub) Len() int

Len returns the length of the socket hub. Note: the count implemented using sync.Map may be inaccurate.

func (*SocketHub) Random

func (sh *SocketHub) Random() (Socket, bool)

Random gets a Socket randomly. If third returned arg is false, mean no Socket is exist.

func (*SocketHub) Range

func (sh *SocketHub) Range(f func(Socket) bool)

Range calls f sequentially for each id and Socket present in the socket hub. If f returns false, range stops the iteration.

func (*SocketHub) Set

func (sh *SocketHub) Set(socket Socket)

Set sets a Socket.

Directories

Path Synopsis
pb

Jump to

Keyboard shortcuts

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