Skip to content

Commit

Permalink
opt: make use of the inheritance of file status flags on BSD-like OS
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jun 14, 2023
1 parent c839bfb commit d98706e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
25 changes: 25 additions & 0 deletions acceptor_bsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2023 The Gnet Authors. All rights reserved.
//
// 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.

//go:build freebsd || dragonfly || darwin
// +build freebsd dragonfly darwin

package gnet

// The canonical BSD sockets implementation will inherit file status flags
// from the listening socket, so we don't need to set the non-blocking flag
// for the accepted sockets explicitly.
func setNonBlock(_ int, _ bool) error {
return nil
}
24 changes: 24 additions & 0 deletions acceptor_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2023 The Gnet Authors. All rights reserved.
*
* 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 gnet

import "golang.org/x/sys/unix"

func setNonBlock(fd int, nonBlocking bool) error {
return unix.SetNonblock(fd, nonBlocking)
}
4 changes: 2 additions & 2 deletions acceptor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (eng *engine) accept(fd int, _ netpoll.IOEvent) error {
}
}

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

if err = os.NewSyscallError("fcntl nonblock", unix.SetNonblock(nfd, true)); err != nil {
if err = os.NewSyscallError("fcntl nonblock", setNonBlock(nfd, true)); err != nil {
return err
}
remoteAddr := socket.SockaddrToTCPOrUnixAddr(sa)
Expand Down

0 comments on commit d98706e

Please sign in to comment.