Skip to content

Commit

Permalink
feat(golang-lint): fix golang-lint warn
Browse files Browse the repository at this point in the history
  • Loading branch information
Softwarekang committed Apr 9, 2023
1 parent 9be9815 commit 1826a80
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 20 deletions.
8 changes: 4 additions & 4 deletions net/connection/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

"github.com/Softwarekang/knetty/net/poll"
"github.com/Softwarekang/knetty/pkg/buffer"
mnet "github.com/Softwarekang/knetty/pkg/net"
msyscall "github.com/Softwarekang/knetty/pkg/syscall"
netutil "github.com/Softwarekang/knetty/pkg/net"
syscallutil "github.com/Softwarekang/knetty/pkg/syscall"
)

// TcpConn tcp connection implements the Connection interface.
Expand All @@ -50,13 +50,13 @@ func NewTcpConn(conn net.Conn) (*TcpConn, error) {
}

// get real fd from conn
fd, err := mnet.ResolveConnFileDesc(conn)
fd, err := netutil.ResolveConnFileDesc(conn)
if err != nil {
return nil, err
}

// set conn no block
_ = msyscall.SetConnectionNoBlock(fd)
_ = syscallutil.SetConnectionNoBlock(fd)
return &TcpConn{
knettyConn: knettyConn{
id: idBuilder.Inc(),
Expand Down
10 changes: 5 additions & 5 deletions net/poll/poller_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"unsafe"

"github.com/Softwarekang/knetty/pkg/log"
msyscall "github.com/Softwarekang/knetty/pkg/syscall"
syscallutil "github.com/Softwarekang/knetty/pkg/syscall"
)

// Epoll poller for epoll.
Expand Down Expand Up @@ -58,22 +58,22 @@ func (e *Epoll) Register(netFd *NetFileDesc, eventType EventType) error {
op, events = syscall.EPOLL_CTL_DEL, syscall.EPOLLIN
case OnceWrite:
// once write use et trigger
op, events = syscall.EPOLL_CTL_ADD, uint32(msyscall.EpollET|syscall.EPOLLOUT)
op, events = syscall.EPOLL_CTL_ADD, uint32(syscallutil.EpollET|syscall.EPOLLOUT)
default:
return fmt.Errorf("epoll not support the event type:%d", int(eventType))
}

return msyscall.EpollCtl(e.fd, op, netFd.FD, &msyscall.EpollEvent{
return syscallutil.EpollCtl(e.fd, op, netFd.FD, &syscallutil.EpollEvent{
Events: events | syscall.EPOLLHUP | syscall.EPOLLRDHUP | syscall.EPOLLERR,
Udata: *(*[8]byte)(unsafe.Pointer(&netFd)),
})
}

// Wait implements Poll.
func (e *Epoll) Wait() error {
events := make([]msyscall.EpollEvent, 1024)
events := make([]syscallutil.EpollEvent, 1024)
for {
n, err := msyscall.EpollWait(e.fd, events, -1)
n, err := syscallutil.EpollWait(e.fd, events, -1)
if err != nil {
if err == syscall.EINTR {
continue
Expand Down
3 changes: 2 additions & 1 deletion pkg/buffer/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package buffer

import (
"github.com/Softwarekang/knetty/pkg/utils"
"syscall"

"github.com/Softwarekang/knetty/pkg/utils"

"github.com/Softwarekang/knetty/pkg/math"
msycall "github.com/Softwarekang/knetty/pkg/syscall"
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Package math common mathematical methods
package math

// Max return the larger of the two numbers, a and b.
func Max(a, b int) int {
if a < b {
return b
Expand All @@ -25,6 +26,7 @@ func Max(a, b int) int {
return a
}

// Min return the smaller of the two numbers, a and b.
func Min(a, b int) int {
if a < b {
return a
Expand Down
2 changes: 1 addition & 1 deletion pkg/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"syscall"
)

// ResolveConnFileDesc resolve tcp、udp fd
// ResolveConnFileDesc get the real file descriptor of net.conn.
func ResolveConnFileDesc(conn net.Conn) (int, error) {
if conn == nil {
return 0, errors.New("conn is nil")
Expand Down
10 changes: 6 additions & 4 deletions pkg/pool/ringbuffer/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func init() {
size := 1 << i
ringBufferPool[size] = &sync.Pool{
New: func() any {
return make([]byte, size)
buf := make([]byte, size)
return &buf
},
}
}
Expand All @@ -59,8 +60,8 @@ func Get(size int) []byte {
}

size = utils.AdjustNToPowerOfTwo(size)
buf := ringBufferPool[size].Get().([]byte)
return buf[:size]
bufPointer := ringBufferPool[size].Get().(*[]byte)
return (*bufPointer)[:size]
}

// Put if buf is legal then it will be put into the buffer pool.
Expand All @@ -73,5 +74,6 @@ func Put(buf []byte) {
if c > maxAllocSize || c < minAllocSize {
return
}
ringBufferPool[c].Put(buf[:0])
releasedBuf := buf[:0]
ringBufferPool[c].Put(&releasedBuf)
}
2 changes: 2 additions & 0 deletions pkg/pool/ringbuffer/ringbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func TestPut(t *testing.T) {
bytes1 := Get(1)
bytes1[1] = '1'
Put(bytes1)
// get released buf
oldBytes := Get(1)
// check index 1 value
assert.Equal(t, uint8('1'), oldBytes[1])
}
6 changes: 1 addition & 5 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,5 @@ func AdjustNToPowerOfTwo(n int) int {

// IsPowerOfTwo check whether n is 2 to the nth power.
func IsPowerOfTwo(n int) bool {
if (n & (n - 1)) != 0 {
return false
}

return true
return n&(n-1) == 0
}

0 comments on commit 1826a80

Please sign in to comment.