Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush events only when the buffer is full (close #827) #833

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Sources/Core/Emitter/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,17 @@ class Emitter: NSObject, EmitterEventProcessing {
}

/// Insert a Payload object into the buffer to be sent to collector.
/// This method will add the payload to the database and flush (send all events).
/// This method will add the payload to the database and flush (send all events) when the buffer is full.
/// - Parameter eventPayload: A Payload containing a completed event to be added into the buffer.
func addPayload(toBuffer eventPayload: Payload) {
DispatchQueue.global(qos: .default).async { [weak self] in
self?.eventStore?.addEvent(eventPayload)
self?.flush()
guard let self = self else { return }
self.eventStore?.addEvent(eventPayload)
if let eventsCount = self.eventStore?.count() {
if eventsCount >= self.bufferOption.rawValue {
self.flush()
}
}
}
}

Expand Down Expand Up @@ -379,6 +384,9 @@ class Emitter: NSObject, EmitterEventProcessing {
}

let events = eventStore.emittableEvents(withQueryLimit: UInt(emitRange))
if !retryFailedRequests {
let _ = eventStore.removeEvents(withIds: events.map { $0.storeId })
}
let requests = buildRequests(fromEvents: events)
let sendResults = networkConnection?.sendRequests(requests)

Expand Down Expand Up @@ -426,7 +434,11 @@ class Emitter: NSObject, EmitterEventProcessing {
Thread.sleep(forTimeInterval: 5)
return
} else {
self.attemptEmit()
if let eventsCount = self.eventStore?.count() {
if eventsCount >= self.bufferOption.rawValue {
self.attemptEmit()
}
}
}
}

Expand Down