Git Product home page Git Product logo

tinytcp's Introduction

tinytcp

master status

tinytcp is a high-performance, zero-allocation TCP server in Go. It wraps around the standard net library to provide a sane API for quick prototyping. Major features include:

  • No external dependencies.
  • No memory allocations on critical paths, all the important objects and buffers are pooled.
  • Automated packet extraction with no memory allocations (see LengthPrefixedFraming or SplitBySeparator).
  • Full customization of connection handling process. By default, the sever starts a new goroutine for each connection, (GoroutinePerConnection strategy), but this can be changed.
  • Metrics collection for both the server and each connected client separately (optional Prometheus binding).
  • Support for tcp, tcp4, tcp6 and unix listeners.

Install

go get github.com/mkorman9/tinytcp

Example

package main

import (
	"fmt"
	"github.com/mkorman9/tinytcp"
)

func main() {
	server := tinytcp.NewServer("0.0.0.0:7000")

	server.OnStart(func() {
		fmt.Printf("Server started on: %d\n", server.Port())
	})

	server.ForkingStrategy(tinytcp.GoroutinePerConnection(serve))

	if err := tinytcp.StartAndBlock(server); err != nil {
		fmt.Printf("Error while starting: %v\n", err)
	}
}

func serve(socket *tinytcp.Socket) {
	socket.Write([]byte("Hello world!"))
}

Benchmarks

GOMAXPROCS=1

$ GOMAXPROCS=1 go test ./... -bench=. -benchmem
BenchmarkSingleClient             728698              1582 ns/op               0 B/op          0 allocs/op
BenchmarkConcurrentClients        757249              1577 ns/op               0 B/op          0 allocs/op

GOMAXPROCS=8

$ GOMAXPROCS=8 go test ./... -bench=. -benchmem
BenchmarkSingleClient-8           615766              1838 ns/op               0 B/op          0 allocs/op
BenchmarkConcurrentClients-8     4523785               273.7 ns/op             0 B/op          0 allocs/op

tinytcp's People

Contributors

mkorman9 avatar

Stargazers

randydom avatar

Watchers

 avatar randydom avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.