A chance to come into contact with golang, Attracted by its high concurrency legend , Start learning this language , The more you learn, the more interesting it feels ^_^
I have registered blog park for so many years , Writing for the first time , Older , My brain is not working well , You have to write it down , write down , For your future reference , Also share with golang Lovers .
gnet stay CSDN The introduction on the is as follows :https://blog.csdn.net/qq_31967569/article/details/103107707
github v1 The open source address of the version is :https://github.com/panjf2000/gnet
See here for a detailed introduction , The English version has the introduction of each module , More detailed :https://pkg.go.dev/github.com/panjf2000/gnet?GOOS=windows#Option
gnet Author pan Shao is a handsome young man in China ^_^, It's right to know gnet This framework has different views , There is also something to say , There are different sounds , Is a good thing , Use it if you like , Don't use if you don't like it , Please respect open source authors . Open source authors on go The contribution of the community is obvious . Here are some notes I took in using .
Take a note of it Options, For convenience of inquiry , Keep the original English remarks
type Options struct { // Multicore indicates whether the server will be effectively created with multi-cores, if so,
// then you must take care with synchronizing memory between all event callbacks, otherwise,
// it will run the server with single thread. The number of threads in the server will be automatically
// assigned to the value of logical CPUs usable by the current process.
// Multicore Indicates whether the server will effectively use multi-core creation , If it is , You must pay attention to synchronizing memory between all event callbacks , Otherwise it will run the server as a single thread .
// The number of threads in the server will be automatically allocated to the logic available to the current process CPU Value .
Multicore bool // NumEventLoop is set up to start the given number of event-loop goroutine.
// NumEventLoop Set to start a given number of event-loop goroutine, That is to say sub reactor The number of ---- I specially consulted pan Shao in private about the description of this parameter
// Note: Setting up NumEventLoop will override Multicore.
// This is something to watch out for , If set NumEventLoop Value , that Multicored The parameters of will be overwritten , Pan Shao's reply is generally NumEventLoop This parameter ,Multicore and NumEventLoop You can use any parameter , It needs to be verified during production pressure test
// NumEventLoop How much is the value of , It's also an empirical value , It needs to be adjusted according to the actual situation , This can be used in the configuration file , It is easy to adjust at any time in the production environment without modifying the code
NumEventLoop int // LB represents the load-balancing algorithm used when assigning new connections.
LB LoadBalancing // ReuseAddr indicates whether to set up the SO_REUSEADDR socket option.
ReuseAddr bool // ReusePort indicates whether to set up the SO_REUSEPORT socket option.
ReusePort bool // ReadBufferCap is the maximum number of bytes that can be read from the peer when the readable event comes.
// The default value is 64KB, it can be reduced to avoid starving the subsequent connections.
// Note that ReadBufferCap will always be converted to the least power of two integer value greater than
// or equal to its real amount.
ReadBufferCap int // LockOSThread is used to determine whether each I/O event-loop is associated to an OS thread, it is useful when you
// need some kind of mechanisms like thread local storage, or invoke certain C libraries (such as graphics lib: GLib)
// that require thread-level manipulation via cgo, or want all I/O event-loops to actually run in parallel for a
// potential higher performance.
LockOSThread bool // Ticker indicates whether the ticker has been set up.
Ticker bool // TCPKeepAlive sets up a duration for (SO_KEEPALIVE) socket option.
TCPKeepAlive time.Duration // TCPNoDelay controls whether the operating system should delay
// packet transmission in hopes of sending fewer packets (Nagle's algorithm).
// The default is true (no delay), meaning that data is sent
// as soon as possible after a write operation.
TCPNoDelay TCPSocketOpt // SocketRecvBuffer sets the maximum socket receive buffer in bytes.
SocketRecvBuffer int // SocketSendBuffer sets the maximum socket send buffer in bytes.
SocketSendBuffer int // ICodec encodes and decodes TCP stream.
Codec ICodec // LogPath the local path where logs will be written, this is the easiest way to set up logging,
// gnet instantiates a default uber-go/zap logger with this given log path, you are also allowed to employ
// you own logger during the lifetime by implementing the following log.Logger interface.
// Note that this option can be overridden by the option Logger.
LogPath string // LogLevel indicates the logging level, it should be used along with LogPath.
LogLevel logging.Level // Logger is the customized logger for logging info, if it is not set,
// then gnet will use the default logger powered by go.uber.org/zap.
Logger logging.Logger
}
Look again. EventHandler
type EventHandler interface {
// OnInitComplete fires when the server is ready for accepting connections.
// The parameter server has information and various utilities.
OnInitComplete(server Server) (action Action) // OnShutdown fires when the server is being shut down, it is called right after
// all event-loops and connections are closed.
OnShutdown(server Server) // OnOpened fires when a new connection has been opened.
// The Conn c has information about the connection such as it's local and remote address.
// The parameter out is the return value which is going to be sent back to the peer.
// It is usually not recommended to send large amounts of data back to the peer in OnOpened.
// Note that the bytes returned by OnOpened will be sent back to the peer without being encoded.
OnOpened(c Conn) (out []byte, action Action) // OnClosed fires when a connection has been closed.
// The parameter err is the last known connection error.
OnClosed(c Conn, err error) (action Action) // PreWrite fires just before a packet is written to the peer socket, this event function is usually where
// you put some code of logging/counting/reporting or any fore operations before writing data to the peer.
PreWrite(c Conn) // AfterWrite fires right after a packet is written to the peer socket, this event function is usually where
// you put the []byte returned from React() back to your memory pool.
AfterWrite(c Conn, b []byte) // React fires when a socket receives data from the peer.
// Call c.Read() or c.ReadN(n) of Conn c to read incoming data from the peer.
// The parameter out is the return value which is going to be sent back to the peer.
// Note that the parameter packet returned from React() is not allowed to be passed to a new goroutine,
// as this []byte will be reused within event-loop after React() returns.
// If you have to use packet in a new goroutine, then you need to make a copy of buf and pass this copy
// to that new goroutine.
// Our business processing should be right packet Data processing in , It should be noted that , If business processing is put in a new goroutine In the implementation of , Then you need to copy packet Pass in a copy of
// Why exactly , The author didn't say , I guess it may be because the slice belongs to the reference type , new gorountine The life cycle of will affect the whole gnet The job of
React(packet []byte, c Conn) (out []byte, action Action) // Tick fires immediately after the server starts and will fire again
// following the duration specified by the delay return value.
Tick() (delay time.Duration, action Action)
}
The following is the official use method with blocking , Used pool
package main import (
"log"
"time" "github.com/panjf2000/gnet"
"github.com/panjf2000/gnet/pool"
) type echoServer struct {
*gnet.EventServer
pool *pool.WorkerPool
} func (es *echoServer) React(c gnet.Conn) (out []byte, action gnet.Action) {
data := append([]byte{}, c.Read()...)
c.ResetBuffer() // Use ants pool to unblock the event-loop.
_ = es.pool.Submit( func () {
time.Sleep(1 * time.Second)
c.AsyncWrite(data)
}) return
} func main() {
p := pool.NewWorkerPool()
defer p.Release() echo := &echoServer{pool: p}
log.Fatal(gnet.Serve(echo, "tcp://:9000" , gnet.WithMulticore(true)))
}
stay Ubuntu The upper pressure has been measured , A single machine is connected 6 Ten thousand nodes , It's easy , Of course , This connection does not have any business processing . Just one echo Test of . Be careful , stay Ubuntu During the test Need modification ulimite Value , Default 1024 Too small . modify ulimite Value method self search .
Now the problem is ,options Parameter is gnet.WithMulticore(true) when , stay windows When the upper pressure is measured , From the task manager ,gnet Created many threads , Even if all clients are disconnected , These threads are not reduced , I don't know why . I consulted the author in private , The author says ,windows The platform is only used for development and debugging , It can't be used in production .
gnet: A lightweight and high-performance Go Network framework More articles about using notes
- [ Open source ] gnet: A lightweight and high-performance Golang The network library
Github Home page https://github.com/panjf2000/gnet Welcome to watch ~~, It's still being updated , If you're interested star Let's watch in the dark . brief introduction gnet It's based on Ev ...
- Open source a simple and lightweight reactor Network framework
github https://github.com/sea-boat/net-reactor net-reactor it's a simple and easy net framework with ...
- Dapeng frame - Open source high performance distributed microservice framework
The nature of our company is new retail , The company also has a special framework group . These big bulls have developed a whole set of distributed microservice framework by themselves . We are also using this framework , There are many experiences . The framework is both Dapeng also ! Open source github Address :https://github ...
- Voovan Is a high-performance asynchronous network framework and HTTP(Java)
Voovan Is a high-performance asynchronous network framework and HTTP Server framework , Support at the same time HTTP Client crawl . Dynamic compilation support . Database access encapsulation and DateTime.String.Log. Reflection . Object tools . Flow operation . Document operation ...
- A lightweight distributed RPC frame --NettyRpc
1. background I've been searching for Netty and Zookeeper When you write about , I saw this article < Lightweight distributed RPC frame >, The author uses Zookeeper.Netty and Spring Write a lightweight distributed RPC ...
- How to use Netty Achieve a lightweight HTTP proxy server
Why would you want to pass Netty Construct a HTTP proxy server ? This is also the purpose of this article . It mainly comes from the solution in the daily development and testing process , A problem that has been bothering test students for a long time , Now let me elaborate on this issue . In daily life ...
- A lightweight distributed RPC frame — NettyRpc
The source of the original text is : Avanlu 1. background I've been searching for Netty and Zookeeper When you write about , I saw this article < Lightweight distributed RPC frame >, The author uses Zookeeper.Netty and Spring Wrote a ...
- Do it yourself , Develop lightweight , High performance http The server .
Preface http Protocol is the most widely used communication protocol on the Internet .web Communication is also based on http agreement : Corresponding c# For developers ,asp.net core Is the latest development web Application platform . Due to the recent development of a face recognition system , Impact on communication efficiency ...
- Focus on HTTP High performance and ease of use network library :Fslib.network library
Blog list page :http://blog.fishlee.net/tag/fslib-network/ original FSLib.Network library ( Focus on HTTP High performance and ease of use network library ) FSLib.Networ ...
- SSDB Is an open source high performance database server
SSDB Is an open source high performance database server , Use Google LevelDB As a storage engine , Support T Level of data , At the same time support similar Redis Medium zset and hash And so on , Under the condition of high performance and big data demand at the same time ...
Random recommendation
- iOS Wechat third party login implementation
iOS Wechat third party login implementation One . Third party login preparation for wechat access . Wechat login of mobile application is based on OAuth2.0 Wechat based on protocol standard OAuth2.0 Authorized login system . Wechat in progress OAuth2.0 Before authorized login access , On wechat ...
- 【.net Deep breathing 】 Use binary format to compress XML file
In quite a few cases , Let's write XML Files are written in text format by default , If XML Content is transmitted over the Internet , Or want to save space , Especially for XML In the case of large documents , We have to consider compressing as much as possible XML File size . XmlDicti ...
- codeforces 340A The Wall( Simple math problems )
The question : Input a,b,x,y,a,b Each is the number of steps ( Every time a brick , Brush once ), Some bricks were painted by two people at the same time , ask [x,y] How many bricks in the interval were painted by two people at the same time . analysis : Is o [x,y] How many of them can a,b The least common multiple of l ...
- ./wls1036_linux32.bin: /lib/ld-linux.so.2: bad ELF interpreter
[CentOS] Install the software :/lib/ld-linux.so.2: bad ELF interpreter solve Environmental Science : [[email protected] Downloads]$ uname -m ...
- HNOI2002 Turnover statistics (Splay Tree)
subject :http://www.lydsy.com/JudgeOnline/problem.php?id=1588 The question : Tiger Recently, he was promoted to sales manager by the company , After he took office, the first task assigned by the company was to count and ...
- Glad You Came hdu-6356(ST surface || Line segment tree )
The first one is a segment tree , Maintain the maximum value and the minimum value of the interval with two numbers , Then when updating, if the maximum value in my current interval is greater than what I get v Small , Then I will change this interval to v, If my minimum value is smaller than v Big , that v It's useless , Just skip , Then every time ...
- linux Out of memory ,swap Swap partition creation
Why swap root According to the Redhat The company's proposal ,Linux System swap The most suitable size of partition is physical memory 1-2 times . however Linux There are some software on swap There is a great demand for zoning , For example, you should follow Benefit implementation Oracle Database software , ...
- 【 note 】metasploit Penetration test devil camp - Information gathering
exploit Exploit code Encoder module : No killing . control help [cmd] msfcli Suitable for unified testing of a large number of systems in the network . Turn on the packet routing and forwarding function :/etc/sysctl.conf /etc/rc.loc ...
- openvswitch dpdk
author : Zhang hua Published in :2016-04-07 Copyright notice : You can reprint at will , When reprinting, please make sure to indicate the original source of the article, the author's information and this copyright notice in the form of hyperlinks ( http://blog.csdn.net/quqi99 ) Hardware requirements ...
- ( turn )extern Keyword two scenarios
The first scenario -- extern extern Keyword is used to declare variables and functions as external links , That is, the variable or function name is visible in other files . Variables or functions declared with them should be defined in another file or elsewhere in the same file . For example, statement :exter ...