Skip to content

Commit

Permalink
feat: support more functional options for client
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 24, 2021
1 parent f7a3ada commit 4db46da
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
25 changes: 24 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net"
"sync"
"syscall"
"time"

"golang.org/x/sys/unix"

Expand Down Expand Up @@ -75,7 +76,7 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
el.ln = svr.ln
el.svr = svr
el.poller = p
el.buffer = make([]byte, 16*1024)
el.buffer = make([]byte, options.ReadBufferCap)
el.connections = make(map[int]*conn)
el.eventHandler = eventHandler
cli.el = el
Expand Down Expand Up @@ -142,6 +143,28 @@ func (cli *Client) Dial(network, address string) (Conn, error) {
return nil, e
}

opts := cli.el.svr.opts
if opts.TCPNoDelay == TCPNoDelay {
if err = socket.SetNoDelay(DupFD, 1); err != nil {
return nil, err
}
}
if opts.TCPKeepAlive > 0 {
if err = socket.SetKeepAlive(DupFD, int(opts.TCPKeepAlive/time.Second)); err != nil {
return nil, err
}
}
if opts.SocketSendBuffer > 0 {
if err = socket.SetSendBuffer(DupFD, opts.SocketSendBuffer); err != nil {
return nil, err
}
}
if opts.SocketRecvBuffer > 0 {
if err = socket.SetRecvBuffer(DupFD, opts.SocketRecvBuffer); err != nil {
return nil, err
}
}

var (
sockAddr unix.Sockaddr
gc Conn
Expand Down
40 changes: 22 additions & 18 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,41 @@ const (

// Options are configurations for the gnet application.
type Options struct {
// ================================== Options for only server-side ==================================

// 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 bool

// 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

// 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 be always converted to the least power of two integer value greater than
// or equal to its real amount.
ReadBufferCap int
// NumEventLoop is set up to start the given number of event-loop goroutine.
// Note: Setting up NumEventLoop will override Multicore.
NumEventLoop int

// LB represents the load-balancing algorithm used when assigning new connections.
LB LoadBalancing

// NumEventLoop is set up to start the given number of event-loop goroutine.
// Note: Setting up NumEventLoop will override Multicore.
NumEventLoop int
// 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

// ReuseAddr indicates whether to set up the SO_REUSEADDR socket option.
ReuseAddr bool
// ============================= Options for both server-side and client-side =============================

// 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
Expand All @@ -86,7 +90,7 @@ type Options struct {
// 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.
// as soon as possible after a write operation.
TCPNoDelay TCPSocketOpt

// SocketRecvBuffer sets the maximum socket receive buffer in bytes.
Expand Down

0 comments on commit 4db46da

Please sign in to comment.