-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcp_listeners.go
37 lines (32 loc) · 907 Bytes
/
tcp_listeners.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2017, Janoš Guljaš <janos@resenje.org>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"net"
"time"
)
// TCPKeepAliveListener sets TCP keep alive period.
type TCPKeepAliveListener struct {
*net.TCPListener
}
// NewTCPKeepAliveListener creates TCPKeepAliveListener
// from net.TCPListener.
func NewTCPKeepAliveListener(listener *net.TCPListener) TCPKeepAliveListener {
return TCPKeepAliveListener{TCPListener: listener}
}
// Accept accepts TCP connection and sets TCP keep alive period
func (ln TCPKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
if err := tc.SetKeepAlive(true); err != nil {
return nil, err
}
if err := tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
return nil, err
}
return tc, nil
}