@@ -47,18 +47,19 @@ func cancelContext(ctx context.Context) context.Context {
47
47
}
48
48
49
49
type transporter struct {
50
- sync.RWMutex
51
50
senseClientDisconnection bool
52
51
network string
53
52
addr string
54
53
keepAliveTimeout time.Duration
55
54
readTimeout time.Duration
56
55
writeTimeout time.Duration
57
- listener net.Listener
58
- eventLoop netpoll.EventLoop
59
56
listenConfig * net.ListenConfig
60
57
OnAccept func (conn net.Conn ) context.Context
61
58
OnConnect func (ctx context.Context , conn network.Conn ) context.Context
59
+
60
+ mu sync.RWMutex
61
+ ln net.Listener
62
+ el netpoll.EventLoop
62
63
}
63
64
64
65
// For transporter switch
@@ -70,23 +71,31 @@ func NewTransporter(options *config.Options) network.Transporter {
70
71
keepAliveTimeout : options .KeepAliveTimeout ,
71
72
readTimeout : options .ReadTimeout ,
72
73
writeTimeout : options .WriteTimeout ,
73
- listener : nil ,
74
- eventLoop : nil ,
75
74
listenConfig : options .ListenConfig ,
76
75
OnAccept : options .OnAccept ,
77
76
OnConnect : options .OnConnect ,
78
77
}
79
78
}
80
79
80
+ func (t * transporter ) Listener () net.Listener {
81
+ t .mu .RLock ()
82
+ defer t .mu .RUnlock ()
83
+ return t .ln
84
+ }
85
+
81
86
// ListenAndServe binds listen address and keep serving, until an error occurs
82
87
// or the transport shutdowns
83
88
func (t * transporter ) ListenAndServe (onReq network.OnData ) (err error ) {
84
89
network .UnlinkUdsFile (t .network , t .addr ) //nolint:errcheck
90
+
91
+ t .mu .Lock ()
85
92
if t .listenConfig != nil {
86
- t .listener , err = t .listenConfig .Listen (context .Background (), t .network , t .addr )
93
+ t .ln , err = t .listenConfig .Listen (context .Background (), t .network , t .addr )
87
94
} else {
88
- t .listener , err = net .Listen (t .network , t .addr )
95
+ t .ln , err = net .Listen (t .network , t .addr )
89
96
}
97
+ ln := t .ln
98
+ t .mu .Unlock ()
90
99
91
100
if err != nil {
92
101
panic ("create netpoll listener fail: " + err .Error ())
@@ -127,20 +136,19 @@ func (t *transporter) ListenAndServe(onReq network.OnData) (err error) {
127
136
}
128
137
129
138
// Create EventLoop
130
- t .Lock ()
131
- t .eventLoop , err = netpoll .NewEventLoop (func (ctx context.Context , connection netpoll.Connection ) error {
139
+ t .mu . Lock ()
140
+ t .el , err = netpoll .NewEventLoop (func (ctx context.Context , connection netpoll.Connection ) error {
132
141
return onReq (ctx , newConn (connection ))
133
142
}, opts ... )
134
- t .Unlock ()
143
+ eventLoop := t .el
144
+ t .mu .Unlock ()
135
145
if err != nil {
136
146
panic ("create netpoll event-loop fail" )
137
147
}
138
148
139
149
// Start Server
140
- hlog .SystemLogger ().Infof ("HTTP server listening on address=%s" , t .listener .Addr ().String ())
141
- t .RLock ()
142
- err = t .eventLoop .Serve (t .listener )
143
- t .RUnlock ()
150
+ hlog .SystemLogger ().Infof ("HTTP server listening on address=%s" , ln .Addr ().String ())
151
+ err = eventLoop .Serve (ln )
144
152
if err != nil {
145
153
panic ("netpoll server exit" )
146
154
}
@@ -160,11 +168,11 @@ func (t *transporter) Close() error {
160
168
func (t * transporter ) Shutdown (ctx context.Context ) error {
161
169
defer func () {
162
170
network .UnlinkUdsFile (t .network , t .addr ) //nolint:errcheck
163
- t .RUnlock ()
171
+ t .mu . RUnlock ()
164
172
}()
165
- t .RLock ()
166
- if t .eventLoop == nil {
173
+ t .mu . RLock ()
174
+ if t .el == nil {
167
175
return nil
168
176
}
169
- return t .eventLoop .Shutdown (ctx )
177
+ return t .el .Shutdown (ctx )
170
178
}
0 commit comments