winlin

limit the user-space buffer size to 128KB, 128MB for 1k publishers.

@@ -429,7 +429,8 @@ void SrsPublishRecvThread::set_socket_buffer(int sleep_ms) @@ -429,7 +429,8 @@ void SrsPublishRecvThread::set_socket_buffer(int sleep_ms)
429 // sleep 800ms for small bytes, the buffer should set to: 429 // sleep 800ms for small bytes, the buffer should set to:
430 // 800*1000/8=100000B(about 128KB). 430 // 800*1000/8=100000B(about 128KB).
431 // 2000*3000/8=750000B(about 732KB). 431 // 2000*3000/8=750000B(about 732KB).
432 - int kbps = 3000; 432 + // 2000*5000/8=1250000B(about 1220KB).
  433 + int kbps = 5000;
433 int socket_buffer_size = sleep_ms * kbps / 8; 434 int socket_buffer_size = sleep_ms * kbps / 8;
434 435
435 // socket recv buffer, system will double it. 436 // socket recv buffer, system will double it.
@@ -28,8 +28,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,8 +28,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_kernel_utility.hpp> 28 #include <srs_kernel_utility.hpp>
29 #include <srs_core_performance.hpp> 29 #include <srs_core_performance.hpp>
30 30
31 -// the default recv buffer size  
32 -#define SRS_DEFAULT_RECV_BUFFER_SIZE 32768 31 +// the default recv buffer size, 128KB.
  32 +#define SRS_DEFAULT_RECV_BUFFER_SIZE 131072
  33 +
  34 +// limit user-space buffer to 256KB, for 3Mbps stream delivery.
  35 +// 800*2000/8=200000B(about 195KB).
  36 +// @remark it's ok for higher stream, the buffer is ok for one chunk is 256KB.
  37 +#define SRS_MAX_SOCKET_BUFFER 262144
33 38
34 // the max header size, 39 // the max header size,
35 // @see SrsProtocol::read_message_header(). 40 // @see SrsProtocol::read_message_header().
@@ -100,15 +105,21 @@ SrsFastBuffer::SrsFastBuffer() @@ -100,15 +105,21 @@ SrsFastBuffer::SrsFastBuffer()
100 105
101 void SrsFastBuffer::set_buffer(int buffer_size) 106 void SrsFastBuffer::set_buffer(int buffer_size)
102 { 107 {
  108 + // the user-space buffer size limit to a max value.
  109 + int nb_max_buf = srs_min(buffer_size, SRS_MAX_SOCKET_BUFFER);
  110 + if (nb_max_buf < buffer_size) {
  111 + srs_warn("limit the user-space buffer from %d to %d", buffer_size, nb_max_buf);
  112 + }
  113 +
103 // only realloc when buffer changed bigger 114 // only realloc when buffer changed bigger
104 - if (buffer_size <= nb_buffer) { 115 + if (nb_max_buf <= nb_buffer) {
105 return; 116 return;
106 } 117 }
107 118
108 int start = p - buffer; 119 int start = p - buffer;
109 int cap = end - p; 120 int cap = end - p;
110 121
111 - char* buf = new char[buffer_size]; 122 + char* buf = new char[nb_max_buf];
112 if (cap > 0) { 123 if (cap > 0) {
113 memcpy(buf, buffer, nb_buffer); 124 memcpy(buf, buffer, nb_buffer);
114 } 125 }