High-performance network library with event handle based on go language
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
.gitignore
LICENSE
README.md
conn.go
handle.go
server.go

README.md

NetHandle

This is a library for building high-performance TCP server applications, standing on the shoulders of Go giants and redcon

1. Features

  1. High performance, low resource consumption
  2. Very easy to use development interface

2. Install

go get -u github.com/gitsrc/NetHandle

3. Performance Testing:

3.1 50*10000 (50 threads X 10000 requests)

3.2 50*20000 (50 threads X 20000 requests)

3.3 100*10000 (100 threads X 10000 requests)

4. Sample code:

When using this library, you only need to customize the simple callback function to construct a powerful network listener.

package main

import (
	"fmt"
	"github.com/gitsrc/NetHandle"
	"log"
	"sync"
)

var addrTcp = "127.0.0.1:10000"

func main() {
	log.SetFlags(log.Lshortfile | log.LstdFlags)

	var mu sync.RWMutex
	count := 0
	go log.Printf("started server at %s", addrTcp)

	err := NetHandle.ListenAndServe("tcp", addrTcp,
		func(conn NetHandle.Conn) {

			requestData := make([]byte, 512)

			_, err := conn.NetConn().Read(requestData)

			if err != nil {
				conn.Close()
				return
			}

			mu.Lock()
			count++
			mu.Unlock()

			countCurrent := 0

			mu.RLock()
			countCurrent = count
			mu.RUnlock()

			replyData := fmt.Sprintf("%d\r\n", countCurrent)
			conn.NetConn().Write(append([]byte(replyData)))

		},
		func(conn NetHandle.Conn) bool {
			// use this function to accept or deny the connection.
			log.Printf("accept: %s", conn.RemoteAddr())
			return true
		},
		func(conn NetHandle.Conn, err error) {
			// this is called when the connection has been closed
			log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err)
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}