Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
bbcdb318
Commit
bbcdb318
authored
May 09, 2018
by
justcoding121
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
order by internal, private methods
parent
42cd8e76
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
73 deletions
+72
-73
TcpConnectionFactory.cs
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
+72
-73
No files found.
Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs
View file @
bbcdb318
...
@@ -107,6 +107,78 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -107,6 +107,78 @@ namespace Titanium.Web.Proxy.Network.Tcp
return
connection
;
return
connection
;
}
}
/// <summary>
/// Release connection back to cache.
/// </summary>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
internal
void
Release
(
TcpServerConnection
connection
,
bool
close
=
false
)
{
if
(
close
||
connection
.
IsWinAuthenticated
)
{
disposalBag
.
Add
(
connection
);
return
;
}
connection
.
LastAccess
=
DateTime
.
Now
;
@lock
.
Wait
();
while
(
true
)
{
if
(
cache
.
TryGetValue
(
connection
.
CacheKey
,
out
var
existingConnections
))
{
existingConnections
.
Enqueue
(
connection
);
break
;
}
if
(
cache
.
TryAdd
(
connection
.
CacheKey
,
new
ConcurrentQueue
<
TcpServerConnection
>(
new
[]
{
connection
})))
{
break
;
}
}
@lock
.
Release
();
}
private
async
Task
clearOutdatedConnections
()
{
while
(
runCleanUpTask
)
{
foreach
(
var
item
in
cache
)
{
var
queue
=
item
.
Value
;
while
(
queue
.
TryDequeue
(
out
var
connection
))
{
var
cutOff
=
DateTime
.
Now
.
AddSeconds
(-
1
*
server
.
ConnectionTimeOutSeconds
);
if
(
connection
.
LastAccess
<
cutOff
)
{
disposalBag
.
Add
(
connection
);
continue
;
}
queue
.
Enqueue
(
connection
);
break
;
}
}
//clear empty queues
await
@lock
.
WaitAsync
();
var
emptyKeys
=
cache
.
Where
(
x
=>
x
.
Value
.
Count
==
0
).
Select
(
x
=>
x
.
Key
).
ToList
();
foreach
(
string
key
in
emptyKeys
)
{
cache
.
TryRemove
(
key
,
out
var
_
);
}
@lock
.
Release
();
while
(
disposalBag
.
TryTake
(
out
var
connection
))
{
connection
?.
Dispose
();
}
//cleanup every ten seconds by default
await
Task
.
Delay
(
1000
*
10
);
}
}
/// <summary>
/// <summary>
/// Creates a TCP connection to server
/// Creates a TCP connection to server
/// </summary>
/// </summary>
...
@@ -244,79 +316,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
...
@@ -244,79 +316,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
Version
=
httpVersion
Version
=
httpVersion
};
};
}
}
/// <summary>
/// Release connection back to cache.
/// </summary>
/// <param name="connection">The Tcp server connection to return.</param>
/// <param name="close">Should we just close the connection instead of reusing?</param>
internal
void
Release
(
TcpServerConnection
connection
,
bool
close
=
false
)
{
if
(
close
||
connection
.
IsWinAuthenticated
)
{
disposalBag
.
Add
(
connection
);
return
;
}
connection
.
LastAccess
=
DateTime
.
Now
;
@lock
.
Wait
();
while
(
true
)
{
if
(
cache
.
TryGetValue
(
connection
.
CacheKey
,
out
var
existingConnections
))
{
existingConnections
.
Enqueue
(
connection
);
break
;
}
if
(
cache
.
TryAdd
(
connection
.
CacheKey
,
new
ConcurrentQueue
<
TcpServerConnection
>(
new
[]
{
connection
})))
{
break
;
}
}
@lock
.
Release
();
}
private
async
Task
clearOutdatedConnections
()
{
while
(
runCleanUpTask
)
{
foreach
(
var
item
in
cache
)
{
var
queue
=
item
.
Value
;
while
(
queue
.
TryDequeue
(
out
var
connection
))
{
var
cutOff
=
DateTime
.
Now
.
AddSeconds
(-
1
*
server
.
ConnectionTimeOutSeconds
);
if
(
connection
.
LastAccess
<
cutOff
)
{
disposalBag
.
Add
(
connection
);
continue
;
}
queue
.
Enqueue
(
connection
);
break
;
}
}
//clear empty queues
await
@lock
.
WaitAsync
();
var
emptyKeys
=
cache
.
Where
(
x
=>
x
.
Value
.
Count
==
0
).
Select
(
x
=>
x
.
Key
).
ToList
();
foreach
(
string
key
in
emptyKeys
)
{
cache
.
TryRemove
(
key
,
out
var
_
);
}
@lock
.
Release
();
while
(
disposalBag
.
TryTake
(
out
var
connection
))
{
connection
?.
Dispose
();
}
//cleanup every ten seconds by default
await
Task
.
Delay
(
1000
*
10
);
}
}
/// <summary>
/// <summary>
/// Check if a TcpClient is good to be used.
/// Check if a TcpClient is good to be used.
/// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx
/// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx
...
...
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