Skip to content

Commit

Permalink
feat: export the core internal packages
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jan 4, 2025
1 parent 042e5ec commit 61e3a33
Show file tree
Hide file tree
Showing 76 changed files with 256 additions and 73 deletions.
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
6 changes: 3 additions & 3 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
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 @@ -44,6 +44,21 @@ const (
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 on the local side.
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.
108 changes: 108 additions & 0 deletions pkg/netpoll/netpoll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 netpoll provides a portable event-driven interface for network I/O.
The underlying facility of event notification is OS-specific:
- epoll on Linux - https://man7.org/linux/man-pages/man7/epoll.7.html
- kqueue on *BSD/Darwin - https://man.freebsd.org/cgi/man.cgi?kqueue
With the help of the netpoll package, you can easily build your own high-performance
event-driven network applications based on epoll/kqueue.
The Poller represents the event notification facility whose backend is epoll or kqueue.
The OpenPoller function creates a new Poller instance:
poller, err := netpoll.OpenPoller()
if err != nil {
// handle error
}
defer poller.Close()
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9090")
if err != nil {
// handle error
}
c, err := net.DialTCP("tcp", nil, addr)
if err != nil {
// handle error
}
f, err := c.File()
if err != nil {
// handle error
}
closeClient := func() {
c.Close()
f.Close()
}
The PollAttachment consists of a file descriptor and its callback function.
PollAttachment is used to register a file descriptor with the Poller.
The callback function is called when an event occurs on the file descriptor:
pa := netpoll.PollAttachment{
FD: int(f.Fd()),
Callback: func(i int, event netpoll.IOEvent, flags netpoll.IOFlags) error {
// For epoll
if event&netpoll.ErrEvents != 0 {
closeClient()
return errors.ErrEngineShutdown
}
// For kqueue
//if flags&netpoll.ErrEvents != 0 {
// closeClient()
// return errors.ErrEngineShutdown
//}
if event&netpoll.ReadEvents != 0 {
buf := make([]byte, 64)
_, err := c.Read(buf)
if err != nil {
closeClient()
return errors.ErrEngineShutdown
}
// Process the data...
}
if event&netpoll.WriteEvents != 0 {
// Write data to the connection...
_, err := c.Write([]byte("hello"))
if err != nil {
closeClient()
return errors.ErrEngineShutdown
}
}
return nil
}}
if err := poller.AddReadWrite(&pa, false); err != nil {
// handle error
}
The Poller.Polling function starts the event loop monitoring file descriptors and
waiting for I/O events to occur:
poller.Polling(func(fd int, event netpoll.IOEvent, flags netpoll.IOFlags) error {
return pa.Callback(fd, event, flags)
})
*/
package netpoll
Loading

0 comments on commit 61e3a33

Please sign in to comment.