Skip to content

Commit

Permalink
reduce maxBufSize to avoid moving of bufer to heap
Browse files Browse the repository at this point in the history
  • Loading branch information
czeumer committed Jul 13, 2023
1 parent e5137e9 commit 08b9792
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions ossec/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const (
HOST_INFO = 10 /* Host information log (from nmap or similar) */
OSSEC_RL = 11 /* OSSEC rule */

maxBufferSize = 1024 * 1024 * 10
maxBufferSize = 1024 * 1024 * 2
ReadWaitTimeout = time.Duration(30 * time.Second)
ReadImmediateTimeout = time.Duration(1 * time.Second)
)
Expand Down Expand Up @@ -819,14 +819,15 @@ func (a *Client) readServerResponse(timeout time.Duration) error {

func readResponse(timeout time.Duration, a *Client, totallyRead int, messagesRead int, buf *bytes.Buffer) (int, bool, error) {
deadline := time.Now().Add(timeout)
err := a.conn.SetReadDeadline(deadline)
if err != nil {
a.logger.Warn("setReadDeadlineFailed", zap.Any("agentId", a.AgentID), zap.Any("deadline", deadline), zap.Error(err))
return 0, true, err
}

buffer := make([]byte, maxBufferSize)
for {
err := a.conn.SetReadDeadline(deadline)
if err != nil {
a.logger.Warn("setReadDeadlineFailed", zap.Any("agentId", a.AgentID), zap.Any("deadline", deadline), zap.Error(err))
return 0, true, err
}

nRead, err := a.conn.Read(buffer)
a.receivedBytes = a.receivedBytes + uint64(nRead)
a.receivedBytesTotal = a.receivedBytesTotal + uint64(nRead)
Expand All @@ -853,12 +854,6 @@ func readResponse(timeout time.Duration, a *Client, totallyRead int, messagesRea

buf.Write(buffer[:nRead])
totallyRead += nRead
deadline = time.Now().Add(ReadImmediateTimeout)
err = a.conn.SetReadDeadline(deadline)
if err != nil {
a.logger.Warn("setReadDeadlineFailed", zap.Any("agentId", a.AgentID), zap.Any("deadline", deadline), zap.Error(err))
return 0, true, err
}
}
buffer = nil
return totallyRead, false, nil
Expand Down

0 comments on commit 08b9792

Please sign in to comment.