Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
autoradio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
autoradio
Commits
0f759401
Commit
0f759401
authored
5 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Drop the whole connection on error in the proxy
Half-closed connections will get sources stuck.
parent
f3689dfd
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
v2.0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
node/proxy.go
+21
-7
21 additions, 7 deletions
node/proxy.go
with
21 additions
and
7 deletions
node/proxy.go
+
21
−
7
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
()
}()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment