2016-05-13 06:48:53 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2014-11-24 11:39:55 +03:00
|
|
|
|
2014-11-24 08:12:02 +03:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-11-24 10:37:54 +03:00
|
|
|
// NewTimeoutListener returns a listener that listens on the given address.
|
|
|
|
// If read/write on the accepted connection blocks longer than its time limit,
|
|
|
|
// it will return timeout error.
|
2017-04-07 07:01:33 +03:00
|
|
|
func NewTimeoutListener(addr string, scheme string, tlsinfo *TLSInfo, rdtimeoutd, wtimeoutd time.Duration) (net.Listener, error) {
|
2016-07-20 02:47:14 +03:00
|
|
|
ln, err := newListener(addr, scheme)
|
2014-11-24 10:37:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-20 02:47:14 +03:00
|
|
|
ln = &rwTimeoutListener{
|
2014-11-24 10:37:54 +03:00
|
|
|
Listener: ln,
|
|
|
|
rdtimeoutd: rdtimeoutd,
|
|
|
|
wtimeoutd: wtimeoutd,
|
2016-07-20 02:47:14 +03:00
|
|
|
}
|
2018-05-10 01:06:11 +03:00
|
|
|
if ln, err = wrapTLS(scheme, tlsinfo, ln); err != nil {
|
2016-07-20 02:47:14 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ln, nil
|
2014-11-24 10:37:54 +03:00
|
|
|
}
|
|
|
|
|
2014-11-24 08:12:02 +03:00
|
|
|
type rwTimeoutListener struct {
|
|
|
|
net.Listener
|
|
|
|
wtimeoutd time.Duration
|
|
|
|
rdtimeoutd time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rwln *rwTimeoutListener) Accept() (net.Conn, error) {
|
|
|
|
c, err := rwln.Listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return timeoutConn{
|
|
|
|
Conn: c,
|
|
|
|
wtimeoutd: rwln.wtimeoutd,
|
|
|
|
rdtimeoutd: rwln.rdtimeoutd,
|
|
|
|
}, nil
|
|
|
|
}
|