GithubHelp home page GithubHelp logo

cat2neat / gtcp Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 1.0 71 KB

Go TCP server framework that inherits battle-tested code from net/http and can be extended through built-in interfaces.

License: Apache License 2.0

Go 100.00%

gtcp's Introduction

gtcp

Go Report Card Build Status Coverage Status GoDoc

Package gtcp is a TCP server framework that inherits battle-tested code from net/http and can be extended through built-in interfaces.

Features

  • Can be used in the same manner with http.Server(>= 1.8).

    • Make API as much compatible as possible.
    • Make the zero value useful.
  • Inherits as much battle tested code from net/http.

  • Provides much flexiblity through built-in interfaces.

    • ConnHandler
      • ConnHandler
      • KeepAliveHandler that makes it easy to implement keepalive.
      • PipelineHandler that makes it easy to implement pipelining.
    • ConnTracker
      • MapConnTracker that handles force closing active connections also graceful shutdown.
      • WGConnTracker that handles only graceful shutdown using a naive way with sync.WaitGroup.
    • Conn
      • BufferedConn that wraps Conn in bufio.Reader/Writer.
      • StatsConn that wraps Conn to measure incomming/outgoing bytes.
      • DebugConn that wraps Conn to output debug information.
    • Logger
      • BuiltinLogger that logs using standard log package.
    • Retry
      • ExponentialRetry that implements exponential backoff algorithm without jitter.
    • Statistics
      • TrafficStatistics that measures incomming/outgoing traffic across a server.
    • Limiter
      • MaxConnLimiter that limits connections based on the maximum number.
  • Gets GC pressure as little as possible with sync.Pool.

  • Zero 3rd party depentencies.

TODO

  • Support TLS

  • Support multiple listeners

Example

import "github.com/cat2neat/gtcp"

// echo server:
// https://tools.ietf.org/html/rfc862
func echoServer() error {
	srv := &gtcp.Server{
		Addr:    ":1979",
		NewConn: gtcp.NewStatsConn,
		ConnHandler: func(ctx context.Context, conn gtcp.Conn) {
			buf := make([]byte, 1024)
			for {
				n, err := conn.Read(buf)
				if err != nil {
					return
				}
				conn.Write(buf[:n])
				err = conn.Flush()
				if err != nil {
					return
				}
				select {
				case <-ctx.Done():
					// canceled by parent
					return
				default:
				}
			}
		},
	}
	return srv.ListenAndServe()
}

Install

go get -u github.com/cat2neat/gtcp

gtcp's People

Contributors

cat2neat avatar

Stargazers

joc avatar Tao avatar Matt Singletary avatar  avatar Alex Wang avatar syklevin avatar Bryan Allred avatar  avatar yoshiyuki.kanno avatar Luc Stepniewski avatar  avatar Denis Denisov avatar Adrian Lanzafame avatar Dragos Harabor avatar Raffaele Sena avatar  avatar Tanabe Ken-ichi avatar

Watchers

yoshiyuki.kanno avatar

Forkers

jdwang001

gtcp's Issues

conn: Implement Peek for baseConn

Implementing Peek for baseConn to prevent from panic.
Call net.Conn.Read and cache the result byte retrieved into an inner field for later baseConn.Read ops.

Files missing copyright notices

The individual files are missing copyright notices about licensing. Each file should be begin with a copyright notice and an optional reference to the license file.

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.