Skip to content

Commit d98706e

Browse files
committed
opt: make use of the inheritance of file status flags on BSD-like OS
1 parent c839bfb commit d98706e

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

acceptor_bsd.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2023 The Gnet Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build freebsd || dragonfly || darwin
16+
// +build freebsd dragonfly darwin
17+
18+
package gnet
19+
20+
// The canonical BSD sockets implementation will inherit file status flags
21+
// from the listening socket, so we don't need to set the non-blocking flag
22+
// for the accepted sockets explicitly.
23+
func setNonBlock(_ int, _ bool) error {
24+
return nil
25+
}

acceptor_linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2023 The Gnet Authors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package gnet
19+
20+
import "golang.org/x/sys/unix"
21+
22+
func setNonBlock(fd int, nonBlocking bool) error {
23+
return unix.SetNonblock(fd, nonBlocking)
24+
}

acceptor_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (eng *engine) accept(fd int, _ netpoll.IOEvent) error {
4444
}
4545
}
4646

47-
if err = os.NewSyscallError("fcntl nonblock", unix.SetNonblock(nfd, true)); err != nil {
47+
if err = os.NewSyscallError("fcntl nonblock", setNonBlock(nfd, true)); err != nil {
4848
return err
4949
}
5050
remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)
@@ -83,7 +83,7 @@ func (el *eventloop) accept(fd int, ev netpoll.IOEvent) error {
8383
}
8484
}
8585

86-
if err = os.NewSyscallError("fcntl nonblock", unix.SetNonblock(nfd, true)); err != nil {
86+
if err = os.NewSyscallError("fcntl nonblock", setNonBlock(nfd, true)); err != nil {
8787
return err
8888
}
8989
remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)

0 commit comments

Comments
 (0)