Skip to content

Commit

Permalink
fix 100% cpu (#13)
Browse files Browse the repository at this point in the history
* allow override interval

Signed-off-by: dimagolomozy <[email protected]>

* convert to time duration

Signed-off-by: dimagolomozy <[email protected]>

Signed-off-by: dimagolomozy <[email protected]>
  • Loading branch information
DimaGolomozy authored Jan 23, 2023
1 parent 5ce02e2 commit 8db9885
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 4 additions & 2 deletions constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coralogix

import "time"

const (
// MaxLogBufferSize is maximum log buffer size (default=128MiB)
MaxLogBufferSize uint64 = 128 * (1024 * 1024)
Expand All @@ -8,10 +10,10 @@ const (
MaxLogChunkSize uint64 = 1.5 * (1024 * 1024)

// NormalSendSpeedInterval is a bulk send interval in normal mode
NormalSendSpeedInterval float64 = 0.5
NormalSendSpeedInterval = 1 * time.Second

// FastSendSpeedInterval is a bulk send interval in fast mode
FastSendSpeedInterval float64 = 0.1
FastSendSpeedInterval = 500 * time.Millisecond

// TimeDelayTimeout is a timeout for time-delay request
TimeDelayTimeout uint = 5
Expand Down
16 changes: 11 additions & 5 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type LoggerManager struct {
TimeDelta float64 // Time difference between local machine and Coralogix servers
TimeDeltaLastUpdate int // Last time-delta update time
Stopped bool // Is current logger manager stopped
SendInterval time.Duration // Send bulk logs interval
LogsBuffer LogBuffer // Logs buffer
Credentials // Credentials for Coralogix account
Lock sync.WaitGroup // CoralogixLogger manager locker
Expand All @@ -25,6 +26,7 @@ func NewLoggerManager(PrivateKey string, ApplicationName string, SubsystemName s
0,
0,
false,
0,
LogBuffer{},
Credentials{
PrivateKey,
Expand Down Expand Up @@ -126,7 +128,7 @@ func (manager *LoggerManager) SendBulk(SyncTime bool) bool {

// Run should work in separate thread and asynchronously operate with logs
func (manager *LoggerManager) Run() {
var NextSendInterval float64
var NextSendInterval time.Duration

defer manager.Lock.Done()

Expand All @@ -138,14 +140,18 @@ func (manager *LoggerManager) Run() {

manager.SendBulk(manager.SyncTime)

if manager.LogsBuffer.Size() > (MaxLogChunkSize / 2) {
NextSendInterval = FastSendSpeedInterval
if manager.SendInterval > 0 {
NextSendInterval = manager.SendInterval
} else {
NextSendInterval = NormalSendSpeedInterval
if manager.LogsBuffer.Size() > (MaxLogChunkSize / 2) {
NextSendInterval = FastSendSpeedInterval
} else {
NextSendInterval = NormalSendSpeedInterval
}
}

DebugLogger.Printf("Next buffer check is scheduled in %.1f seconds\n", NextSendInterval)
time.Sleep(time.Duration(NextSendInterval) * time.Second)
time.Sleep(NextSendInterval)
}
}

Expand Down

0 comments on commit 8db9885

Please sign in to comment.