// +build linux

package tcplisten

import (
	
	
	
	
	
	
)

const (
	soReusePort = 0x0F
	tcpFastOpen = 0x17
)

func enableDeferAccept( int) error {
	if  := syscall.SetsockoptInt(, syscall.IPPROTO_TCP, syscall.TCP_DEFER_ACCEPT, 1);  != nil {
		return fmt.Errorf("cannot enable TCP_DEFER_ACCEPT: %s", )
	}
	return nil
}

func enableFastOpen( int) error {
	if  := syscall.SetsockoptInt(, syscall.SOL_TCP, tcpFastOpen, fastOpenQlen);  != nil {
		return fmt.Errorf("cannot enable TCP_FASTOPEN(qlen=%d): %s", fastOpenQlen, )
	}
	return nil
}

const fastOpenQlen = 16 * 1024

func soMaxConn() (int, error) {
	,  := ioutil.ReadFile(soMaxConnFilePath)
	if  != nil {
		// This error may trigger on travis build. Just use SOMAXCONN
		if os.IsNotExist() {
			return syscall.SOMAXCONN, nil
		}
		return -1, 
	}
	 := strings.TrimSpace(string())
	,  := strconv.Atoi()
	if  != nil ||  <= 0 {
		return -1, fmt.Errorf("cannot parse somaxconn %q read from %s: %s", , soMaxConnFilePath, )
	}

	// Linux stores the backlog in a uint16.
	// Truncate number to avoid wrapping.
	// See https://github.com/golang/go/issues/5030 .
	if  > 1<<16-1 {
		 = 1<<16 - 1
	}
	return , nil
}

const soMaxConnFilePath = "/proc/sys/net/core/somaxconn"