Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ale
autoradio
Commits
0f759401
Commit
0f759401
authored
Apr 13, 2019
by
ale
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Drop the whole connection on error in the proxy
Half-closed connections will get sources stuck.
parent
f3689dfd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
7 deletions
+21
-7
node/proxy.go
node/proxy.go
+21
-7
No files found.
node/proxy.go
View file @
0f759401
...
...
@@ -150,11 +150,18 @@ func doIcecastProxy(rw http.ResponseWriter, req *http.Request, target *url.URL,
// (>1.11), this is quite fast as io.CopyBuffer uses the splice()
// system call internally (in exchange we lose the ability to figure
// out which connection is the source of the error).
func
copyStream
(
out
,
in
*
net
.
TCPConn
,
promCounter
prometheus
.
Counter
,
cntr
*
uint64
)
{
func
copyStream
(
tag
string
,
out
,
in
*
net
.
TCPConn
,
promCounter
prometheus
.
Counter
,
cntr
*
uint64
)
{
buf
:=
getBuf
()
defer
releaseBuf
(
buf
)
defer
in
.
CloseRead
()
//nolint
defer
out
.
CloseWrite
()
//nolint
// We used to do this in order to support half-closed connections.
//defer in.CloseRead() //nolint
//defer out.CloseWrite() //nolint
// Instead we do this and shut down the entire connection on error.
// We end up calling Close() twice but that's not a huge problem.
defer
in
.
Close
()
//nolint
defer
out
.
Close
()
//nolint
for
{
n
,
err
:=
io
.
CopyBuffer
(
out
,
in
,
buf
)
...
...
@@ -163,14 +170,21 @@ func copyStream(out, in *net.TCPConn, promCounter prometheus.Counter, cntr *uint
atomic
.
AddUint64
(
cntr
,
uint64
(
n
))
}
if
err
!=
nil
{
if
err
!=
io
.
EOF
{
log
.
Printf
(
"http: proxy error
: %v"
,
err
)
if
!
isCloseError
(
err
)
{
log
.
Printf
(
"http: proxy error
(%s): %v"
,
tag
,
err
)
}
return
}
}
}
// This is a bad implementation (see https://github.com/golang/go/issues/4373
// for some notes on why it is a layering violation), and we could replace it
// with an atomic 'closing' flag.
func
isCloseError
(
err
error
)
bool
{
return
strings
.
Contains
(
err
.
Error
(),
"use of closed network connection"
)
}
// Simple two-way TCP proxy that copies data in both directions and
// can shutdown each direction of the connection independently.
func
handleProxy
(
conn
*
net
.
TCPConn
,
upstream
*
net
.
TCPConn
,
streamName
string
)
{
...
...
@@ -181,11 +195,11 @@ func handleProxy(conn *net.TCPConn, upstream *net.TCPConn, streamName string) {
// Instrument both directions of the stream, but let the
// bandwidth estimator count only the bytes sent to the user.
go
func
()
{
copyStream
(
conn
,
upstream
,
streamSentBytes
.
WithLabelValues
(
streamName
),
&
bwBytesSent
)
copyStream
(
"upstream -> client"
,
conn
,
upstream
,
streamSentBytes
.
WithLabelValues
(
streamName
),
&
bwBytesSent
)
wg
.
Done
()
}()
go
func
()
{
copyStream
(
upstream
,
conn
,
streamRcvdBytes
.
WithLabelValues
(
streamName
),
nil
)
copyStream
(
"client -> upstream"
,
upstream
,
conn
,
streamRcvdBytes
.
WithLabelValues
(
streamName
),
nil
)
wg
.
Done
()
}()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment