Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
f78f293a
Commit
f78f293a
authored
Sep 26, 2018
by
Ian Roddis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implementing deque for write queue to allow for multiple pending writes and to preserve write order
parent
7bb54435
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
82 additions
and
71 deletions
+82
-71
include/pistache/transport.h
include/pistache/transport.h
+5
-6
src/common/transport.cc
src/common/transport.cc
+76
-64
tests/streaming_test.cc
tests/streaming_test.cc
+1
-1
No files found.
include/pistache/transport.h
View file @
f78f293a
...
...
@@ -12,6 +12,8 @@
#include <pistache/async.h>
#include <pistache/stream.h>
#include <deque>
namespace
Pistache
{
namespace
Tcp
{
...
...
@@ -199,7 +201,7 @@ private:
fd becomes ready again, we can write everything
*/
PollableQueue
<
WriteEntry
>
writesQueue
;
std
::
unordered_map
<
Fd
,
WriteEntry
>
toWrite
;
std
::
unordered_map
<
Fd
,
std
::
deque
<
WriteEntry
>
>
toWrite
;
PollableQueue
<
TimerEntry
>
timersQueue
;
std
::
unordered_map
<
Fd
,
TimerEntry
>
timers
;
...
...
@@ -227,11 +229,8 @@ private:
void
armTimerMsImpl
(
TimerEntry
entry
);
void
asyncWriteImpl
(
Fd
fd
,
WriteEntry
&
entry
,
WriteStatus
status
=
FirstTry
);
void
asyncWriteImpl
(
Fd
fd
,
int
flags
,
const
BufferHolder
&
buffer
,
Async
::
Deferred
<
ssize_t
>
deferred
,
WriteStatus
status
=
FirstTry
);
// This will attempt to drain the write queue for the fd
void
asyncWriteImpl
(
Fd
fd
);
void
handlePeerDisconnection
(
const
std
::
shared_ptr
<
Peer
>&
peer
);
void
handleIncoming
(
const
std
::
shared_ptr
<
Peer
>&
peer
);
...
...
src/common/transport.cc
View file @
f78f293a
...
...
@@ -99,8 +99,8 @@ Transport::onReady(const Aio::FdSet& fds) {
reactor
()
->
modifyFd
(
key
(),
fd
,
NotifyOn
::
Read
,
Polling
::
Mode
::
Edge
);
auto
&
write
=
it
->
second
;
asyncWriteImpl
(
fd
,
write
,
Retry
);
// Try to drain the queue
asyncWriteImpl
(
fd
);
}
}
}
...
...
@@ -164,80 +164,90 @@ Transport::handlePeerDisconnection(const std::shared_ptr<Peer>& peer) {
throw
std
::
runtime_error
(
"Could not find peer to erase"
);
peers
.
erase
(
it
);
toWrite
.
erase
(
fd
);
close
(
fd
);
}
void
Transport
::
asyncWriteImpl
(
Fd
fd
,
Transport
::
WriteEntry
&
entry
,
WriteStatus
status
)
{
asyncWriteImpl
(
fd
,
entry
.
flags
,
entry
.
buffer
,
std
::
move
(
entry
.
deferred
),
status
);
}
void
Transport
::
asyncWriteImpl
(
Fd
fd
,
int
flags
,
const
BufferHolder
&
buffer
,
Async
::
Deferred
<
ssize_t
>
deferred
,
WriteStatus
status
)
{
auto
cleanUp
=
[
&
]()
{
// Clean up buffers
auto
&
wq
=
toWrite
[
fd
];
while
(
wq
.
size
()
>
0
)
{
auto
&
entry
=
wq
.
front
();
const
BufferHolder
&
buffer
=
entry
.
buffer
;
if
(
buffer
.
isRaw
())
{
auto
raw
=
buffer
.
raw
();
if
(
raw
.
isOwned
)
delete
[]
raw
.
data
;
}
}
toWrite
.
erase
(
fd
);
if
(
status
==
Retry
)
toWrite
.
erase
(
fd
);
};
size_t
totalWritten
=
buffer
.
offset
();
for
(;;)
{
ssize_t
bytesWritten
=
0
;
auto
len
=
buffer
.
size
()
-
totalWritten
;
if
(
buffer
.
isRaw
())
{
auto
raw
=
buffer
.
raw
();
auto
ptr
=
raw
.
data
+
totalWritten
;
bytesWritten
=
::
send
(
fd
,
ptr
,
len
,
flags
|
MSG_NOSIGNAL
);
}
else
{
auto
file
=
buffer
.
fd
();
off_t
offset
=
totalWritten
;
bytesWritten
=
::
sendfile
(
fd
,
file
,
&
offset
,
len
);
}
if
(
bytesWritten
<
0
)
{
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
// save for a future retry with the totalWritten offset.
if
(
status
==
Retry
)
{
toWrite
.
erase
(
fd
);
}
toWrite
.
insert
(
std
::
make_pair
(
fd
,
WriteEntry
(
std
::
move
(
deferred
),
buffer
.
detach
(
totalWritten
),
flags
)));
close
(
fd
);
}
reactor
()
->
modifyFd
(
key
(),
fd
,
NotifyOn
::
Read
|
NotifyOn
::
Write
,
Polling
::
Mode
::
Edge
);
void
Transport
::
asyncWriteImpl
(
Fd
fd
)
{
auto
&
wq
=
toWrite
[
fd
];
while
(
wq
.
size
()
>
0
)
{
auto
&
entry
=
wq
.
front
();
int
flags
=
entry
.
flags
;
const
BufferHolder
&
buffer
=
entry
.
buffer
;
Async
::
Deferred
<
ssize_t
>
deferred
=
std
::
move
(
entry
.
deferred
);
auto
cleanUp
=
[
&
]()
{
if
(
buffer
.
isRaw
())
{
auto
raw
=
buffer
.
raw
();
if
(
raw
.
isOwned
)
delete
[]
raw
.
data
;
}
else
{
cleanUp
();
deferred
.
reject
(
Pistache
::
Error
::
system
(
"Could not write data"
));
wq
.
pop_front
();
if
(
wq
.
size
()
==
0
)
{
toWrite
.
erase
(
fd
);
reactor
()
->
modifyFd
(
key
(),
fd
,
NotifyOn
::
Read
,
Polling
::
Mode
::
Edge
);
}
break
;
}
else
{
totalWritten
+=
bytesWritten
;
if
(
totalWritten
>=
buffer
.
size
())
{
cleanUp
();
if
(
buffer
.
isFile
())
{
// done with the file buffer, nothing else knows whether to
// close it with the way the code is written.
::
close
(
buffer
.
fd
());
};
bool
halt
=
false
;
size_t
totalWritten
=
buffer
.
offset
();
for
(;;)
{
ssize_t
bytesWritten
=
0
;
auto
len
=
buffer
.
size
()
-
totalWritten
;
if
(
buffer
.
isRaw
())
{
auto
raw
=
buffer
.
raw
();
auto
ptr
=
raw
.
data
+
totalWritten
;
bytesWritten
=
::
send
(
fd
,
ptr
,
len
,
flags
|
MSG_NOSIGNAL
);
}
else
{
auto
file
=
buffer
.
fd
();
off_t
offset
=
totalWritten
;
bytesWritten
=
::
sendfile
(
fd
,
file
,
&
offset
,
len
);
}
if
(
bytesWritten
<
0
)
{
if
(
errno
==
EAGAIN
||
errno
==
EWOULDBLOCK
)
{
wq
.
pop_front
();
wq
.
push_front
(
WriteEntry
(
std
::
move
(
deferred
),
buffer
.
detach
(
totalWritten
),
flags
));
reactor
()
->
modifyFd
(
key
(),
fd
,
NotifyOn
::
Read
|
NotifyOn
::
Write
,
Polling
::
Mode
::
Edge
);
}
else
{
cleanUp
();
deferred
.
reject
(
Pistache
::
Error
::
system
(
"Could not write data"
));
halt
=
true
;
}
// Cast to match the type of defered template
// to avoid a BadType exception
deferred
.
resolve
(
static_cast
<
ssize_t
>
(
totalWritten
));
break
;
}
else
{
totalWritten
+=
bytesWritten
;
if
(
totalWritten
>=
buffer
.
size
())
{
cleanUp
();
if
(
buffer
.
isFile
())
{
// done with the file buffer, nothing else knows whether to
// close it with the way the code is written.
::
close
(
buffer
.
fd
());
}
// Cast to match the type of defered template
// to avoid a BadType exception
deferred
.
resolve
(
static_cast
<
ssize_t
>
(
totalWritten
));
break
;
}
}
}
if
(
halt
)
break
;
}
}
...
...
@@ -299,7 +309,9 @@ Transport::handleWriteQueue() {
if
(
!
entry
)
break
;
auto
&
write
=
entry
->
data
();
asyncWriteImpl
(
write
.
peerFd
,
write
);
auto
fd
=
write
.
peerFd
;
toWrite
[
fd
].
push_back
(
std
::
move
(
write
));
reactor
()
->
modifyFd
(
key
(),
fd
,
NotifyOn
::
Read
|
NotifyOn
::
Write
,
Polling
::
Mode
::
Edge
);
}
}
...
...
tests/streaming_test.cc
View file @
f78f293a
...
...
@@ -14,7 +14,7 @@ using namespace Pistache;
static
const
size_t
N_LETTERS
=
26
;
static
const
size_t
LETTER_REPEATS
=
100000
;
static
const
size_t
SET_REPEATS
=
10
;
static
const
uint16_t
PORT
=
908
0
;
static
const
uint16_t
PORT
=
908
2
;
void
dumpData
(
const
Rest
::
Request
&
req
,
Http
::
ResponseWriter
response
)
{
UNUSED
(
req
);
...
...
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