Skip to content
/ gtcp Public

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

License

Apache-2.0, BSD-3-Clause licenses found

Licenses found

Apache-2.0
LICENSE
BSD-3-Clause
LICENSE.golang
Notifications You must be signed in to change notification settings

cat2neat/gtcp

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

About

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

Resources

License

Apache-2.0, BSD-3-Clause licenses found

Licenses found

Apache-2.0
LICENSE
BSD-3-Clause
LICENSE.golang

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages