本文整理汇总了Golang中net.TCPConn.SetLinger方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.SetLinger方法的具体用法?Golang TCPConn.SetLinger怎么用?Golang TCPConn.SetLinger使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.SetLinger方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TCProxy
func TCProxy(srvConn, cliConn *net.TCPConn) {
// channels to wait on the close event for each connection
serverClosed := make(chan struct{}, 1)
clientClosed := make(chan struct{}, 1)
go broker(srvConn, cliConn, clientClosed)
go broker(cliConn, srvConn, serverClosed)
// wait for one half of the proxy to exit, then trigger a shutdown of the
// other half by calling CloseRead(). This will break the read loop in the
// broker and allow us to fully close the connection cleanly without a
// "use of closed network connection" error.
var waitFor chan struct{}
select {
case <-clientClosed:
// the client closed first and any more packets from the server aren't
// useful, so we can optionally SetLinger(0) here to recycle the port
// faster.
srvConn.SetLinger(0)
srvConn.CloseRead()
waitFor = serverClosed
case <-serverClosed:
cliConn.CloseRead()
waitFor = clientClosed
}
// Wait for the other connection to close.
// This "waitFor" pattern isn't required, but gives us a way to track the
// connection and ensure all copies terminate correctly; we can trigger
// stats on entry and deferred exit of this function.
<-waitFor
}
示例2: handleClient
func handleClient(source *net.TCPConn) {
atomic.AddInt32(&daemon.status.actives, 1)
defer func() {
atomic.AddInt32(&daemon.status.actives, -1)
daemon.wg.Done()
}()
// read req
err, req := ReadReq(source)
if err != nil {
source.Close()
Error("conn:%v, read req failed:%v", source.RemoteAddr(), err)
return
}
source.SetKeepAlive(true)
source.SetKeepAlivePeriod(time.Second * 60)
source.SetLinger(-1)
// judge: new conn or reuse conn
switch req := req.(type) {
case *NewConnReq:
Info("new conn request:%v", req)
onNewConn(source, req)
case *ReuseConnReq:
Info("reuse conn request:%v", req)
onReuseConn(source, req)
default:
Info("unknown request:%v", req)
source.Close()
return
}
}
示例3: handleClient
func handleClient(pf *PF, source *net.TCPConn) {
atomic.AddInt32(&pf.status.actives, 1)
defer func() {
atomic.AddInt32(&pf.status.actives, -1)
pf.wg.Done()
}()
settings := pf.settings
host := chooseHost(settings.weight, settings.Hosts)
if host == nil {
source.Close()
logger.Println("choose host failed")
return
}
dest, err := net.DialTCP("tcp", nil, host.addr)
if err != nil {
source.Close()
logger.Printf("connect to %s failed: %s", host.addr, err.Error())
return
}
source.SetKeepAlive(true)
source.SetKeepAlivePeriod(time.Second * 60)
source.SetLinger(-1)
dest.SetLinger(-1)
go forward(source, dest)
forward(dest, source)
//logger.Printf("forward finished, %v -> %v", source.RemoteAddr(), host)
}
示例4: Start
func (server *TcpServer) Start() (err error) {
for {
var conn *net.TCPConn
if conn, err = server.TCPListener.AcceptTCP(); err != nil {
return err
}
if server.keepAlive != nil {
if err := conn.SetKeepAlive(server.keepAlive.(bool)); err != nil {
return err
}
}
if server.keepAlivePeriod != nil {
if kap, ok := (net.Conn(conn)).(iKeepAlivePeriod); ok {
if err := kap.SetKeepAlivePeriod(server.keepAlivePeriod.(time.Duration)); err != nil {
return err
}
}
}
if server.linger != nil {
if err := conn.SetLinger(server.linger.(int)); err != nil {
return err
}
}
if server.noDelay != nil {
if err := conn.SetNoDelay(server.noDelay.(bool)); err != nil {
return err
}
}
if server.readBuffer != nil {
if err := conn.SetReadBuffer(server.readBuffer.(int)); err != nil {
return err
}
}
if server.writerBuffer != nil {
if err := conn.SetWriteBuffer(server.writerBuffer.(int)); err != nil {
return err
}
}
if server.deadline != nil {
if err := conn.SetDeadline(server.deadline.(time.Time)); err != nil {
return err
}
}
if server.readDeadline != nil {
if err := conn.SetReadDeadline(server.readDeadline.(time.Time)); err != nil {
return err
}
}
if server.writerDeadline != nil {
if err := conn.SetWriteDeadline(server.writerDeadline.(time.Time)); err != nil {
return err
}
}
if server.config != nil {
server.ServeTCP(tls.Client(conn, server.config))
} else {
server.ServeTCP(conn)
}
}
}
示例5: SetClientOptions
// Set TCP socket options on a new SMTP connection.
func (s *SMTPService) SetClientOptions(conn *net.TCPConn) error {
if err := conn.SetKeepAlive(false); err != nil {
log.Error("%s: SetKeepAlive: %v", conn.RemoteAddr(), err)
return err
}
if err := conn.SetLinger(-1); err != nil {
log.Error("%s: SetLinger: %v", conn.RemoteAddr(), err)
return err
}
return nil
}
示例6:
func configureConn (conn *net.TCPConn, spec *ConnectionSpec) {
// these two -- the most important -- are causing problems on my osx/64
// where a "service unavailable" pops up in the async reads
// but we absolutely need to be able to use timeouts.
// conn.SetReadTimeout(spec.rTimeout);
// conn.SetWriteTimeout(spec.wTimeout);
conn.SetLinger(spec.lingerspec);
conn.SetKeepAlive(spec.keepalive);
conn.SetReadBuffer(spec.rBufSize);
conn.SetWriteBuffer(spec.wBufSize);
}
示例7: loop
func loop(conn *net.TCPConn) {
defer conn.Close()
conn.SetLinger(5)
conn.SetKeepAlive(false)
conn.SetNoDelay(true)
now := time.Now()
conn.SetReadDeadline(now.Add(readWriteLimit))
if _, err := io.ReadFull(conn, buf); err == nil {
conn.Write(response)
}
}
示例8: Pump
func (self *Link) Pump(conn *net.TCPConn) {
conn.SetKeepAlive(true)
conn.SetLinger(-1)
self.conn = conn
self.wg.Add(1)
go self.pumpIn()
self.wg.Add(1)
go self.pumpOut()
self.wg.Wait()
Info("link(%d) closed", self.id)
}
示例9: setTCPOptions
func setTCPOptions(conn *net.TCPConn) {
var err error
if err = conn.SetLinger(0); err != nil {
l.Infoln(err)
}
if err = conn.SetNoDelay(false); err != nil {
l.Infoln(err)
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
l.Infoln(err)
}
if err = conn.SetKeepAlive(true); err != nil {
l.Infoln(err)
}
}
示例10: SetTCPOptions
// SetTCPOptions sets syncthings default TCP options on a TCP connection
func SetTCPOptions(conn *net.TCPConn) error {
var err error
if err = conn.SetLinger(0); err != nil {
return err
}
if err = conn.SetNoDelay(false); err != nil {
return err
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
return err
}
if err = conn.SetKeepAlive(true); err != nil {
return err
}
return nil
}
示例11: newTunnel
func newTunnel(conn *net.TCPConn) *Tunnel {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 60)
conn.SetLinger(-1)
// conn.SetWriteBuffer(64 * 1024)
// conn.SetReadBuffer(64 * 1024)
desc := fmt.Sprintf("tunnel[%s <-> %s]", conn.LocalAddr(), conn.RemoteAddr())
return &Tunnel{
wlock: new(sync.Mutex),
writer: NewRC4Writer(conn, options.RC4Key),
rlock: new(sync.Mutex),
reader: NewRC4Reader(bufio.NewReaderSize(conn, 8192), options.RC4Key),
conn: conn,
desc: desc,
}
}
示例12: setupSocket
func (server *Server) setupSocket(conn *net.TCPConn) (err error) {
if err = conn.SetLinger(0); err != nil {
return
}
if server.ConfigReadBuffer != 0 {
if err = conn.SetReadBuffer(int(server.ConfigReadBuffer)); err != nil {
return
}
}
if err = conn.SetKeepAlive(true); err != nil {
return
}
if err = conn.SetReadDeadline(time.Now().Add(server.ConfigIdleTimeout)); err != nil {
return
}
return
}
示例13: handleClient
func handleClient(source *net.TCPConn) {
atomic.AddInt32(&daemon.status.actives, 1)
defer func() {
atomic.AddInt32(&daemon.status.actives, -1)
daemon.wg.Done()
}()
Info("accept new connection: %v", source.RemoteAddr())
source.SetKeepAlive(true)
source.SetKeepAlivePeriod(time.Second * 60)
source.SetLinger(-1)
// read req
// set read request timeout
source.SetReadDeadline(time.Now().Add(time.Second * 30))
err, req := ReadReq(source)
if err != nil {
source.Close()
Error("conn:%v, read req failed: %v", source.RemoteAddr(), err)
return
}
// cancel read timeout
var t time.Time
source.SetReadDeadline(t)
// judge: new conn or reuse conn
switch req := req.(type) {
case *NewConnReq:
Info("new conn request:%v", req)
onNewConn(source, req)
case *ReuseConnReq:
Info("reuse conn request:%v", req)
onReuseConn(source, req)
default:
Info("unknown request:%v", req)
source.Close()
return
}
Info("connection close: %v", source.RemoteAddr())
}
示例14: ServeTCP
// ServeTCP ...
func (service *TcpService) ServeTCP(conn *net.TCPConn) (err error) {
if service.keepAlive != nil {
if err = conn.SetKeepAlive(service.keepAlive.(bool)); err != nil {
return err
}
}
if service.keepAlivePeriod != nil {
if kap, ok := (net.Conn(conn)).(iKeepAlivePeriod); ok {
if err = kap.SetKeepAlivePeriod(service.keepAlivePeriod.(time.Duration)); err != nil {
return err
}
}
}
if service.linger != nil {
if err = conn.SetLinger(service.linger.(int)); err != nil {
return err
}
}
if service.noDelay != nil {
if err = conn.SetNoDelay(service.noDelay.(bool)); err != nil {
return err
}
}
if service.readBuffer != nil {
if err = conn.SetReadBuffer(service.readBuffer.(int)); err != nil {
return err
}
}
if service.writeBuffer != nil {
if err = conn.SetWriteBuffer(service.writeBuffer.(int)); err != nil {
return err
}
}
if service.config != nil {
tlsConn := tls.Server(conn, service.config)
tlsConn.Handshake()
return service.Serve(tlsConn)
}
return service.Serve(conn)
}
示例15: proxy
// proxy brokers a connection from src to dst
func proxy(dst, src *net.TCPConn) error {
// channels to wait on the close event for each connection
serverClosed := make(chan struct{}, 1)
clientClosed := make(chan struct{}, 1)
errors := make(chan error, 2)
go broker(dst, src, clientClosed, errors)
go broker(src, dst, serverClosed, errors)
// wait for one half of the proxy to exit, then trigger a shutdown of the
// other half by calling CloseRead(). This will break the read loop in the
// broker and allow us to fully close the connection cleanly without a
// "use of closed network connection" error.
var waitFor chan struct{}
select {
case <-clientClosed:
// the client closed first and any more packets from the server aren't
// useful, so we can optionally SetLinger(0) here to recycle the port
// faster.
dst.SetLinger(0)
dst.Close()
waitFor = serverClosed
case <-serverClosed:
src.Close()
waitFor = clientClosed
case err := <-errors:
src.Close()
dst.SetLinger(0)
dst.Close()
return err
}
// Wait for the other connection to close.
<-waitFor
return nil
}