Skip to content

Commit a5e0aef

Browse files
authored
Merge pull request #57 from jizhuozhi/main
optimize: check network before listening for memfd
2 parents 4e116be + e6403dd commit a5e0aef

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

listener.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ import (
2828
"time"
2929
)
3030

31-
//ListenCallback is server's asynchronous API
31+
// ListenCallback is server's asynchronous API
3232
type ListenCallback interface {
3333
//OnNewStream was called when accept a new stream
3434
OnNewStream(s *Stream)
3535
//OnShutdown was called when the listener was stopped
3636
OnShutdown(reason string)
3737
}
3838

39-
//ListenerConfig is the configuration of Listener
39+
// ListenerConfig is the configuration of Listener
4040
type ListenerConfig struct {
4141
*Config
4242
Network string //Only support unix or tcp
@@ -45,7 +45,7 @@ type ListenerConfig struct {
4545
ListenPath string
4646
}
4747

48-
//Listener listen socket and accept connection as shmipc server connection
48+
// Listener listen socket and accept connection as shmipc server connection
4949
type Listener struct {
5050
mu sync.Mutex
5151
dispatcher dispatcher
@@ -62,7 +62,7 @@ type Listener struct {
6262
unlinkOnClose bool
6363
}
6464

65-
//NewDefaultListenerConfig return the default Listener's config
65+
// NewDefaultListenerConfig return the default Listener's config
6666
func NewDefaultListenerConfig(listenPath string, network string) *ListenerConfig {
6767
return &ListenerConfig{
6868
Config: DefaultConfig(),
@@ -71,7 +71,7 @@ func NewDefaultListenerConfig(listenPath string, network string) *ListenerConfig
7171
}
7272
}
7373

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.
7575
func NewListener(callback ListenCallback, config *ListenerConfig) (*Listener, error) {
7676
if callback == nil {
7777
return nil, errors.New("ListenCallback couldn't be nil")
@@ -81,7 +81,14 @@ func NewListener(callback ListenCallback, config *ListenerConfig) (*Listener, er
8181
return nil, fmt.Errorf("only support linux OS")
8282
}
8383

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+
8592
ln, err := net.Listen(config.Network, config.ListenPath)
8693

8794
if err != nil {
@@ -122,17 +129,17 @@ func (l *Listener) Close() error {
122129
return nil
123130
}
124131

125-
//Addr returns the listener's network address.
132+
// Addr returns the listener's network address.
126133
func (l *Listener) Addr() net.Addr {
127134
return l.ln.Addr()
128135
}
129136

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.
131138
func (l *Listener) Accept() (net.Conn, error) {
132139
return nil, errors.New("not support now, just compact net.Listener interface")
133140
}
134141

135-
//Run starting a loop to listen socket
142+
// Run starting a loop to listen socket
136143
func (l *Listener) Run() error {
137144
for {
138145
conn, err := l.ln.Accept()
@@ -164,7 +171,7 @@ func (l *Listener) Run() error {
164171
return nil
165172
}
166173

167-
//HotRestart will do shmipc server hot restart
174+
// HotRestart will do shmipc server hot restart
168175
func (l *Listener) HotRestart(epoch uint64) error {
169176
l.logger.warnf("begin HotRestart epoch:%d", epoch)
170177

@@ -204,15 +211,15 @@ func (l *Listener) HotRestart(epoch uint64) error {
204211
return nil
205212
}
206213

207-
//IsHotRestartDone return whether the Listener is under the hot restart state.
214+
// IsHotRestartDone return whether the Listener is under the hot restart state.
208215
func (l *Listener) IsHotRestartDone() bool {
209216
l.mu.Lock()
210217
defer l.mu.Unlock()
211218

212219
return l.state != hotRestartState
213220
}
214221

215-
//SetUnlinkOnClose sets whether unlink unix socket path when Listener was stopped
222+
// SetUnlinkOnClose sets whether unlink unix socket path when Listener was stopped
216223
func (l *Listener) SetUnlinkOnClose(unlink bool) {
217224
l.mu.Lock()
218225
defer l.mu.Unlock()

0 commit comments

Comments
 (0)