Skip to content

Commit

Permalink
refactor: replace all interface{} with any in go1.18 (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: longyue0521 <[email protected]>
  • Loading branch information
longyue0521 authored Nov 29, 2023
1 parent e2b4b5a commit ed8bb2a
Show file tree
Hide file tree
Showing 22 changed files with 85 additions and 85 deletions.
10 changes: 5 additions & 5 deletions extensions/websocket/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type conn struct {
// Note: This lock does not guarantee concurrent safety when reading message.
mu sync.Mutex
raw rawConnection
metaData interface{}
metaData any
role ws.State // ws.StateServerSide or ws.StateClientSide.
reader io.Reader // connection-wise message type reader for Read.
messageType MessageType // connection-wise message type for Read/Write.
Expand All @@ -56,9 +56,9 @@ type rawConnection interface {
// SetIdleTimeout sets the idle timeout to close connection.
SetIdleTimeout(d time.Duration) error
// SetMetaData sets meta data. Through this method, users can bind some custom data to a connection.
SetMetaData(interface{})
SetMetaData(any)
// GetMetaData gets meta data.
GetMetaData() interface{}
GetMetaData() any
}

// rawConn wraps tls.Conn to provide a pseudo Writev implementation.
Expand Down Expand Up @@ -296,12 +296,12 @@ func (w *writeCloser) Close() error {
}

// SetMetaData sets meta data.
func (c *conn) SetMetaData(m interface{}) {
func (c *conn) SetMetaData(m any) {
c.metaData = m
}

// GetMetaData gets meta data.
func (c *conn) GetMetaData() interface{} {
func (c *conn) GetMetaData() any {
return c.metaData
}

Expand Down
4 changes: 2 additions & 2 deletions extensions/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ type Conn interface {
// A finished message write should end with writer.Close().
NextMessageWriter(MessageType) (io.WriteCloser, error)
// SetMetaData sets metadata. Through this method, users can bind some custom data to a connection.
SetMetaData(interface{})
SetMetaData(any)
// GetMetaData gets meta data.
GetMetaData() interface{}
GetMetaData() any
// Subprotocol returns the negotiated protocol for the connection.
Subprotocol() string
// SetPingHandler sets customized Ping frame handler.
Expand Down
6 changes: 3 additions & 3 deletions internal/asynctimer/asynctimer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ func Stop() {
}

// Callback is asynchronous callback function when timer expired.
type Callback func(data interface{})
type Callback func(data any)

// Timer is an async timer.
type Timer struct {
data interface{}
data any
expiredHandle Callback
begin atomic.Time
circle int
Expand All @@ -81,7 +81,7 @@ type Timer struct {
// NewTimer creates an async timer with data and expiredHandle function. After timeout
// time, the timer will be expired, and expiredHandle will be called with argument data.
// Note that the timer becomes effective after having been added to time wheel.
func NewTimer(data interface{}, expiredHandle Callback, timeout time.Duration) *Timer {
func NewTimer(data any, expiredHandle Callback, timeout time.Duration) *Timer {
t := &Timer{
data: data,
expiredHandle: expiredHandle,
Expand Down
2 changes: 1 addition & 1 deletion internal/asynctimer/asynctimer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type testWrapper struct {
isHandled bool
}

var expireHandle = func(data interface{}) {
var expireHandle = func(data any) {
t, ok := data.(*testWrapper)
if !ok {
return
Expand Down
2 changes: 1 addition & 1 deletion internal/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var (
)

var bufferPool = sync.Pool{
New: func() interface{} {
New: func() any {
return &Buffer{}
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/buffer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
)

var nodePool = sync.Pool{
New: func() interface{} {
New: func() any {
return &node{}
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/mcache/mcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var caches [maxSize]sync.Pool
func init() {
for i := 0; i < maxSize; i++ {
size := 1 << i
caches[i].New = func() interface{} {
caches[i].New = func() any {
s := make([]byte, 0, size)
return s
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cache/systype/systype.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type IOVECWrapper struct {
}

var iovecPool sync.Pool = sync.Pool{
New: func() interface{} {
New: func() any {
return &IOVECWrapper{
iovec: make([]unix.Iovec, 0, MaxLen),
}
Expand Down Expand Up @@ -81,7 +81,7 @@ type IOData struct {
}

var ioDataPool sync.Pool = sync.Pool{
New: func() interface{} {
New: func() any {
return &IOData{
D: make([][]byte, 0, MaxLen),
}
Expand Down Expand Up @@ -122,7 +122,7 @@ var (
)

func init() {
mmsghdrsPool.New = func() interface{} {
mmsghdrsPool.New = func() any {
mmsghdrs := make([]MMsghdr, MaxLen)
return mmsghdrs
}
Expand Down
4 changes: 2 additions & 2 deletions internal/netutil/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

// GetFD returns the integer Unix file descriptor referencing the tcp/udp socket.
func GetFD(socket interface{}) (int, error) {
func GetFD(socket any) (int, error) {
conn, ok := socket.(syscall.Conn)
if !ok {
return -1, fmt.Errorf("type %T doesn't implement syscall.Conn interface", socket)
Expand All @@ -49,7 +49,7 @@ func GetFD(socket interface{}) (int, error) {
}

// DupFD duplicates file descriptor and returns the new fd.
func DupFD(socket interface{}) (int, error) {
func DupFD(socket any) (int, error) {
var f *os.File
var err error
switch conn := socket.(type) {
Expand Down
8 changes: 4 additions & 4 deletions internal/poller/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ type Desc struct {
next *Desc
poller Poller
index int32
Data interface{}
Data any

// Desc provides three callbacks for fd's reading, writing or hanging events.
OnRead func(data interface{}, ioData *iovec.IOData) error
OnWrite func(data interface{}) error
OnHup func(data interface{})
OnRead func(data any, ioData *iovec.IOData) error
OnWrite func(data any) error
OnHup func(data any)

// FD is the file descriptor that will be monitored by poller.
FD int
Expand Down
6 changes: 3 additions & 3 deletions internal/poller/poller_epoll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ func TestNormal(t *testing.T) {
pollDesc.FD = eventFD
pollDesc.Data = 1
ch := make(chan struct{}, 1)
pollDesc.OnRead = func(_ interface{}, _ *iovec.IOData) error {
pollDesc.OnRead = func(_ any, _ *iovec.IOData) error {
onRead++
ch <- struct{}{}
buf := make([]byte, 8)
unix.Read(eventFD, buf)
return nil
}
hup := make(chan struct{}, 1)
pollDesc.OnHup = func(_ interface{}) {
pollDesc.OnHup = func(_ any) {
onHup = 1
hup <- struct{}{}
}
Expand All @@ -75,7 +75,7 @@ func TestNormal(t *testing.T) {
assert.Equal(t, n, len(buf))
<-ch
assert.Equal(t, onRead, 1)
pollDesc.OnRead = func(_ interface{}, _ *iovec.IOData) error {
pollDesc.OnRead = func(_ any, _ *iovec.IOData) error {
return errors.New("fake fails")
}
_, err = unix.Write(eventFD, buf)
Expand Down
4 changes: 2 additions & 2 deletions internal/poller/poller_kqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func TestNormal(t *testing.T) {
pollDesc.FD = readStream
pollDesc.Data = 0
var onRead, onWrite int
pollDesc.OnRead = func(interface{}, *iovec.IOData) error {
pollDesc.OnRead = func(any, *iovec.IOData) error {
onRead++
buf := make([]byte, 16)
n, err := unix.Read(pollDesc.FD, buf)
assert.Nil(t, err)
assert.Equal(t, 10, n)
return nil
}
pollDesc.OnWrite = func(interface{}) error {
pollDesc.OnWrite = func(any) error {
onWrite++
return nil
}
Expand Down
40 changes: 20 additions & 20 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,73 +39,73 @@ var encoderConfig = zapcore.EncoderConfig{
// Logger provides a unified logging interface.
type Logger interface {
// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
Debug(args ...interface{})
Debug(args ...any)
// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
Debugf(format string, args ...interface{})
Debugf(format string, args ...any)
// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
Info(args ...interface{})
Info(args ...any)
// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
Infof(format string, args ...interface{})
Infof(format string, args ...any)
// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
Warn(args ...interface{})
Warn(args ...any)
// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
Warnf(format string, args ...interface{})
Warnf(format string, args ...any)
// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
Error(args ...interface{})
Error(args ...any)
// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
Errorf(format string, args ...interface{})
Errorf(format string, args ...any)
// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
Fatal(args ...interface{})
Fatal(args ...any)
// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
Fatalf(format string, args ...interface{})
Fatalf(format string, args ...any)
}

// Debug logs to DEBUG log. Arguments are handled in the manner of fmt.Print.
func Debug(args ...interface{}) {
func Debug(args ...any) {
Default.Debug(args...)
}

// Debugf logs to DEBUG log. Arguments are handled in the manner of fmt.Printf.
func Debugf(format string, args ...interface{}) {
func Debugf(format string, args ...any) {
Default.Debugf(format, args...)
}

// Info logs to INFO log. Arguments are handled in the manner of fmt.Print.
func Info(args ...interface{}) {
func Info(args ...any) {
Default.Info(args...)
}

// Infof logs to INFO log. Arguments are handled in the manner of fmt.Printf.
func Infof(format string, args ...interface{}) {
func Infof(format string, args ...any) {
Default.Infof(format, args...)
}

// Warn logs to WARNING log. Arguments are handled in the manner of fmt.Print.
func Warn(args ...interface{}) {
func Warn(args ...any) {
Default.Warn(args...)
}

// Warnf logs to WARNING log. Arguments are handled in the manner of fmt.Printf.
func Warnf(format string, args ...interface{}) {
func Warnf(format string, args ...any) {
Default.Warnf(format, args...)
}

// Error logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func Error(args ...interface{}) {
func Error(args ...any) {
Default.Error(args...)
}

// Errorf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, args ...interface{}) {
func Errorf(format string, args ...any) {
Default.Errorf(format, args...)
}

// Fatal logs to ERROR log. Arguments are handled in the manner of fmt.Print.
func Fatal(args ...interface{}) {
func Fatal(args ...any) {
Default.Fatal(args...)
}

// Fatalf logs to ERROR log. Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, args ...interface{}) {
func Fatalf(format string, args ...any) {
Default.Fatalf(format, args...)
}
20 changes: 10 additions & 10 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestLog(t *testing.T) {

type noopLogger struct{}

func (*noopLogger) Debug(args ...interface{}) {}
func (*noopLogger) Debugf(format string, args ...interface{}) {}
func (*noopLogger) Info(args ...interface{}) {}
func (*noopLogger) Infof(format string, args ...interface{}) {}
func (*noopLogger) Warn(args ...interface{}) {}
func (*noopLogger) Warnf(format string, args ...interface{}) {}
func (*noopLogger) Error(args ...interface{}) {}
func (*noopLogger) Errorf(format string, args ...interface{}) {}
func (*noopLogger) Fatal(args ...interface{}) {}
func (*noopLogger) Fatalf(format string, args ...interface{}) {}
func (*noopLogger) Debug(args ...any) {}
func (*noopLogger) Debugf(format string, args ...any) {}
func (*noopLogger) Info(args ...any) {}
func (*noopLogger) Infof(format string, args ...any) {}
func (*noopLogger) Warn(args ...any) {}
func (*noopLogger) Warnf(format string, args ...any) {}
func (*noopLogger) Error(args ...any) {}
func (*noopLogger) Errorf(format string, args ...any) {}
func (*noopLogger) Fatal(args ...any) {}
func (*noopLogger) Fatalf(format string, args ...any) {}
8 changes: 4 additions & 4 deletions netfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ func (nfd *netFD) close() {

// Schedule add NetFD to poller system, and monitor Readable Event.
func (nfd *netFD) Schedule(
onRead func(data interface{}, ioData *iovec.IOData) error,
onWrite func(data interface{}) error,
onHup func(data interface{}),
conn interface{},
onRead func(data any, ioData *iovec.IOData) error,
onWrite func(data any) error,
onHup func(data any),
conn any,
) error {
if nfd.desc != nil {
return errors.New("already in poller system")
Expand Down
4 changes: 2 additions & 2 deletions taskpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
usrPool, _ = ants.NewPool(maxRoutines)
)

func taskHandler(v interface{}) {
func taskHandler(v any) {
switch conn := v.(type) {
case *tcpconn:
tcpAsyncHandler(conn)
Expand All @@ -33,7 +33,7 @@ func taskHandler(v interface{}) {
}
}

func doTask(args interface{}) error {
func doTask(args any) error {
metrics.Add(metrics.TaskAssigned, 1)
return sysPool.Invoke(args)
}
Expand Down
Loading

0 comments on commit ed8bb2a

Please sign in to comment.