Giter Club home page Giter Club logo

zput_net_golang's Introduction

zput_net_golang network library

LICENSE Github Actions Go Report Card GoDoc

中文 | English

zput_net_golang event-driven (Reactor mode) based on high-performance , non-blocking and lightweight networking framework, not use the standard golang language net network package, its multiplexing according to different systems using different system functions (epoll (linux system) and kqueue (FreeBSD/DragonFly/Darwin system)),easy to quickly build high-performance server.

Features

  • Freedom to decide whether to lock or not, balancing performance and thread safety
  • Automatically expands space when cache is full
  • Provides peek at cached content in advance
  • Provide explore class functions that simulate reading first, but don't move the actual

Performance Testing

📈 test result

os platform: Mac

test for write and read

Example

echo server
package main

import (
	"flag"
	"github.com/zput/ringbuffer"
	"github.com/zput/zput_net_golang/net/event_loop"
	"github.com/zput/zput_net_golang/net/log"
	"github.com/zput/zput_net_golang/net/protocol"
	"github.com/zput/zput_net_golang/net/connect"
	"github.com/zput/zput_net_golang/net/tcpserver"
	"net/http"
	_ "net/http/pprof"
	"strconv"
	"sync/atomic"
	"time"
)

type Echo struct {
	tcpserver.HandleEventImpl
	connectTimes int64
}

func (this *Echo) GetConnectTimes() int64 {
	return this.connectTimes
}

func (this *Echo) ConnectCallback(c *tcpconnect.Connect) {
	atomic.AddInt64(&this.connectTimes, 1)
	this.HandleEventImpl.ConnectCallback(c)
}
func (this *Echo) MessageCallback(c *tcpconnect.Connect, buffer *ringbuffer.RingBuffer) {
	first, end := buffer.PeekAll()
	buffer.RetrieveAll()
	out := append(first, end...)
	c.Write(out)
}

func (this *Echo) OnClose(c *tcpconnect.Connect) {
	atomic.AddInt64(&this.connectTimes, -1)
	this.HandleEventImpl.ConnectCloseCallback(c)
}

func main() {
	go func() {
		if err := http.ListenAndServe(":6060", nil); err != nil {
			panic(err)
		}
	}()

	handler := new(Echo)
	var port int
	var loops int

	flag.IntVar(&port, "port", 58810, "server port")
	flag.IntVar(&loops, "loops", -1, "num loops")
	flag.Parse()

	log.Info("server begin")
	mainLoopPtr, err := event_loop.New()
	if err != nil {
		log.Error(err)
	}
	log.Info("created event_loop successful")

	s, err := tcpserver.New(handler, mainLoopPtr,
		protocol.Network("tcp"),
		protocol.Address(":"+strconv.Itoa(port)),
		protocol.NumLoops(loops))
	if err != nil {
		panic(err)
	}

	log.Info("created tcpserver successful")

	s.RunEvery(time.Second*2, func() {
		log.Info("connections :", handler.connectTimes)
	})

	s.Start()
	log.Info("server end")
}

Reference

Appendix

welcome pr

zput_net_golang's People

Contributors

zput avatar

Watchers

James Cloos avatar  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.