Skip to content

Commit

Permalink
Merge pull request #668 from panjf2000/dev
Browse files Browse the repository at this point in the history
minor: v2.7.0
  • Loading branch information
panjf2000 authored Jan 4, 2025
2 parents c1270b4 + 1de2082 commit a7e541b
Show file tree
Hide file tree
Showing 78 changed files with 290 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ English | [中文](README_ZH.md)

`gnet` is an event-driven networking framework that is ultra-fast and lightweight. It is built from scratch by exploiting [epoll](https://man7.org/linux/man-pages/man7/epoll.7.html) and [kqueue](https://en.wikipedia.org/wiki/Kqueue) and it can achieve much higher performance with lower memory consumption than Go [net](https://golang.org/pkg/net/) in many specific scenarios.

`gnet` and [net](https://golang.org/pkg/net/) don't share the same philosophy about network programming. Thus, building network applications with `gnet` can be significantly different from building them with [net](https://golang.org/pkg/net/), and the philosophies can't be reconciled. There are other similar products written in other programming languages in the community, such as [libuv](https://github.com/libuv/libuv), [netty](https://github.com/netty/netty), [twisted](https://github.com/twisted/twisted), [tornado](https://github.com/tornadoweb/tornado), etc. which work in a similar pattern as `gnet` under the hood.
`gnet` and [net](https://golang.org/pkg/net/) don't share the same philosophy in network programming. Thus, building network applications with `gnet` can be significantly different from building them with [net](https://golang.org/pkg/net/), and the philosophies can't be reconciled. There are other similar products written in other programming languages in the community, such as [libuv](https://github.com/libuv/libuv), [netty](https://github.com/netty/netty), [twisted](https://github.com/twisted/twisted), [tornado](https://github.com/tornadoweb/tornado), etc. which work in a similar pattern as `gnet` under the hood.

`gnet` is not designed to displace the Go [net](https://golang.org/pkg/net/), but to create an alternative in the Go ecosystem for building performance-critical network services. As a result of which, `gnet` is not as comprehensive as Go [net](https://golang.org/pkg/net/), it provides only the core functionality (via a concise set of APIs) required by a network application and it doesn't plan on becoming a coverall networking framework, as I think Go [net](https://golang.org/pkg/net/) has done a good enough job in that area.

Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

`gnet` 是一个基于事件驱动的高性能和轻量级网络框架。这个框架是基于 [epoll](https://en.wikipedia.org/wiki/Epoll)[kqueue](https://en.wikipedia.org/wiki/Kqueue) 从零开发的,而且相比 Go [net](https://golang.org/pkg/net/),它能以更低的内存占用实现更高的性能。

`gnet`[net](https://golang.org/pkg/net/) 有着不一样的网络编程模式。因此,用 `gnet` 开发网络应用和用 [net](https://golang.org/pkg/net/) 开发区别很大,而且两者之间不可调和。社区里有其他同类的产品像是 [libuv](https://github.com/libuv/libuv), [netty](https://github.com/netty/netty), [twisted](https://github.com/twisted/twisted), [tornado](https://github.com/tornadoweb/tornado)`gnet` 的底层工作原理和这些框架非常类似。
`gnet`[net](https://golang.org/pkg/net/) 有着不一样的网络编程范式。因此,用 `gnet` 开发网络应用和用 [net](https://golang.org/pkg/net/) 开发区别很大,而且两者之间不可调和。社区里有其他同类的产品像是 [libuv](https://github.com/libuv/libuv), [netty](https://github.com/netty/netty), [twisted](https://github.com/twisted/twisted), [tornado](https://github.com/tornadoweb/tornado)`gnet` 的底层工作原理和这些框架非常类似。

`gnet` 不是为了取代 [net](https://golang.org/pkg/net/) 而生的,而是在 Go 生态中为开发者提供一个开发性能敏感的网络服务的替代品。也正因如此,`gnet` 在功能全面性上比不了 Go [net](https://golang.org/pkg/net/),它只会提供网络应用所需的最核心的功能和最精简的 APIs,而且 `gnet` 也并没有打算变成一个无所不包的网络框架,因为我觉得 Go [net](https://golang.org/pkg/net/) 在这方面已经做得足够好了。

Expand Down
6 changes: 3 additions & 3 deletions acceptor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
"github.com/panjf2000/gnet/v2/pkg/queue"
"github.com/panjf2000/gnet/v2/pkg/socket"
)

func (el *eventloop) accept0(fd int, _ netpoll.IOEvent, _ netpoll.IOFlags) error {
Expand Down
8 changes: 4 additions & 4 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import (
"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/math"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/math"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
"github.com/panjf2000/gnet/v2/pkg/queue"
"github.com/panjf2000/gnet/v2/pkg/socket"
)

// Client of gnet.
Expand Down
2 changes: 1 addition & 1 deletion connection_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
)

func (c *conn) processIO(_ int, filter netpoll.IOEvent, flags netpoll.IOFlags) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion connection_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
)

func (c *conn) processIO(_ int, ev netpoll.IOEvent, _ netpoll.IOFlags) error {
Expand Down
10 changes: 5 additions & 5 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/bs"
"github.com/panjf2000/gnet/v2/internal/gfd"
gio "github.com/panjf2000/gnet/v2/internal/io"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/pkg/bs"
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
gio "github.com/panjf2000/gnet/v2/pkg/io"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
"github.com/panjf2000/gnet/v2/pkg/queue"
"github.com/panjf2000/gnet/v2/pkg/socket"
)

type conn struct {
Expand Down
4 changes: 2 additions & 2 deletions engine_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (
"golang.org/x/sync/errgroup"

"github.com/panjf2000/gnet/v2/internal/gfd"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
"github.com/panjf2000/gnet/v2/pkg/queue"
)

type engine struct {
Expand Down
12 changes: 6 additions & 6 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (

"golang.org/x/sys/unix"

gio "github.com/panjf2000/gnet/v2/internal/io"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/queue"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
gio "github.com/panjf2000/gnet/v2/pkg/io"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
"github.com/panjf2000/gnet/v2/pkg/queue"
)

type eventloop struct {
Expand Down Expand Up @@ -238,11 +238,11 @@ func (el *eventloop) close(c *conn, err error) error {
if len(iov) > iovMax {
iov = iov[:iovMax]
}
if n, e := gio.Writev(c.fd, iov); e != nil {
n, err := gio.Writev(c.fd, iov)
if err != nil {
break
} else { //nolint:revive
_, _ = c.outboundBuffer.Discard(n)
}
_, _ = c.outboundBuffer.Discard(n)
}

c.release()
Expand Down
6 changes: 5 additions & 1 deletion gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package gnet implements a high-performance, lightweight, non-blocking,
// event-driven networking framework written in pure Go.
//
// Visit https://gnet.host/ for more details about gnet.
package gnet

import (
Expand All @@ -23,10 +27,10 @@ import (
"sync"
"time"

"github.com/panjf2000/gnet/v2/internal/math"
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
"github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/math"
)

// Action is an action that occurs after the completion of an event.
Expand Down
4 changes: 2 additions & 2 deletions listener_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/netpoll"
"github.com/panjf2000/gnet/v2/pkg/socket"
)

type listener struct {
Expand Down
2 changes: 1 addition & 1 deletion load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"hash/crc32"
"net"

"github.com/panjf2000/gnet/v2/internal/bs"
"github.com/panjf2000/gnet/v2/pkg/bs"
)

// LoadBalancing represents the type of load-balancing algorithm.
Expand Down
1 change: 1 addition & 0 deletions internal/bs/bs.go → pkg/bs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package bs provides a few handy bytes/string functions.
package bs

import (
Expand Down
1 change: 1 addition & 0 deletions pkg/buffer/elastic/elastic_ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package elastic implements an elastic ring-buffer.
package elastic

import (
Expand Down
1 change: 1 addition & 0 deletions pkg/buffer/linkedlist/linked_list_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package linkedlist implements a memory-reusable linked list of byte slices.
package linkedlist

import (
Expand Down
5 changes: 3 additions & 2 deletions pkg/buffer/ring/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

// Package ring implements a memory-efficient circular buffer.
package ring

import (
"errors"
"io"

"github.com/panjf2000/gnet/v2/internal/bs"
"github.com/panjf2000/gnet/v2/internal/math"
"github.com/panjf2000/gnet/v2/pkg/bs"
"github.com/panjf2000/gnet/v2/pkg/math"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
)

Expand Down
1 change: 1 addition & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package errors defines common errors for gnet.
package errors

import "errors"
Expand Down
18 changes: 18 additions & 0 deletions pkg/io/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2025 The Gnet Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Package io provides some handy network I/O functions.
package io
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions internal/math/math.go → pkg/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package math provides a few fast math functions.
package math

const (
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,25 @@ const (
WriteEvents = unix.EPOLLOUT
// ReadWriteEvents represents both readable and writeable events.
ReadWriteEvents = ReadEvents | WriteEvents
// ErrEvents represents exceptional events that occurred on the local side.
// ErrEvents represents exceptional events that occurred.
ErrEvents = unix.EPOLLERR | unix.EPOLLHUP
)

// IsReadEvent checks if the event is a read event.
func IsReadEvent(event IOEvent) bool {
return event&ReadEvents != 0
}

// IsWriteEvent checks if the event is a write event.
func IsWriteEvent(event IOEvent) bool {
return event&WriteEvents != 0
}

// IsErrorEvent checks if the event is an error event.
func IsErrorEvent(event IOEvent, _ IOFlags) bool {
return event&ErrEvents != 0
}

type eventList struct {
size int
events []epollevent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,31 @@ const (
MinPollEventsCap = 16
// MaxAsyncTasksAtOneTime is the maximum amount of asynchronous tasks that the event-loop will process at one time.
MaxAsyncTasksAtOneTime = 128
// ReadEvents represents readable events that are polled by kqueue.
ReadEvents = unix.EVFILT_READ
// WriteEvents represents writeable events that are polled by kqueue.
WriteEvents = unix.EVFILT_WRITE
// ReadWriteEvents represents both readable and writeable events.
ReadWriteEvents = ReadEvents | WriteEvents
// ErrEvents represents exceptional events that occurred.
ErrEvents = unix.EV_EOF | unix.EV_ERROR
)

// IsReadEvent checks if the event is a read event.
func IsReadEvent(event IOEvent) bool {
return event == ReadEvents
}

// IsWriteEvent checks if the event is a write event.
func IsWriteEvent(event IOEvent) bool {
return event == WriteEvents
}

// IsErrorEvent checks if the event is an error event.
func IsErrorEvent(_ IOEvent, flags IOFlags) bool {
return flags&ErrEvents != 0
}

type eventList struct {
size int
events []unix.Kevent_t
Expand Down
File renamed without changes.
Loading

0 comments on commit a7e541b

Please sign in to comment.