diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-04 15:38:27 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-05-04 15:38:27 +0200 |
commit | bb3a79c8b71d6d4e2a4269bdcffb616b2db9d619 (patch) | |
tree | 1e750c48f39c56deedfd0f7484dbea28215063f3 | |
parent | 6adf9a4734f15a2c0fa199897f76ded4659b83b7 (diff) | |
download | sink-bb3a79c8b71d6d4e2a4269bdcffb616b2db9d619.tar.gz sink-bb3a79c8b71d6d4e2a4269bdcffb616b2db9d619.zip |
Limit the buffering on write.
Otherwise the system becomes rather unresponsive under load.
-rw-r--r-- | common/commands.cpp | 22 | ||||
-rw-r--r-- | common/commands.h | 8 |
2 files changed, 16 insertions, 14 deletions
diff --git a/common/commands.cpp b/common/commands.cpp index eeb7f08..24f2017 100644 --- a/common/commands.cpp +++ b/common/commands.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | 2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> |
3 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | * | 4 | * |
4 | * This library is free software; you can redistribute it and/or | 5 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public | 6 | * modify it under the terms of the GNU Lesser General Public |
@@ -20,7 +21,7 @@ | |||
20 | 21 | ||
21 | #include "commands.h" | 22 | #include "commands.h" |
22 | 23 | ||
23 | #include <QIODevice> | 24 | #include <QLocalSocket> |
24 | #include <log.h> | 25 | #include <log.h> |
25 | 26 | ||
26 | namespace Sink { | 27 | namespace Sink { |
@@ -73,19 +74,19 @@ int headerSize() | |||
73 | return sizeof(int) + (sizeof(uint) * 2); | 74 | return sizeof(int) + (sizeof(uint) * 2); |
74 | } | 75 | } |
75 | 76 | ||
76 | void write(QIODevice *device, int messageId, int commandId) | 77 | void write(QLocalSocket *device, int messageId, int commandId) |
77 | { | 78 | { |
78 | write(device, messageId, commandId, 0, 0); | 79 | write(device, messageId, commandId, 0, 0); |
79 | } | 80 | } |
80 | 81 | ||
81 | static void write(QIODevice *device, const char *buffer, uint size) | 82 | static void write(QLocalSocket *device, const char *buffer, uint size) |
82 | { | 83 | { |
83 | if (device->write(buffer, size) < 0) { | 84 | if (device->write(buffer, size) < 0) { |
84 | SinkWarningCtx(Sink::Log::Context{"commands"}) << "Error while writing " << device->errorString(); | 85 | SinkWarningCtx(Sink::Log::Context{"commands"}) << "Error while writing " << device->errorString(); |
85 | } | 86 | } |
86 | } | 87 | } |
87 | 88 | ||
88 | void write(QIODevice *device, int messageId, int commandId, const char *buffer, uint size) | 89 | void write(QLocalSocket *device, int messageId, int commandId, const char *buffer, uint size) |
89 | { | 90 | { |
90 | if (size > 0 && !buffer) { | 91 | if (size > 0 && !buffer) { |
91 | size = 0; | 92 | size = 0; |
@@ -97,15 +98,16 @@ void write(QIODevice *device, int messageId, int commandId, const char *buffer, | |||
97 | if (buffer) { | 98 | if (buffer) { |
98 | write(device, buffer, size); | 99 | write(device, buffer, size); |
99 | } | 100 | } |
101 | //The default implementation will happily buffer 200k bytes before sending it out which doesn't make the sytem exactly responsive. | ||
102 | //1k is arbitrary, but fits a bunch of messages at least. | ||
103 | if (device->bytesToWrite() > 1000) { | ||
104 | device->flush(); | ||
105 | } | ||
100 | } | 106 | } |
101 | 107 | ||
102 | void write(QIODevice *device, int messageId, int commandId, flatbuffers::FlatBufferBuilder &fbb) | 108 | void write(QLocalSocket *device, int messageId, int commandId, flatbuffers::FlatBufferBuilder &fbb) |
103 | { | 109 | { |
104 | const int dataSize = fbb.GetSize(); | 110 | write(device, messageId, commandId, (const char *)fbb.GetBufferPointer(), fbb.GetSize()); |
105 | write(device, (const char *)&messageId, sizeof(int)); | ||
106 | write(device, (const char *)&commandId, sizeof(int)); | ||
107 | write(device, (const char *)&dataSize, sizeof(int)); | ||
108 | write(device, (const char *)fbb.GetBufferPointer(), dataSize); | ||
109 | } | 111 | } |
110 | 112 | ||
111 | } // namespace Commands | 113 | } // namespace Commands |
diff --git a/common/commands.h b/common/commands.h index 6d5d39b..1548eac 100644 --- a/common/commands.h +++ b/common/commands.h | |||
@@ -24,7 +24,7 @@ | |||
24 | #include <flatbuffers/flatbuffers.h> | 24 | #include <flatbuffers/flatbuffers.h> |
25 | #include <QByteArray> | 25 | #include <QByteArray> |
26 | 26 | ||
27 | class QIODevice; | 27 | class QLocalSocket; |
28 | 28 | ||
29 | namespace Sink { | 29 | namespace Sink { |
30 | 30 | ||
@@ -55,9 +55,9 @@ enum CommandIds | |||
55 | QByteArray name(int commandId); | 55 | QByteArray name(int commandId); |
56 | 56 | ||
57 | int SINK_EXPORT headerSize(); | 57 | int SINK_EXPORT headerSize(); |
58 | void SINK_EXPORT write(QIODevice *device, int messageId, int commandId); | 58 | void SINK_EXPORT write(QLocalSocket *device, int messageId, int commandId); |
59 | void SINK_EXPORT write(QIODevice *device, int messageId, int commandId, const char *buffer, uint size); | 59 | void SINK_EXPORT write(QLocalSocket *device, int messageId, int commandId, const char *buffer, uint size); |
60 | void SINK_EXPORT write(QIODevice *device, int messageId, int commandId, flatbuffers::FlatBufferBuilder &fbb); | 60 | void SINK_EXPORT write(QLocalSocket *device, int messageId, int commandId, flatbuffers::FlatBufferBuilder &fbb); |
61 | } | 61 | } |
62 | 62 | ||
63 | } // namespace Sink | 63 | } // namespace Sink |