forked from cloudwego/netpoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_onevent.go
161 lines (146 loc) · 4.1 KB
/
connection_onevent.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2021 CloudWeGo Authors
//
// 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 netpoll
import (
"context"
"log"
"sync/atomic"
"github.com/bytedance/gopkg/util/gopool"
)
// ------------------------------------ implement OnPrepare, OnRequest, CloseCallback ------------------------------------
type gracefulExit interface {
isIdle() (yes bool)
Close() (err error)
}
// onEvent is the collection of event processing.
// OnPrepare, OnRequest, CloseCallback share the lock processing,
// which is a CAS lock and can only be cleared by OnRequest.
type onEvent struct {
ctx context.Context
process atomic.Value // value is OnRequest
callbacks atomic.Value // value is latest *callbackNode
}
type callbackNode struct {
fn CloseCallback
pre *callbackNode
}
// SetOnRequest initialize ctx when setting OnRequest.
func (on *onEvent) SetOnRequest(onReq OnRequest) error {
if onReq != nil {
on.process.Store(onReq)
}
return nil
}
// AddCloseCallback adds a CloseCallback to this connection.
func (on *onEvent) AddCloseCallback(callback CloseCallback) error {
if callback == nil {
return nil
}
var cb = &callbackNode{}
cb.fn = callback
if pre := on.callbacks.Load(); pre != nil {
cb.pre = pre.(*callbackNode)
}
on.callbacks.Store(cb)
return nil
}
// OnPrepare supports close connection, but not read/write data.
// connection will be register by this call after preparing.
func (c *connection) onPrepare(prepare OnPrepare) (err error) {
// calling prepare first and then register.
if prepare != nil {
c.ctx = prepare(c)
}
// prepare may close the connection.
if c.IsActive() {
return c.register()
}
return nil
}
// onRequest is also responsible for executing the callbacks after the connection has been closed.
func (c *connection) onRequest() (err error) {
var process = c.process.Load()
if process == nil {
return nil
}
// Buffer has been fully processed, or task already exists
if !c.lock(processing) {
return nil
}
// add new task
var task = func() {
if c.ctx == nil {
c.ctx = context.Background()
}
var handler = process.(OnRequest)
START:
// NOTE: loop processing, which is useful for streaming.
for c.Reader().Len() > 0 && c.IsActive() {
// Single request processing, blocking allowed.
handler(c.ctx, c)
}
// Handling callback if connection has been closed.
if !c.IsActive() {
c.closeCallback(false)
return
}
// Double check when exiting.
c.unlock(processing)
if c.Reader().Len() > 0 {
if !c.lock(processing) {
return
}
goto START
}
}
gopool.CtxGo(c.ctx, task)
return nil
}
// closeCallback .
// It can be confirmed that closeCallback and onRequest will not be executed concurrently.
// If onRequest is still running, it will trigger closeCallback on exit.
func (c *connection) closeCallback(needLock bool) (err error) {
if needLock && !c.lock(processing) {
return nil
}
var latest = c.callbacks.Load()
if latest == nil {
return nil
}
for callback := latest.(*callbackNode); callback != nil; callback = callback.pre {
callback.fn(c)
}
return nil
}
// register only use for connection register into poll.
func (c *connection) register() (err error) {
if c.operator.poll != nil {
err = c.operator.Control(PollModReadable)
} else {
c.operator.poll = pollmanager.Pick()
err = c.operator.Control(PollReadable)
}
if err != nil {
log.Println("connection register failed:", err.Error())
c.Close()
return Exception(ErrConnClosed, err.Error())
}
return nil
}
// isIdle implements gracefulExit.
func (c *connection) isIdle() (yes bool) {
return c.isUnlock(processing) &&
c.inputBuffer.IsEmpty() &&
c.outputBuffer.IsEmpty()
}