Skip to content

Commit 6d40211

Browse files
authored
optimize code & adjuste directory (#105)
1 parent de840aa commit 6d40211

File tree

32 files changed

+171
-193
lines changed

32 files changed

+171
-193
lines changed

benchmarks/gev-echo-server/echo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99
"github.com/Allenxuxu/gev/log"
1010

1111
"github.com/Allenxuxu/gev"
12-
"github.com/Allenxuxu/gev/connection"
1312
)
1413

1514
type example struct {
1615
}
1716

18-
func (s *example) OnConnect(c *connection.Connection) {}
19-
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
17+
func (s *example) OnConnect(c *gev.Connection) {}
18+
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
2019

2120
out = data
2221

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

32-
func (s *example) OnClose(c *connection.Connection) {
31+
func (s *example) OnClose(c *gev.Connection) {
3332
//log.Error("onclose ")
3433
}
3534

benchmarks/websocket/server.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/Allenxuxu/gev/log"
88

99
"github.com/Allenxuxu/gev"
10-
"github.com/Allenxuxu/gev/connection"
1110
"github.com/Allenxuxu/gev/plugins/websocket"
1211
"github.com/Allenxuxu/gev/plugins/websocket/ws"
1312
)
@@ -18,11 +17,11 @@ type example struct {
1817
// connection lifecycle
1918
// OnConnect() -> OnRequest() -> OnHeader() -> OnMessage() -> OnClose()
2019

21-
func (s *example) OnConnect(c *connection.Connection) {
20+
func (s *example) OnConnect(c *gev.Connection) {
2221
//log.Println("OnConnect: ", c.PeerAddr())
2322
}
2423

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

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

34-
func (s *example) OnClose(c *connection.Connection) {
33+
func (s *example) OnClose(c *gev.Connection) {
3534
//log.Println("123 OnClose", c.PeerAddr())
3635
}
3736

3837
// NewWebSocketServer 创建 WebSocket Server
3938
func NewWebSocketServer(handler websocket.WSHandler, u *ws.Upgrader, opts ...gev.Option) (server *gev.Server, err error) {
40-
opts = append(opts, gev.Protocol(websocket.New(u)))
39+
opts = append(opts, gev.CustomProtocol(websocket.New(u)))
4140
return gev.NewServer(websocket.NewHandlerWrap(u, handler), opts...)
4241
}
4342

connection/connection.go connection.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connection
1+
package gev
22

33
import (
44
"errors"
@@ -45,8 +45,8 @@ type Connection struct {
4545

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

48-
// New 创建 Connection
49-
func New(fd int,
48+
// NewConnection 创建 Connection
49+
func NewConnection(fd int,
5050
loop *eventloop.EventLoop,
5151
sa unix.Sockaddr,
5252
protocol Protocol,
@@ -112,12 +112,12 @@ func (c *Connection) Connected() bool {
112112
}
113113

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

120-
opt := Options{}
120+
opt := ConnectionOptions{}
121121
for _, o := range opts {
122122
o(&opt)
123123
}
@@ -271,7 +271,7 @@ func (c *Connection) handleWrite(fd int) (closed bool) {
271271

272272
if c.outBuffer.IsEmpty() {
273273
if err := c.loop.EnableRead(fd); err != nil {
274-
log.Error("[EnableRead]", err)
274+
log.Error("[enableRead]", err)
275275
}
276276
}
277277

connection/options.go

-15
This file was deleted.

connection_options.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package gev
2+
3+
type SendInLoopFunc func(interface{})
4+
5+
type ConnectionOptions struct {
6+
sendInLoopFinish SendInLoopFunc
7+
}
8+
9+
type ConnectionOption func(*ConnectionOptions)
10+
11+
func SendInLoop(f SendInLoopFunc) ConnectionOption {
12+
return func(o *ConnectionOptions) {
13+
o.sendInLoopFinish = f
14+
}
15+
}

connection/context.go context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connection
1+
package gev
22

33
import "sync"
44

connection/context_test.go context_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connection
1+
package gev
22

33
import (
44
"fmt"

eventloop/eventloop.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (l *EventLoop) DeleteFdInLoop(fd int) {
8383
l.ConnCunt.Add(-1)
8484
}
8585

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

108-
// RunLoop 启动事件循环
109-
func (l *EventLoop) RunLoop() {
108+
// Run 启动事件循环
109+
func (l *EventLoop) Run() {
110110
l.poll.Poll(l.handlerEvent)
111111
}
112112

eventloop/eventloop_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestEventLoop_RunLoop(t *testing.T) {
3030
}
3131
}()
3232

33-
el.RunLoop()
33+
el.Run()
3434
}
3535

3636
func TestEventLoopSize(t *testing.T) {

example/bufferlength/main.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/Allenxuxu/gev"
10-
"github.com/Allenxuxu/gev/connection"
1110
)
1211

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

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

6665
// OnConnect callback
67-
func (s *Server) OnConnect(c *connection.Connection) {
66+
func (s *Server) OnConnect(c *gev.Connection) {
6867
log.Println(" OnConnect : ", c.PeerAddr())
6968

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

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

8079
out = data
8180
return
8281
}
8382

8483
// OnClose callback
85-
func (s *Server) OnClose(c *connection.Connection) {
84+
func (s *Server) OnClose(c *gev.Connection) {
8685
log.Println("OnClose")
8786
v, ok := c.Get(clientsKey)
8887
if !ok {

example/echo/echo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/Allenxuxu/gev"
11-
"github.com/Allenxuxu/gev/connection"
1211
"github.com/Allenxuxu/gev/log"
1312
"github.com/Allenxuxu/toolkit/sync/atomic"
1413
)
@@ -17,17 +16,17 @@ type example struct {
1716
Count atomic.Int64
1817
}
1918

20-
func (s *example) OnConnect(c *connection.Connection) {
19+
func (s *example) OnConnect(c *gev.Connection) {
2120
s.Count.Add(1)
2221
//log.Println(" OnConnect : ", c.PeerAddr())
2322
}
24-
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
23+
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
2524
//log.Println("OnMessage")
2625
out = data
2726
return
2827
}
2928

30-
func (s *example) OnClose(c *connection.Connection) {
29+
func (s *example) OnClose(c *gev.Connection) {
3130
s.Count.Add(-1)
3231
//log.Println("OnClose")
3332
}

example/idleconnection/echo.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ import (
88
"time"
99

1010
"github.com/Allenxuxu/gev"
11-
"github.com/Allenxuxu/gev/connection"
1211
"github.com/Allenxuxu/gev/log"
1312
)
1413

1514
type example struct {
1615
}
1716

18-
func (s *example) OnConnect(c *connection.Connection) {
17+
func (s *example) OnConnect(c *gev.Connection) {
1918
log.Info(" OnConnect : ", c.PeerAddr())
2019
}
21-
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
20+
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
2221
log.Infof("OnMessage from : %s", c.PeerAddr())
2322
out = data
2423
return
2524
}
2625

27-
func (s *example) OnClose(c *connection.Connection) {
26+
func (s *example) OnClose(c *gev.Connection) {
2827
log.Info("OnClose: ", c.PeerAddr())
2928
}
3029

example/maxconnection/main.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
_ "net/http/pprof"
77

88
"github.com/Allenxuxu/gev"
9-
"github.com/Allenxuxu/gev/connection"
109
"github.com/Allenxuxu/toolkit/sync/atomic"
1110
)
1211

@@ -42,7 +41,7 @@ func (s *Server) Stop() {
4241
}
4342

4443
// OnConnect callback
45-
func (s *Server) OnConnect(c *connection.Connection) {
44+
func (s *Server) OnConnect(c *gev.Connection) {
4645
s.clientNum.Add(1)
4746
log.Println(" OnConnect : ", c.PeerAddr())
4847

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

5655
// OnMessage callback
57-
func (s *Server) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
56+
func (s *Server) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
5857
log.Println("OnMessage")
5958
out = data
6059
return
6160
}
6261

6362
// OnClose callback
64-
func (s *Server) OnClose(c *connection.Connection) {
63+
func (s *Server) OnClose(c *gev.Connection) {
6564
s.clientNum.Add(-1)
6665
log.Println("OnClose")
6766
}

example/protobuf/server/main.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import (
66
"strconv"
77

88
"github.com/Allenxuxu/gev"
9-
"github.com/Allenxuxu/gev/connection"
109
pb "github.com/Allenxuxu/gev/example/protobuf/proto"
1110
"github.com/Allenxuxu/gev/plugins/protobuf"
1211
"google.golang.org/protobuf/proto"
1312
)
1413

1514
type example struct{}
1615

17-
func (s *example) OnConnect(c *connection.Connection) {
16+
func (s *example) OnConnect(c *gev.Connection) {
1817
log.Println(" OnConnect : ", c.PeerAddr())
1918
}
20-
func (s *example) OnMessage(c *connection.Connection, ctx interface{}, data []byte) (out interface{}) {
19+
func (s *example) OnMessage(c *gev.Connection, ctx interface{}, data []byte) (out interface{}) {
2120
msgType := ctx.(string)
2221

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

43-
func (s *example) OnClose(c *connection.Connection) {
42+
func (s *example) OnClose(c *gev.Connection) {
4443
log.Println("OnClose")
4544
}
4645

@@ -57,7 +56,7 @@ func main() {
5756
gev.Network("tcp"),
5857
gev.Address(":"+strconv.Itoa(port)),
5958
gev.NumLoops(loops),
60-
gev.Protocol(&protobuf.Protocol{}))
59+
gev.CustomProtocol(&protobuf.Protocol{}))
6160
if err != nil {
6261
panic(err)
6362
}

example/protocol/protocol.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"encoding/binary"
55

6-
"github.com/Allenxuxu/gev/connection"
6+
"github.com/Allenxuxu/gev"
77
"github.com/Allenxuxu/ringbuffer"
88
"github.com/gobwas/pool/pbytes"
99
)
@@ -12,7 +12,7 @@ const exampleHeaderLen = 4
1212

1313
type ExampleProtocol struct{}
1414

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

35-
func (d *ExampleProtocol) Packet(c *connection.Connection, data interface{}) []byte {
35+
func (d *ExampleProtocol) Packet(c *gev.Connection, data interface{}) []byte {
3636
dd := data.([]byte)
3737
dataLen := len(dd)
3838
ret := make([]byte, exampleHeaderLen+dataLen)

0 commit comments

Comments
 (0)