Skip to content

Commit

Permalink
Merge pull request #10 from pawelgaczynski/feature-double-buffer
Browse files Browse the repository at this point in the history
Feature double buffer
  • Loading branch information
pawelgaczynski authored May 7, 2023
2 parents 1f2d07e + 99fb193 commit 68732ec
Show file tree
Hide file tree
Showing 100 changed files with 8,935 additions and 1,783 deletions.
192 changes: 132 additions & 60 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ run:
timeout: 3m

modules-download-mode: readonly
go: "1.17"
go: "1.18"

linters-settings:
cyclop:
Expand All @@ -29,6 +29,9 @@ linters-settings:
underef:
skipRecvDeref: false

godot:
scope: declarations

gomnd:
ignored-functions:
- os.Chmod
Expand All @@ -45,6 +48,7 @@ linters-settings:
- strconv.ParseFloat
- strconv.ParseInt
- strconv.ParseUint
- doubleSize

gomodguard:
blocked:
Expand Down Expand Up @@ -76,65 +80,139 @@ linters-settings:
nestif:
min-complexity: 8

nolintlint:
allow-no-explanation: [ funlen, gocognit, lll, unused, varcheck ]
require-explanation: true
require-specific: true

rowserrcheck:
packages:
- github.com/jmoiron/sqlx

tenv:
all: true

varnamelen:
ignore-names:
- fd
- err
- vm
- gc
- wg
- lb
- ip
ignore-decls:
- n int
- n uint32
- i int
- ip net.IP
- ln *listener
- c gain.Conn

wrapcheck:
ignoreSigs:
- .Errorf(
- os.NewSyscallError(
- syscall.Errno(
- unix.Socket(
- ErrIsEmpty
- .Wrapf(

ignoreSigRegexps:
- errors.Error.*\(

wsl:
allow-cuddle-declarations: false
force-err-cuddling: true
force-case-trailing-whitespace: 2
allow-assign-and-anything: true

linters:
disable-all: true

enable:
- errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
- gosimple # Linter for Go source code that specializes in simplifying a code
- ineffassign # Detects when assignments to existing variables are not used
- staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
- unused # Checks Go code for unused constants, variables, functions and types
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
- bidichk # Checks for dangerous unicode character sequences
- cyclop # checks function and package cyclomatic complexity
- dupl # Tool for code clone detection
- durationcheck # check for two durations multiplied together
- errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error.
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
- exhaustive # check exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- forbidigo # Forbids identifiers
- funlen # Tool for detection of long functions
- gochecknoinits # Checks that no init functions are present in Go code
- gocognit # Computes and checks the cognitive complexity of functions
- goconst # Finds repeated strings that could be replaced by a constant
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
- gocyclo # Computes and checks the cyclomatic complexity of functions
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt.
- gomnd # An analyzer to detect magic numbers.
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
- goprintffuncname # Checks that printf-like functions are named with f at the end
- gosec # Inspects source code for security problems
- lll # Reports long lines
- makezero # Finds slice declarations with non-zero initial length
- nakedret # Finds naked returns in functions greater than a specified function length
- nestif # Reports deeply nested if statements
- nilnil # Checks that there is no simultaneous return of nil error and an invalid value.
- nonamedreturns # Reports all named returns
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL.
- predeclared # find code that shadows one of Go's predeclared identifiers
- promlinter # Check Prometheus metrics naming via promlint
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
- stylecheck # Stylecheck is a replacement for golint
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
- testpackage # linter that makes you use a separate _test package
- unconvert # Remove unnecessary type conversions
- whitespace # Tool for detection of leading and trailing whitespace
- errcheck # errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases [fast: false, auto-fix: false]
- gosimple # (megacheck) Linter for Go source code that specializes in simplifying code [fast: false, auto-fix: false]
- govet # (vet, vetshadow) Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
- ineffassign # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- staticcheck # (megacheck) It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint. [fast: false, auto-fix: false]
- typecheck # Like the front-end of a Go compiler, parses and type-checks Go code [fast: false, auto-fix: false]
- unused # (megacheck) Checks Go code for unused constants, variables, functions and types [fast: false, auto-fix: false]
- asasalint # check for pass []any as any in variadic func(...any) [fast: false, auto-fix: false]
- asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers [fast: true, auto-fix: false]
- bidichk # Checks for dangerous unicode character sequences [fast: true, auto-fix: false]
- bodyclose # checks whether HTTP response body is closed successfully [fast: false, auto-fix: false]
- containedctx # containedctx is a linter that detects struct contained context.Context field [fast: true, auto-fix: false]
- contextcheck # check whether the function uses a non-inherited context [fast: false, auto-fix: false]
- cyclop # checks function and package cyclomatic complexity [fast: false, auto-fix: false]
- decorder # check declaration order and count of types, constants, variables and functions [fast: true, auto-fix: false]
- depguard # Go linter that checks if package imports are in a list of acceptable packages [fast: true, auto-fix: false]
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) [fast: true, auto-fix: false]
- dupl # Tool for code clone detection [fast: true, auto-fix: false]
- dupword # checks for duplicate words in the source code [fast: true, auto-fix: true]
- durationcheck # check for two durations multiplied together [fast: false, auto-fix: false]
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted. [fast: false, auto-fix: false]
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`. [fast: false, auto-fix: false]
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13. [fast: false, auto-fix: false]
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds [fast: false, auto-fix: false]
- exhaustive # check exhaustiveness of enum switch statements [fast: false, auto-fix: false]
- exportloopref # checks for pointers to enclosing loop variables [fast: false, auto-fix: false]
- forbidigo # Forbids identifiers [fast: false, auto-fix: false]
- forcetypeassert # finds forced type assertions [fast: true, auto-fix: false]
- funlen # Tool for detection of long functions [fast: true, auto-fix: false]
- gci # Gci controls Go package import order and makes it always deterministic. [fast: true, auto-fix: false]
- ginkgolinter # enforces standards of using ginkgo and gomega [fast: false, auto-fix: false]
- gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid. [fast: true, auto-fix: false]
- gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false]
- gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false]
- goconst # Finds repeated strings that could be replaced by a constant [fast: true, auto-fix: false]
- gocritic # Provides diagnostics that check for bugs, performance and style issues. [fast: false, auto-fix: false]
- gocyclo # Computes and checks the cyclomatic complexity of functions [fast: true, auto-fix: false]
- godot # Check if comments end in a period [fast: true, auto-fix: true]
- godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false]
- goerr113 # Go linter to check the errors handling expressions [fast: false, auto-fix: false]
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification [fast: true, auto-fix: true]
- gofumpt # Gofumpt checks whether code was gofumpt-ed. [fast: true, auto-fix: true]
- goheader # Checks is file header matches to pattern [fast: true, auto-fix: false]
- goimports # Check import statements are formatted according to the 'goimport' command. Reformat imports in autofix mode. [fast: true, auto-fix: true]
- gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false]
- gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod. [fast: true, auto-fix: false]
- gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. [fast: true, auto-fix: false]
- goprintffuncname # Checks that printf-like functions are named with `f` at the end [fast: true, auto-fix: false]
- gosec # (gas) Inspects source code for security problems [fast: false, auto-fix: false]
- grouper # An analyzer to analyze expression groups. [fast: true, auto-fix: false]
- importas # Enforces consistent import aliases [fast: false, auto-fix: false]
- interfacebloat # A linter that checks the number of methods inside an interface. [fast: true, auto-fix: false]
- lll # Reports long lines [fast: true, auto-fix: false]
- loggercheck # (logrlint) Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). [fast: false, auto-fix: false]
- maintidx # maintidx measures the maintainability index of each function. [fast: true, auto-fix: false]
- makezero # Finds slice declarations with non-zero initial length [fast: false, auto-fix: false]
- misspell # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
- musttag # enforce field tags in (un)marshaled structs [fast: false, auto-fix: false]
- nakedret # Finds naked returns in functions greater than a specified function length [fast: true, auto-fix: false]
- nestif # Reports deeply nested if statements [fast: true, auto-fix: false]
- nilerr # Finds the code that returns nil even if it checks that the error is not nil. [fast: false, auto-fix: false]
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value. [fast: false, auto-fix: false]
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity [fast: true, auto-fix: false]
- noctx # noctx finds sending http request without context.Context [fast: false, auto-fix: false]
- nonamedreturns # Reports all named returns [fast: false, auto-fix: false]
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL. [fast: true, auto-fix: false]
- prealloc # Finds slice declarations that could potentially be pre-allocated [fast: true, auto-fix: false]
- predeclared # find code that shadows one of Go's predeclared identifiers [fast: true, auto-fix: false]
- promlinter # Check Prometheus metrics naming via promlint [fast: true, auto-fix: false]
- reassign # Checks that package variables are not reassigned [fast: false, auto-fix: false]
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint. [fast: false, auto-fix: false]
- rowserrcheck # checks whether Err of rows is checked successfully [fast: false, auto-fix: false]
- sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. [fast: false, auto-fix: false]
- stylecheck # Stylecheck is a replacement for golint [fast: false, auto-fix: false]
- tagliatelle # Checks the struct tags. [fast: true, auto-fix: false]
- tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 [fast: false, auto-fix: false]
- testableexamples # linter checks if examples are testable (have an expected output) [fast: true, auto-fix: false]
- thelper # thelper detects Go test helpers without t.Helper() call and checks the consistency of test helpers [fast: false, auto-fix: false]
- tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes [fast: false, auto-fix: false]
- unconvert # Remove unnecessary type conversions [fast: false, auto-fix: false]
- unparam # Reports unused function parameters [fast: false, auto-fix: false]
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. [fast: true, auto-fix: false]
- varnamelen # checks that the length of a variable's name matches its scope [fast: false, auto-fix: false]
- wastedassign # wastedassign finds wasted assignment statements. [fast: false, auto-fix: false]
- whitespace # Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
- wrapcheck # Checks that errors returned from external packages are wrapped [fast: false, auto-fix: false]
- wsl # Whitespace Linter - Forces you to use empty lines! [fast: true, auto-fix: false]

issues:
max-same-issues: 50
Expand All @@ -148,14 +226,8 @@ issues:
linters: [ gocritic ]
- source: "^\\s+if _, ok := err\\.\\([^.]+\\.InternalError\\); ok {"
linters: [ errorlint ]
- path: "iouring/(completion|opcode|register|setup|submission).go"
linters: [ unused ]
- path: "_test\\.go"
linters:
- bodyclose
- dupl
- funlen
- goconst
- gosec
- noctx
- wrapcheck
linters: [ wrapcheck, goerr113, funlen, cognitive, gocognit, nestif, makezero, unparam ]
- linters:
- typecheck
text: "could not load export data"
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ Gain requires Go 1.18+
- [x] Lock-free
- [ ] Protocols
- [x] TCP
- [ ] UDP
- [x] UDP
- [ ] Unix Domain Socket
- [ ] Load balancing
- [x] Round robin
- [x] Least connection
- [ ] Source IP hash
- [x] Source IP hash
- [ ] Support for custom implementations
- [ ] Support for read and write deadlines
- [ ] Further io_uring optimizations
- [ ] More flexible connection buffer
- [ ] Documentation
- [x] More flexible connection buffer
- [x] Documentation
- [x] Support for kernels older than 5.15
- [ ] Support for Windows - IoRing ([documentation](https://learn.microsoft.com/en-us/windows/win32/api/ioringapi/))

Expand Down Expand Up @@ -187,8 +189,6 @@ Project Link: [https://github.com/pawelgaczynski/gain](https://github.com/pawelg

## Relevant Articles

Use this space to list resources you find helpful and would like to give credit to. I've included a few of my favorites to kick things off!

* [Lord of the io_uring](https://unixism.net/loti/what_is_io_uring.html)
* [Ringing in a new asynchronous I/O API](https://lwn.net/Articles/776703/)
* [The rapid growth of io_uring](https://lwn.net/Articles/810414/)
Expand Down
78 changes: 57 additions & 21 deletions acceptor.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,81 @@
// Copyright (c) 2023 Paweł Gaczyński
//
// 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 gain

import (
"fmt"
"net"
"syscall"
"unsafe"

"github.com/pawelgaczynski/gain/iouring"
"github.com/pawelgaczynski/gain/pkg/errors"
"github.com/pawelgaczynski/gain/pkg/socket"
)

type acceptor struct {
ring *iouring.Ring
socket int
clientAddrPointer uintptr
clientLenPointer uint64
connectionPool *connectionPool
fd int
clientAddr *syscall.RawSockaddrAny
clientLenPointer *uint32
connectionManager *connectionManager
}

func (a *acceptor) addAcceptRequest() (*iouring.SubmissionQueueEntry, error) {
func (a *acceptor) addAcceptRequest() error {
entry, err := a.ring.GetSQE()
if err != nil {
return nil, err
return fmt.Errorf("error getting SQE: %w", err)
}
entry.PrepareAccept(int(uintptr(a.socket)), a.clientAddrPointer, a.clientLenPointer, 0)
entry.UserData = acceptDataFlag | uint64(a.socket)
return entry, err

entry.PrepareAccept(
a.fd, uintptr(unsafe.Pointer(a.clientAddr)), uint64(uintptr(unsafe.Pointer(a.clientLenPointer))), 0)
entry.UserData = acceptDataFlag | uint64(a.fd)

return nil
}

func (a *acceptor) addAcceptConnRequest() (*iouring.SubmissionQueueEntry, error) {
entry, err := a.addAcceptRequest()
func (a *acceptor) addAcceptConnRequest() error {
err := a.addAcceptRequest()
if err != nil {
return nil, err
return err
}

conn := a.connectionManager.getFd(a.fd)
if conn == nil {
return errors.ErrConnectionIsMissing
}
conn, err := a.connectionPool.get(a.socket)
conn.state = connAccept

return nil
}

func (a *acceptor) lastClientAddr() (net.Addr, error) {
addr, err := anyToSockaddr(a.clientAddr)
if err != nil {
return nil, err
}
conn.state = connAccept
return entry, nil

return socket.SockaddrToTCPOrUnixAddr(addr), nil
}

func newAcceptor(ring *iouring.Ring, connectionPool *connectionPool) *acceptor {
a := &acceptor{
ring: ring,
connectionPool: connectionPool,
func newAcceptor(ring *iouring.Ring, connectionManager *connectionManager) *acceptor {
acceptor := &acceptor{
ring: ring,
connectionManager: connectionManager,
}
a.clientAddrPointer, a.clientLenPointer = createClientAddr()
return a
acceptor.clientAddr, acceptor.clientLenPointer = createClientAddr()

return acceptor
}
Loading

0 comments on commit 68732ec

Please sign in to comment.