Skip to content

Commit

Permalink
Change default buffer option to single (close #649)
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Dec 8, 2023
1 parent a4ff351 commit bf8eeae
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ private boolean setupWithLocalConfig() {
NetworkConfiguration networkConfiguration = new NetworkConfiguration(uri, method);
EmitterConfiguration emitterConfiguration = new EmitterConfiguration()
.requestCallback(getRequestCallback())
.bufferOption(BufferOption.DefaultGroup)
.threadPoolSize(20)
.emitRange(500)
.byteLimitPost(52000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Demo : Activity(), LoggerDelegate {
val networkConfiguration = NetworkConfiguration(uri, method)
val emitterConfiguration = EmitterConfiguration()
.requestCallback(requestCallback)
.bufferOption(BufferOption.DefaultGroup)
.bufferOption(BufferOption.SmallGroup)
.threadPoolSize(20)
.emitRange(500)
.byteLimitPost(52000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ class EmitterTest {
var builder = { emitter: Emitter -> emitter.bufferOption = BufferOption.Single }
var emitter = Emitter(context, "com.acme", builder)
Assert.assertEquals(BufferOption.Single, emitter.bufferOption)
builder = { emitter1: Emitter -> emitter1.bufferOption = BufferOption.DefaultGroup }
builder = { emitter1: Emitter -> emitter1.bufferOption = BufferOption.SmallGroup }
emitter = Emitter(context, "com.acme", builder)
Assert.assertEquals(BufferOption.DefaultGroup, emitter.bufferOption)
builder = { emitter2: Emitter -> emitter2.bufferOption = BufferOption.HeavyGroup }
Assert.assertEquals(BufferOption.SmallGroup, emitter.bufferOption)
builder = { emitter2: Emitter -> emitter2.bufferOption = BufferOption.LargeGroup }
emitter = Emitter(context, "com.acme", builder)
Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption)
Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption)
}

@Test
Expand All @@ -76,7 +76,7 @@ class EmitterTest {
var emitter = Emitter(context, uri, builder)
Assert.assertEquals("http://$uri/i", emitter.emitterUri)
builder = { emitter1: Emitter ->
emitter1.bufferOption = BufferOption.DefaultGroup
emitter1.bufferOption = BufferOption.SmallGroup
emitter1.httpMethod = HttpMethod.POST
emitter1.requestSecurity = Protocol.HTTP
}
Expand All @@ -86,14 +86,14 @@ class EmitterTest {
emitter.emitterUri
)
builder = { emitter2: Emitter ->
emitter2.bufferOption = BufferOption.DefaultGroup
emitter2.bufferOption = BufferOption.SmallGroup
emitter2.httpMethod = HttpMethod.GET
emitter2.requestSecurity = Protocol.HTTPS
}
emitter = Emitter(context, uri, builder)
Assert.assertEquals("https://$uri/i", emitter.emitterUri)
builder = { emitter3: Emitter ->
emitter3.bufferOption = BufferOption.DefaultGroup
emitter3.bufferOption = BufferOption.SmallGroup
emitter3.httpMethod = HttpMethod.POST
emitter3.requestSecurity = Protocol.HTTPS
}
Expand Down Expand Up @@ -173,8 +173,8 @@ class EmitterTest {
Assert.assertEquals("https://$uri/i", emitter.emitterUri)
emitter.emitterUri = "com.acme"
Assert.assertEquals("https://com.acme/i", emitter.emitterUri)
emitter.bufferOption = BufferOption.HeavyGroup
Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption)
emitter.bufferOption = BufferOption.LargeGroup
Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption)
emitter.flush()
emitter.flush()
Thread.sleep(500)
Expand All @@ -189,8 +189,8 @@ class EmitterTest {
"https://com.foo/com.snowplowanalytics.snowplow/tp2",
emitter.emitterUri
)
emitter.bufferOption = BufferOption.DefaultGroup
Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption)
emitter.bufferOption = BufferOption.SmallGroup
Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption)
emitter.shutdown()
builder = { emitter1: Emitter ->
emitter1.bufferOption = BufferOption.Single
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit

object EmitterDefaults {
var httpMethod = HttpMethod.POST
var bufferOption = BufferOption.DefaultGroup
var bufferOption = BufferOption.Single
var httpProtocol = Protocol.HTTPS
var tlsVersions: EnumSet<TLSVersion> = EnumSet.of(TLSVersion.TLSv1_2)
var emitRange: Int = 150
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.json.JSONObject
* Configure how the tracker should send the events to the collector.
*
* Default values:
* - bufferOption: [BufferOption.DefaultGroup]
* - bufferOption: [BufferOption.Single]
* - serverAnonymisation: false
* - emitRange: 150 - maximum number of events to process at a time
* - threadPoolSize: 15
Expand All @@ -45,7 +45,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface

private var _bufferOption: BufferOption? = null
override var bufferOption: BufferOption
get() = _bufferOption ?: sourceConfig?.bufferOption ?: BufferOption.DefaultGroup
get() = _bufferOption ?: sourceConfig?.bufferOption ?: EmitterDefaults.bufferOption
set(value) { _bufferOption = value }

private var _emitRange: Int? = null
Expand Down Expand Up @@ -96,8 +96,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface
// Builders

/**
* How many events to send in each request. By default, this is set to [BufferOption.DefaultGroup],
* a maximum of 10 events per request.
* How many events to send in each request. By default, this is set to [BufferOption.Single].
*/
fun bufferOption(bufferOption: BufferOption): EmitterConfiguration {
this.bufferOption = bufferOption
Expand Down Expand Up @@ -205,7 +204,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface
*/
constructor(jsonObject: JSONObject) : this() {
if (jsonObject.has("bufferOption")) {
_bufferOption = BufferOption.valueOf(jsonObject.getString("bufferOption"))
_bufferOption = BufferOption.fromString(jsonObject.getString("bufferOption"))
}
if (jsonObject.has("emitRange")) { _emitRange = jsonObject.getInt("emitRange") }
if (jsonObject.has("threadPoolSize")) { _threadPoolSize = jsonObject.getInt("threadPoolSize") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,35 @@ package com.snowplowanalytics.snowplow.emitter
*/
enum class BufferOption(val code: Int) {
/**
* Sends both GET and POST requests with only a single event. Can cause a spike in
* network traffic if used in correlation with a large amount of events.
* Sends both GET and POST requests with only a single event.
* This is the default setting.
* Can cause a spike in network traffic if used in correlation with a large amount of events.
*/
Single(1),

/**
* Sends POST requests in groups of 10 events. This is the default amount of events to
* package into a POST. All GET requests will still emit one at a time.
* Sends POST requests in groups of 10 events.
* All GET requests will still emit one at a time.
*/
DefaultGroup(10),
SmallGroup(10),

/**
* Sends POST requests in groups of 25 events. Useful for situations where many events
* need to be sent. All GET requests will still emit one at a time.
* Sends POST requests in groups of 25 events.
* Useful for situations where many events need to be sent.
* All GET requests will still emit one at a time.
*/
HeavyGroup(25);
LargeGroup(25);

companion object {
fun fromString(string: String): BufferOption? {
return when (string) {
"Single" -> Single
"SmallGroup" -> SmallGroup
"DefaultGroup" -> SmallGroup
"LargeGroup" -> LargeGroup
"HeavyGroup" -> LargeGroup
else -> null
}
}
}
}

0 comments on commit bf8eeae

Please sign in to comment.