-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevent_loop.go
90 lines (78 loc) · 2.29 KB
/
event_loop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2023-2024 antlabs. All rights reserved.
//
// 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 greatws
import (
"context"
"sync/atomic"
"time"
"github.com/antlabs/greatws/task/driver"
)
type evFlag int
const (
EVENT_EPOLL evFlag = 1 << iota
EVENT_IOURING
)
type EventLoop struct {
maxFd int // highest file descriptor currently registered
setSize int // max number of file descriptors tracked
*apiState // 每个平台对应的异步io接口/epoll/kqueue/iouring(暂时不加,除非io-uring性能超过epoll才加回来)
shutdown bool
parent *MultiEventLoop
localTask selectTasks
}
// 初始化函数
func CreateEventLoop(setSize int, flag evFlag, parent *MultiEventLoop) (e *EventLoop, err error) {
e = &EventLoop{
setSize: setSize,
maxFd: -1,
parent: parent,
}
var c driver.Conf
c.Log = parent.Logger
// 初始化任务池
e.localTask = newSelectTask(parent.ctx, parent.configTask.initCount, parent.configTask.min, parent.configTask.max, &c)
// TODO+
// e.localTask.taskConfig = e.parent.configTask.taskConfig
// e.localTask.taskMode = e.parent.configTask.taskMode
// e.localTask.init()
err = e.apiCreate(flag)
return e, err
}
// 柔性关闭所有的连接
func (e *EventLoop) Shutdown(ctx context.Context) error {
return nil
}
func (el *EventLoop) Loop() {
for !el.shutdown {
_, err := el.apiPoll(time.Duration(time.Second * 100))
if err != nil {
el.parent.Error("apiPolll", "err", err.Error())
return
}
}
}
// 获取一个连接
func (m *EventLoop) getConn(fd int) *Conn {
return m.parent.safeConns.getConn(fd)
}
func (el *EventLoop) del(c *Conn) {
fd := c.getFd()
atomic.AddInt64(&el.parent.curConn, -1)
el.parent.safeConns.delConn(c)
// el.conns.Delete(fd)
closeFd(fd)
}
func (el *EventLoop) GetApiName() string {
return el.apiName()
}