-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add metadata * Change argument order so metadata comes before throwable * Micro-optimizations around atomics/locking
- Loading branch information
1 parent
0e576bf
commit 9599f56
Showing
17 changed files
with
303 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.juul.tuulbox.logging | ||
|
||
/** | ||
* Marker interface to strongly type metadata with its key. Proper usage is | ||
* to create `object`s or `enum class`es when implementing this type. | ||
*/ | ||
public interface Key<T : Any> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.juul.tuulbox.logging | ||
|
||
internal class Metadata : ReadMetadata, WriteMetadata { | ||
private val storedData = mutableMapOf<Key<*>, Any>() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
public override operator fun <T : Any> get(key: Key<T>): T? = | ||
storedData[key] as? T | ||
|
||
public override operator fun <T : Any> set(key: Key<T>, value: T) { | ||
storedData[key] = value | ||
} | ||
|
||
public override fun copy(): Metadata = Metadata().also { copy -> | ||
copy.storedData += this.storedData | ||
} | ||
|
||
internal fun clear() { | ||
storedData.clear() | ||
} | ||
|
||
override fun equals(other: Any?): Boolean = other is Metadata && storedData == other.storedData | ||
override fun hashCode(): Int = storedData.hashCode() | ||
} | ||
|
||
/** | ||
* Additional data associated with a log. It's important that [Logger] instances do NOT hold onto references | ||
* to [ReadMetadata] arguments after the function returns. If a [ReadMetadata] reference must be kept after | ||
* function return, create a [copy]. | ||
*/ | ||
public interface ReadMetadata { | ||
public operator fun <T : Any> get(key: Key<T>): T? | ||
public fun copy(): ReadMetadata | ||
} | ||
|
||
/** | ||
* Additional data associated with a log. It's important that [Log] calls do NOT hold onto references | ||
* to [WriteMetadata] arguments after the lambda returns. | ||
*/ | ||
public interface WriteMetadata { | ||
public operator fun <T : Any> set(key: Key<T>, value: T) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.juul.tuulbox.logging | ||
|
||
import kotlinx.atomicfu.locks.reentrantLock | ||
import kotlinx.atomicfu.locks.withLock | ||
|
||
/** While a [Pool] is logically thread safe, Kotlin/Native's memory model requires @ThreadLocal on instances of this. */ | ||
internal class Pool<T>( | ||
private val factory: () -> T, | ||
private val refurbish: (T) -> Unit, | ||
) { | ||
private val lock = reentrantLock() | ||
private val cache = ArrayDeque<T>() | ||
|
||
fun borrow(): T = lock.withLock { cache.removeLastOrNull() } ?: factory() | ||
fun recycle(value: T) { | ||
refurbish(value) | ||
lock.withLock { cache.addLast(value) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.