unlimitedws

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 7 Imported by: 0

README

Unlimited Websocket

Unlimitedws is a tiny, well-tested library which helps you build a robust Websocket server with a few lines of code. Specifically, the Websocket server can handle up to several million concurrent connections with only a few GBs of memory. The library has successfully done that by utilizing async IO, careful tuning OS, zero-upgrade TCP connections, efficient buffer reuse and worker pool in case of DDOS.

Prerequisites

  1. Linux OS
  2. Some MBs of memory :)

Installation

go get github.com/hxt365/unlimitedws

Usage

A robust Echo Websocket system, that handles 100k concurrent connections within only 200 MBs of memory, can be implemented as follows:

server, _ := unlimitedws.DefaultServer(":8000")

// OnConnect handles logic when a connection comes up
server.OnConnect = func(conn net.Conn) {
    fmt.Println("welcome", nameConn(conn))
}

// OnRead handles logic when a connection sends a message
server.OnRead = func(conn net.Conn) error {
    msg, op, err := wsutil.ReadClientData(conn)
    if err != nil {
        return err
    }
    err = wsutil.WriteServerMessage(conn, op, msg)
    if err != nil {
        return err
    }
    fmt.Println(string(msg))
    return nil
}

// OnClose handles logic when a connection gets closed
server.OnClose = func(conn net.Conn) {
    fmt.Println("goodbye", nameConn(conn))
}

server.Run()

Full example: Echo.

Built With

Unlimitedws is built on top of the excellent Gobwas library. It is highly recommended to read the documentation of that library for advanced usage.

Planning features

  1. Custom Cookie handler, for authentication purpose.
  2. Close orphan connections.
  3. Websocket fallbacks.
  4. Sending missing messages when reconnecting.
  5. Horizontal scaling.

Motivation

  1. Scaling WebSocket in Go and beyond article by Alexander Emelin.
  2. A Million WebSockets and Go article by Sergey Kamardin.
  3. Going Infinite, handling 1 millions websockets connections in Go video by Eran Yanay.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {

	// OnConnect handles logic when a connection comes up
	OnConnect func(conn net.Conn)
	// OnRead handles logic when a connection sends a message
	OnRead func(conn net.Conn) error
	// OnClose handles logic when a connection gets closed
	OnClose func(conn net.Conn)
	// contains filtered or unexported fields
}

func DefaultServer

func DefaultServer(addr string) (*Server, error)

DefaultServer is a echo server with default config

func NewServer

func NewServer(addr string) (*Server, error)

func (*Server) Run

func (s *Server) Run()

Run makes the server ready for accepting request. Here we use asyncIO, with the help of edge-triggered epoll, for the sake of performance. In addition, we use worker pool in order to prevent DDOS. If the server can not accept incoming request for some reasons (eg: run out of resources), we let the server cool down.

func (*Server) SetCooldownTime

func (s *Server) SetCooldownTime(time time.Duration)

SetCooldownTime set cooldown time for the server

func (*Server) SetIOTimeout

func (s *Server) SetIOTimeout(timeout time.Duration)

SetIOTimeout set io timeout for every readl/write call of connections To not set deadline, set timeout to 0

func (*Server) SetPoller

func (s *Server) SetPoller() error

SetPoller set a poller for the server

func (*Server) SetPool

func (s *Server) SetPool(size, queue int) error

SetPool set a worker pool for the server

func (*Server) SetScheduleTimeout

func (s *Server) SetScheduleTimeout(timeout time.Duration)

SetScheduleTimeout set schedule timeout for the server

Directories

Path Synopsis
examples
internal
test

Jump to

Keyboard shortcuts

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