Skip to content

Commit

Permalink
optimize code & adjuste directory (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allenxuxu committed Nov 9, 2021
1 parent de840aa commit 6d40211
Show file tree
Hide file tree
Showing 32 changed files with 171 additions and 193 deletions.
7 changes: 3 additions & 4 deletions benchmarks/gev-echo-server/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import (
"github.com/Allenxuxu/gev/log"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
)

type example struct {
}

func (s *example) OnConnect(c *connection.Connection) {}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *example) OnConnect(c *gev.Connection) {}
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {

out = data

Expand All @@ -29,7 +28,7 @@ func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []by
return
}

func (s *example) OnClose(c *connection.Connection) {
func (s *example) OnClose(c *gev.Connection) {
//log.Error("onclose ")
}

Expand Down
9 changes: 4 additions & 5 deletions benchmarks/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/Allenxuxu/gev/log"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/plugins/websocket"
"github.com/Allenxuxu/gev/plugins/websocket/ws"
)
Expand All @@ -18,11 +17,11 @@ type example struct {
// connection lifecycle
// OnConnect() -> OnRequest() -> OnHeader() -> OnMessage() -> OnClose()

func (s *example) OnConnect(c *connection.Connection) {
func (s *example) OnConnect(c *gev.Connection) {
//log.Println("OnConnect: ", c.PeerAddr())
}

func (s *example) OnMessage(c *connection.Connection, data []byte) (messageType ws.MessageType, out []byte) {
func (s *example) OnMessage(c *gev.Connection, data []byte) (messageType ws.MessageType, out []byte) {
//log.Println("OnMessage: ", string(data))

messageType = ws.MessageBinary
Expand All @@ -31,13 +30,13 @@ func (s *example) OnMessage(c *connection.Connection, data []byte) (messageType
return
}

func (s *example) OnClose(c *connection.Connection) {
func (s *example) OnClose(c *gev.Connection) {
//log.Println("123 OnClose", c.PeerAddr())
}

// NewWebSocketServer 创建 WebSocket Server
func NewWebSocketServer(handler websocket.WSHandler, u *ws.Upgrader, opts ...gev.Option) (server *gev.Server, err error) {
opts = append(opts, gev.Protocol(websocket.New(u)))
opts = append(opts, gev.CustomProtocol(websocket.New(u)))
return gev.NewServer(websocket.NewHandlerWrap(u, handler), opts...)
}

Expand Down
12 changes: 6 additions & 6 deletions connection/connection.go → connection.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package connection
package gev

import (
"errors"
Expand Down Expand Up @@ -45,8 +45,8 @@ type Connection struct {

var ErrConnectionClosed = errors.New("connection closed")

// New 创建 Connection
func New(fd int,
// NewConnection 创建 Connection
func NewConnection(fd int,
loop *eventloop.EventLoop,
sa unix.Sockaddr,
protocol Protocol,
Expand Down Expand Up @@ -112,12 +112,12 @@ func (c *Connection) Connected() bool {
}

// Send 用来在非 loop 协程发送
func (c *Connection) Send(data interface{}, opts ...Option) error {
func (c *Connection) Send(data interface{}, opts ...ConnectionOption) error {
if !c.connected.Get() {
return ErrConnectionClosed
}

opt := Options{}
opt := ConnectionOptions{}
for _, o := range opts {
o(&opt)
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (c *Connection) handleWrite(fd int) (closed bool) {

if c.outBuffer.IsEmpty() {
if err := c.loop.EnableRead(fd); err != nil {
log.Error("[EnableRead]", err)
log.Error("[enableRead]", err)
}
}

Expand Down
15 changes: 0 additions & 15 deletions connection/options.go

This file was deleted.

15 changes: 15 additions & 0 deletions connection_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gev

type SendInLoopFunc func(interface{})

type ConnectionOptions struct {
sendInLoopFinish SendInLoopFunc
}

type ConnectionOption func(*ConnectionOptions)

func SendInLoop(f SendInLoopFunc) ConnectionOption {
return func(o *ConnectionOptions) {
o.sendInLoopFinish = f
}
}
2 changes: 1 addition & 1 deletion connection/context.go → context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package connection
package gev

import "sync"

Expand Down
2 changes: 1 addition & 1 deletion connection/context_test.go → context_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package connection
package gev

import (
"fmt"
Expand Down
6 changes: 3 additions & 3 deletions eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (l *EventLoop) DeleteFdInLoop(fd int) {
l.ConnCunt.Add(-1)
}

// AddSocketAndEnableRead 增加 Socket 到时间循环中,并注册可读事件
// AddSocketAndEnableRead 增加 Socket 到事件循环中,并注册可读事件
func (l *EventLoop) AddSocketAndEnableRead(fd int, s Socket) error {
l.sockets[fd] = s
if err := l.poll.AddRead(fd); err != nil {
Expand All @@ -105,8 +105,8 @@ func (l *EventLoop) EnableRead(fd int) error {
return l.poll.EnableRead(fd)
}

// RunLoop 启动事件循环
func (l *EventLoop) RunLoop() {
// Run 启动事件循环
func (l *EventLoop) Run() {
l.poll.Poll(l.handlerEvent)
}

Expand Down
2 changes: 1 addition & 1 deletion eventloop/eventloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestEventLoop_RunLoop(t *testing.T) {
}
}()

el.RunLoop()
el.Run()
}

func TestEventLoopSize(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions example/bufferlength/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
)

const clientsKey = "demo_push_message_key"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (s *Server) RunPush() {
for e := s.conn.Front(); e != nil; e = next {
next = e.Next()

c := e.Value.(*connection.Connection)
c := e.Value.(*gev.Connection)
if c.WriteBufferLength() > 1024*10 {
log.Printf("write buffer length > 1024*10")
continue
Expand All @@ -64,7 +63,7 @@ func (s *Server) RunPush() {
}

// OnConnect callback
func (s *Server) OnConnect(c *connection.Connection) {
func (s *Server) OnConnect(c *gev.Connection) {
log.Println(" OnConnect : ", c.PeerAddr())

s.mu.Lock()
Expand All @@ -74,15 +73,15 @@ func (s *Server) OnConnect(c *connection.Connection) {
}

// OnMessage callback
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *Server) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Printf("OnMessage, read buffer len %d, write buffer len %d \n", c.ReadBufferLength(), c.WriteBufferLength())

out = data
return
}

// OnClose callback
func (s *Server) OnClose(c *connection.Connection) {
func (s *Server) OnClose(c *gev.Connection) {
log.Println("OnClose")
v, ok := c.Get(clientsKey)
if !ok {
Expand Down
7 changes: 3 additions & 4 deletions example/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/log"
"github.com/Allenxuxu/toolkit/sync/atomic"
)
Expand All @@ -17,17 +16,17 @@ type example struct {
Count atomic.Int64
}

func (s *example) OnConnect(c *connection.Connection) {
func (s *example) OnConnect(c *gev.Connection) {
s.Count.Add(1)
//log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
//log.Println("OnMessage")
out = data
return
}

func (s *example) OnClose(c *connection.Connection) {
func (s *example) OnClose(c *gev.Connection) {
s.Count.Add(-1)
//log.Println("OnClose")
}
Expand Down
7 changes: 3 additions & 4 deletions example/idleconnection/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ import (
"time"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev/log"
)

type example struct {
}

func (s *example) OnConnect(c *connection.Connection) {
func (s *example) OnConnect(c *gev.Connection) {
log.Info(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Infof("OnMessage from : %s", c.PeerAddr())
out = data
return
}

func (s *example) OnClose(c *connection.Connection) {
func (s *example) OnClose(c *gev.Connection) {
log.Info("OnClose: ", c.PeerAddr())
}

Expand Down
7 changes: 3 additions & 4 deletions example/maxconnection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
_ "net/http/pprof"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/toolkit/sync/atomic"
)

Expand Down Expand Up @@ -42,7 +41,7 @@ func (s *Server) Stop() {
}

// OnConnect callback
func (s *Server) OnConnect(c *connection.Connection) {
func (s *Server) OnConnect(c *gev.Connection) {
s.clientNum.Add(1)
log.Println(" OnConnect : ", c.PeerAddr())

Expand All @@ -54,14 +53,14 @@ func (s *Server) OnConnect(c *connection.Connection) {
}

// OnMessage callback
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *Server) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
log.Println("OnMessage")
out = data
return
}

// OnClose callback
func (s *Server) OnClose(c *connection.Connection) {
func (s *Server) OnClose(c *gev.Connection) {
s.clientNum.Add(-1)
log.Println("OnClose")
}
Expand Down
9 changes: 4 additions & 5 deletions example/protobuf/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ import (
"strconv"

"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/gev/connection"
pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"google.golang.org/protobuf/proto"
)

type example struct{}

func (s *example) OnConnect(c *connection.Connection) {
func (s *example) OnConnect(c *gev.Connection) {
log.Println(" OnConnect : ", c.PeerAddr())
}
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
msgType := ctx.(string)

switch msgType {
Expand All @@ -40,7 +39,7 @@ func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []by
return
}

func (s *example) OnClose(c *connection.Connection) {
func (s *example) OnClose(c *gev.Connection) {
log.Println("OnClose")
}

Expand All @@ -57,7 +56,7 @@ func main() {
gev.Network("tcp"),
gev.Address(":"+strconv.Itoa(port)),
gev.NumLoops(loops),
gev.Protocol(&protobuf.Protocol{}))
gev.CustomProtocol(&protobuf.Protocol{}))
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions example/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"encoding/binary"

"github.com/Allenxuxu/gev/connection"
"github.com/Allenxuxu/gev"
"github.com/Allenxuxu/ringbuffer"
"github.com/gobwas/pool/pbytes"
)
Expand All @@ -12,7 +12,7 @@ const exampleHeaderLen = 4

type ExampleProtocol struct{}

func (d *ExampleProtocol) UnPacket(c *connection.Connection, buffer *ringbuffer.RingBuffer) (interface{}, []byte) {
func (d *ExampleProtocol) UnPacket(c *gev.Connection, buffer *ringbuffer.RingBuffer) (interface{}, []byte) {
if buffer.VirtualLength() > exampleHeaderLen {
buf := pbytes.GetLen(exampleHeaderLen)
defer pbytes.Put(buf)
Expand All @@ -32,7 +32,7 @@ func (d *ExampleProtocol) UnPacket(c *connection.Connection, buffer *ringbuffer.
return nil, nil
}

func (d *ExampleProtocol) Packet(c *connection.Connection, data interface{}) []byte {
func (d *ExampleProtocol) Packet(c *gev.Connection, data interface{}) []byte {
dd := data.([]byte)
dataLen := len(dd)
ret := make([]byte, exampleHeaderLen+dataLen)
Expand Down
Loading

0 comments on commit 6d40211

Please sign in to comment.