Skip to content

oldmantaiter/gbc

 
 

Repository files navigation

gbc GoDoc

This library provides a wrapper for net.Listener which returns net.Conn with buffered preconfigured enabling you to avoid writing another connection wrapper with buffers attached.

The aim are:

  • provide a basis for layered listeners which can share the same buffers
  • provide some wrappers for the buffered listener which perform various functions prior to calling Accept, such as vetting connections, or read preamble such as the proxy protocol example.

It uses sync.Pool to reuse buffered readers and writers, which are allocated using the default size of 1024, or the value provided to SetBufferSize.

usage

import (
	"log"

	"github.com/wolfeidau/gbc"
)


func main() {
	
	ln, err := gbc.Listen("tcp", ":8080")
	if err != nil {
		// handle error
	}

	// wrap the buffered listener in the proxy header listener
	// see http://www.haproxy.org/download/1.5/doc/proxy-protocol.txt 
	// this is used by haproxy and Elastic Load Balancer to pass through 
	// source and destination ip addresses to TCP listeners behind these services.
	pln := &gbc.ProxyListener{ln}

	for {
		conn, err := pln.Accept()
		if err != nil {
			// handle error
		}
		go handleConnection(conn)
	}

}

func handleConnection(conn net.Conn) {

	defer conn.Close() // this will flush if needed

	// print the remote address
	log.Printf("remote addr: %s", conn.RemoteAddr().String())

	// cast the connection to the buffered connection
	bconn := conn.(*gbc.BConn)

	// get the read writer for this buffered connection
	rw := bconn.ReadWriter()

	// use rw to do peak/flush ect
	...

}

References

Sponsor

This project was made possible by Ninja Blocks.

License

This code is Copyright (c) 2014 Mark Wolfe and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.

About

Golang library which provides a net.Conn with buffering preconfigured.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 98.0%
  • Makefile 2.0%