@@ -28,15 +28,15 @@ import (
28
28
"time"
29
29
)
30
30
31
- //ListenCallback is server's asynchronous API
31
+ // ListenCallback is server's asynchronous API
32
32
type ListenCallback interface {
33
33
//OnNewStream was called when accept a new stream
34
34
OnNewStream (s * Stream )
35
35
//OnShutdown was called when the listener was stopped
36
36
OnShutdown (reason string )
37
37
}
38
38
39
- //ListenerConfig is the configuration of Listener
39
+ // ListenerConfig is the configuration of Listener
40
40
type ListenerConfig struct {
41
41
* Config
42
42
Network string //Only support unix or tcp
@@ -45,7 +45,7 @@ type ListenerConfig struct {
45
45
ListenPath string
46
46
}
47
47
48
- //Listener listen socket and accept connection as shmipc server connection
48
+ // Listener listen socket and accept connection as shmipc server connection
49
49
type Listener struct {
50
50
mu sync.Mutex
51
51
dispatcher dispatcher
@@ -62,7 +62,7 @@ type Listener struct {
62
62
unlinkOnClose bool
63
63
}
64
64
65
- //NewDefaultListenerConfig return the default Listener's config
65
+ // NewDefaultListenerConfig return the default Listener's config
66
66
func NewDefaultListenerConfig (listenPath string , network string ) * ListenerConfig {
67
67
return & ListenerConfig {
68
68
Config : DefaultConfig (),
@@ -71,7 +71,7 @@ func NewDefaultListenerConfig(listenPath string, network string) *ListenerConfig
71
71
}
72
72
}
73
73
74
- //NewListener will try listen the ListenPath of the configuration, and return the Listener if no error happened.
74
+ // NewListener will try listen the ListenPath of the configuration, and return the Listener if no error happened.
75
75
func NewListener (callback ListenCallback , config * ListenerConfig ) (* Listener , error ) {
76
76
if callback == nil {
77
77
return nil , errors .New ("ListenCallback couldn't be nil" )
@@ -81,7 +81,14 @@ func NewListener(callback ListenCallback, config *ListenerConfig) (*Listener, er
81
81
return nil , fmt .Errorf ("only support linux OS" )
82
82
}
83
83
84
- safeRemoveUdsFile (config .ListenPath )
84
+ if config .MemMapType == MemMapTypeMemFd && config .Network != "unix" {
85
+ return nil , errors .New ("config.Network must be unix when config.MemMapType is MemMapTypeMemFd" )
86
+ }
87
+
88
+ if config .Network == "unix" {
89
+ safeRemoveUdsFile (config .ListenPath )
90
+ }
91
+
85
92
ln , err := net .Listen (config .Network , config .ListenPath )
86
93
87
94
if err != nil {
@@ -122,17 +129,17 @@ func (l *Listener) Close() error {
122
129
return nil
123
130
}
124
131
125
- //Addr returns the listener's network address.
132
+ // Addr returns the listener's network address.
126
133
func (l * Listener ) Addr () net.Addr {
127
134
return l .ln .Addr ()
128
135
}
129
136
130
- //Accept doesn't work, whose existence just adapt to the net.Listener interface.
137
+ // Accept doesn't work, whose existence just adapt to the net.Listener interface.
131
138
func (l * Listener ) Accept () (net.Conn , error ) {
132
139
return nil , errors .New ("not support now, just compact net.Listener interface" )
133
140
}
134
141
135
- //Run starting a loop to listen socket
142
+ // Run starting a loop to listen socket
136
143
func (l * Listener ) Run () error {
137
144
for {
138
145
conn , err := l .ln .Accept ()
@@ -164,7 +171,7 @@ func (l *Listener) Run() error {
164
171
return nil
165
172
}
166
173
167
- //HotRestart will do shmipc server hot restart
174
+ // HotRestart will do shmipc server hot restart
168
175
func (l * Listener ) HotRestart (epoch uint64 ) error {
169
176
l .logger .warnf ("begin HotRestart epoch:%d" , epoch )
170
177
@@ -204,15 +211,15 @@ func (l *Listener) HotRestart(epoch uint64) error {
204
211
return nil
205
212
}
206
213
207
- //IsHotRestartDone return whether the Listener is under the hot restart state.
214
+ // IsHotRestartDone return whether the Listener is under the hot restart state.
208
215
func (l * Listener ) IsHotRestartDone () bool {
209
216
l .mu .Lock ()
210
217
defer l .mu .Unlock ()
211
218
212
219
return l .state != hotRestartState
213
220
}
214
221
215
- //SetUnlinkOnClose sets whether unlink unix socket path when Listener was stopped
222
+ // SetUnlinkOnClose sets whether unlink unix socket path when Listener was stopped
216
223
func (l * Listener ) SetUnlinkOnClose (unlink bool ) {
217
224
l .mu .Lock ()
218
225
defer l .mu .Unlock ()
0 commit comments