本文整理汇总了Golang中net.TCPConn.SetKeepAlivePeriod方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPConn.SetKeepAlivePeriod方法的具体用法?Golang TCPConn.SetKeepAlivePeriod怎么用?Golang TCPConn.SetKeepAlivePeriod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPConn
的用法示例。
在下文中一共展示了TCPConn.SetKeepAlivePeriod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
}
示例2: startLink
func (h *Hub) startLink(l *link, conn *net.TCPConn) {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 60)
l.setConn(conn)
Info("link(%d) start: %v", l.id, conn.RemoteAddr())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
data, err := l.read()
if err != nil {
if err != errPeerClosed {
h.SendCmd(l.id, LINK_CLOSE_SEND)
}
break
}
h.Send(l.id, data)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
err := l._write()
if err != errPeerClosed {
h.SendCmd(l.id, LINK_CLOSE_RECV)
}
}()
wg.Wait()
Info("link(%d) close", l.id)
}
示例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: SetDefaultOptions
func (this *GoServer) SetDefaultOptions(conn *net.TCPConn) {
conn.SetKeepAlive(true)
interval, err := time.ParseDuration("45s")
if err != nil {
conn.SetKeepAlivePeriod(interval)
}
conn.SetNoDelay(true)
}
示例5: 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 err := conn.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)
}
}
}
示例6: newTunnel
func newTunnel(conn *net.TCPConn) *Tunnel {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 10) //we do not keep so long
var tun Tunnel
tun.Conn = &Conn{conn, bufio.NewReaderSize(conn, PacketSize), bufio.NewWriterSize(conn, PacketSize), nil, nil}
Info("new tunnel:%s", tun)
return &tun
}
示例7: newTunnel
func newTunnel(conn *net.TCPConn) *Tunnel {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 180)
var tun Tunnel
tun.Conn = &Conn{conn, bufio.NewReaderSize(conn, 64*1024), bufio.NewWriterSize(conn, 64*1024), nil, nil}
Info("new tunnel:%s", tun)
return &tun
}
示例8: 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)
}
}
示例9: Pump
func (link *Link) Pump(conn *net.TCPConn) {
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second * 60)
link.conn = conn
link.wg.Add(1)
go link.pumpIn()
link.wg.Add(1)
go link.pumpOut()
link.wg.Wait()
Info("link(%d) closed", link.id)
link.hub.deleteLink(link.id)
}
示例10: 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,
}
}
示例11: 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
}
示例12: proxyTCPStream
func (p *Proxy) proxyTCPStream(ctx context.Context, src *net.TCPConn) {
srcRemoteAddr := src.RemoteAddr().(*net.TCPAddr)
srcLocalAddr := src.LocalAddr().(*net.TCPAddr)
route := p.routes.GetTable().Lookup(protocols.TCP,
srcRemoteAddr.IP, srcLocalAddr.IP,
uint16(srcRemoteAddr.Port), uint16(srcLocalAddr.Port))
if route == nil {
src.Close()
return
}
go func() {
dstAddr := net.TCPAddr{
IP: route.Outbound.DstIP,
Port: int(route.Outbound.DstPort),
}
dst, err := net.DialTCP("tcp", nil, &dstAddr)
if err != nil {
src.Close()
return
}
dst.SetKeepAlivePeriod(10 * time.Second)
src.SetKeepAlivePeriod(10 * time.Second)
go func() {
<-ctx.Done()
src.Close()
dst.Close()
}()
go func() {
defer dst.CloseWrite()
defer src.CloseRead()
io.Copy(dst, src)
}()
go func() {
defer src.CloseWrite()
defer dst.CloseRead()
io.Copy(src, dst)
}()
}()
}
示例13: readDiamondMetrics
// readDiamondMetrics reads from the connection
func (d *Diamond) readDiamondMetrics(conn *net.TCPConn) {
defer conn.Close()
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second)
reader := bufio.NewReader(conn)
d.log.Info("Connection started: ", conn.RemoteAddr())
for {
line, err := reader.ReadBytes('\n')
if err != nil {
d.log.Warn("Error while reading diamond metrics", err)
break
}
d.log.Debug("Read: ", string(line))
d.incoming <- line
}
d.log.Info("Connection closed: ", conn.RemoteAddr())
}
示例14: readDiamondMetrics
// readDiamondMetrics reads from the connection
func (d Diamond) readDiamondMetrics(conn *net.TCPConn) {
defer conn.Close()
conn.SetKeepAlive(true)
conn.SetKeepAlivePeriod(time.Second)
reader := bufio.NewReader(conn)
log.Info("Diamond collector connection started: ", conn.RemoteAddr())
for {
// TODO: verify that timeout is actually working.
conn.SetDeadline(time.Now().Add(1e9))
line, err := reader.ReadBytes('\n')
if err != nil {
break
}
log.Debug("Read from Diamond collector: ", string(line))
d.incoming <- line
}
log.Info("Diamond collector connection closed: ", conn.RemoteAddr())
}
示例15: handleConn
func handleConn(source *net.TCPConn) {
var destList []*DestList
source.SetKeepAlive(true)
source.SetKeepAlivePeriod(time.Second * 60)
for _, host := range options.hostList.HostList {
dest, err := net.Dial("tcp", host.Addr)
if err != nil {
source.Close()
log.Printf("connect to %s failed: %s", host.Addr, err.Error())
return
}
tmp := new(DestList)
tmp.Conn = dest.(*net.TCPConn)
tmp.Discard = host.Discard
destList = append(destList, tmp)
}
go sendDest(source, destList)
sendSource(destList, source)
}