Skip to content

Commit

Permalink
Merge pull request #594 from panjf2000/dev
Browse files Browse the repository at this point in the history
patch: v2.5.1
  • Loading branch information
panjf2000 authored Apr 25, 2024
2 parents 5c043e6 + 4c3b84f commit e012f5b
Show file tree
Hide file tree
Showing 51 changed files with 429 additions and 354 deletions.
85 changes: 85 additions & 0 deletions .github/workflows/cross-compile-bsd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
name: Cross-compile for *BSD

on:
push:
branches:
- master
- dev
- 1.x
paths-ignore:
- '**.md'
- '**.yml'
- '**.yaml'
- '!.github/workflows/cross-compile-bsd.yml'
pull_request:
branches:
- master
- dev
- 1.x
paths-ignore:
- '**.md'
- '**.yml'
- '**.yaml'
- '!.github/workflows/cross-compile-bsd.yml'

env:
GO111MODULE: on
GOPROXY: "https://proxy.golang.org"

jobs:
build:
strategy:
fail-fast: false
matrix:
go: ['1.17', '1.21']
os:
- ubuntu-latest
name: Go ${{ matrix.go }} @ ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}

- name: Print Go environment
id: go-env
run: |
printf "Using go at: $(which go)\n"
printf "Go version: $(go version)\n"
printf "\n\nGo environment:\n\n"
go env
printf "\n\nSystem environment:\n\n"
env
# Calculate the short SHA1 hash of the git commit
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "GO_CACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT
- name: Cross-compiling for DragonFlyBSD
run: GOOS=dragonfly GOARCH=amd64 go build

- name: Cross-compiling for DragonFlyBSD -tags=poll_opt,gc_opt
run: GOOS=dragonfly GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for FreeBSD
run: GOOS=freebsd GOARCH=amd64 go build

- name: Cross-compiling for FreeBSD -tags=poll_opt,gc_opt
run: GOOS=freebsd GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for NetBSD
run: GOOS=netbsd GOARCH=amd64 go build

- name: Cross-compiling for NetBSD -tags=poll_opt,gc_opt
run: GOOS=netbsd GOARCH=amd64 go build -tags=poll_opt,gc_opt

- name: Cross-compiling for OpenBSD
run: GOOS=openbsd GOARCH=amd64 go build

- name: Cross-compiling for OpenBSD -tags=poll_opt,gc_opt
run: GOOS=openbsd GOARCH=amd64 go build -tags=poll_opt,gc_opt
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ English | [中文](README_ZH.md)
- [x] Efficient, reusable, and elastic memory buffer: (Elastic-)Ring-Buffer, Linked-List-Buffer and Elastic-Mixed-Buffer
- [x] Multiple protocols/IPC mechanisms: `TCP`, `UDP`, and `Unix Domain Socket`
- [x] Multiple load-balancing algorithms: `Round-Robin`, `Source-Addr-Hash`, and `Least-Connections`
- [x] Two event-driven mechanisms: `epoll` on **Linux** and `kqueue` on **FreeBSD/DragonFly/Darwin**
- [x] Flexible ticker event
- [x] Implementation of `gnet` Client
- [x] **Windows** platform support (For compatibility in development only, do not use it in production)
- [x] `gnet` client
- [x] Running on `Linux`, `macOS`, `Windows`, and *BSD: `Darwin`/`DragonFlyBSD`/`FreeBSD`/`NetBSD`/`OpenBSD`
- [x] **Edge-triggered** I/O support
- [x] Multiple network addresses binding
- [ ] **TLS** support
- [ ] [io_uring](https://kernel.dk/io_uring.pdf) support

***Windows version of `gnet` should only be used in development for developing and testing, it shouldn't be used in production.***

# 🎬 Getting started

`gnet` is available as a Go module and we highly recommend that you use `gnet` via [Go Modules](https://go.dev/blog/using-go-modules), with Go 1.11 Modules enabled (Go 1.11+), you can just simply add `import "github.com/panjf2000/gnet/v2"` to the codebase and run `go mod download/go mod tidy` or `go [build|run|test]` to download the necessary dependencies automatically.
Expand Down
7 changes: 4 additions & 3 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@
- [x] 高效、可重用而且自动伸缩的内存 buffer:(Elastic-)Ring-Buffer, Linked-List-Buffer and Elastic-Mixed-Buffer
- [x] 多种网络协议/IPC 机制:`TCP``UDP``Unix Domain Socket`
- [x] 多种负载均衡算法:`Round-Robin(轮询)``Source-Addr-Hash(源地址哈希)``Least-Connections(最少连接数)`
- [x] 两种事件驱动机制:**Linux** 里的 `epoll` 以及 **FreeBSD/DragonFly/Darwin** 里的 `kqueue`
- [x] 灵活的事件定时器
- [x] 实现 `gnet` 客户端
- [x] 支持 **Windows** 平台 (仅用于开发环境的兼容性,不要在生产环境中使用)
- [x] `gnet` 客户端支持
- [x] 支持 `Linux`, `macOS`, `Windows`*BSD 操作系统: `Darwin`/`DragonFlyBSD`/`FreeBSD`/`NetBSD`/`OpenBSD`
- [x] **Edge-triggered** I/O 支持
- [x] 多网络地址绑定
- [ ] **TLS** 支持
- [ ] [io_uring](https://kernel.dk/io_uring.pdf) 支持

***`gnet` 的 Windows 版本应该仅用于开发阶段的开发和测试,切勿用于生产环境***

# 🎬 开始

`gnet` 是一个 Go module,而且我们也强烈推荐通过 [Go Modules](https://go.dev/blog/using-go-modules) 来使用 `gnet`,在开启 Go Modules 支持(Go 1.11+)之后可以通过简单地在代码中写 `import "github.com/panjf2000/gnet/v2"` 来引入 `gnet`,然后执行 `go mod download/go mod tidy` 或者 `go [build|run|test]` 这些命令来自动下载所依赖的包。
Expand Down
4 changes: 2 additions & 2 deletions acceptor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin || windows
// +build linux freebsd dragonfly netbsd openbsd darwin windows
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || windows
// +build darwin dragonfly freebsd linux netbsd openbsd windows

package gnet

Expand Down
4 changes: 2 additions & 2 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions conn_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && !gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && !gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build !gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions conn_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions conn_matrix_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || linux || netbsd || openbsd) && gc_opt
// +build darwin dragonfly freebsd linux netbsd openbsd
// +build gc_opt

package gnet
Expand Down
4 changes: 2 additions & 2 deletions connection_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions engine_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
4 changes: 2 additions & 2 deletions eventloop_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package gnet

Expand Down
7 changes: 5 additions & 2 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,11 @@ func createListeners(addrs []string, opts ...Option) ([]*listener, *Options, err
// with the capability of load balancing, it's the equivalent of Linux's SO_REUSEPORT.
// Also note that DragonFlyBSD 3.6.0 extended SO_REUSEPORT to distribute workload to
// available sockets, which make it the same as Linux's SO_REUSEPORT.
// AF_LOCAL with SO_REUSEPORT enables duplicate address and port bindings without
// load balancing on Linux and *BSD. Therefore, disable it for Unix domain sockets.
//
// Despite the fact that SO_REUSEPORT can be set on a Unix domain socket
// via setsockopt() without reporting an error, SO_REUSEPORT is actually
// not supported for sockets of AF_UNIX. Thus, we avoid setting it on the
// Unix domain sockets.
goos := runtime.GOOS
if (options.Multicore || options.NumEventLoop > 1) && options.ReusePort &&
((goos != "linux" && goos != "dragonfly" && goos != "freebsd") || hasUnix) {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require (
github.com/stretchr/testify v1.8.4
github.com/valyala/bytebufferpool v1.0.0
go.uber.org/zap v1.21.0 // don't upgrade this one
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.19.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
4 changes: 2 additions & 2 deletions internal/io/io_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package io

Expand Down
4 changes: 2 additions & 2 deletions internal/netpoll/defs_bsd_32bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (freebsd || dragonfly || netbsd || openbsd || darwin) && (386 || arm || mips || mipsle)
// +build freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && (386 || arm || mips || mipsle)
// +build darwin dragonfly freebsd netbsd openbsd
// +build 386 arm mips mipsle

package netpoll
Expand Down
4 changes: 2 additions & 2 deletions internal/netpoll/defs_bsd_64bit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build (freebsd || dragonfly || netbsd || openbsd || darwin) && (amd64 || arm64 || ppc64 || ppc64le || mips64 || mips64le || riscv64)
// +build freebsd dragonfly netbsd openbsd darwin
//go:build (darwin || dragonfly || freebsd || netbsd || openbsd) && (amd64 || arm64 || ppc64 || ppc64le || mips64 || mips64le || riscv64)
// +build darwin dragonfly freebsd netbsd openbsd
// +build amd64 arm64 ppc64 ppc64le mips64 mips64le riscv64

package netpoll
Expand Down
7 changes: 2 additions & 5 deletions internal/netpoll/defs_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build linux || freebsd || dragonfly || netbsd || openbsd || darwin
// +build linux freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd

package netpoll

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// PollEventHandler is the callback for I/O events notified by the poller.
type PollEventHandler func(int, IOEvent, IOFlags) error

Expand Down
24 changes: 24 additions & 0 deletions internal/netpoll/defs_poller_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2024 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.

//go:build darwin || dragonfly || freebsd || openbsd
// +build darwin dragonfly freebsd openbsd

package netpoll

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// IOEvent is the integer type of I/O events on BSD's.
type IOEvent = int16
3 changes: 3 additions & 0 deletions internal/netpoll/defs_poller_epoll.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package netpoll

import "golang.org/x/sys/unix"

// IOFlags represents the flags of IO events.
type IOFlags = uint16

// IOEvent is the integer type of I/O events on Linux.
type IOEvent = uint32

Expand Down
7 changes: 2 additions & 5 deletions internal/netpoll/defs_poller_kqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build freebsd || dragonfly || netbsd || openbsd || darwin
// +build freebsd dragonfly netbsd openbsd darwin
//go:build darwin || dragonfly || freebsd || netbsd || openbsd
// +build darwin dragonfly freebsd netbsd openbsd

package netpoll

import "golang.org/x/sys/unix"

// IOEvent is the integer type of I/O events on BSD's.
type IOEvent = int16

const (
// InitPollEventsCap represents the initial capacity of poller event-list.
InitPollEventsCap = 64
Expand Down
Loading

0 comments on commit e012f5b

Please sign in to comment.