Skip to content

Instantly share code, notes, and snippets.

@eungju
eungju / FixZipkinV2Reporter.kt
Last active June 13, 2021 04:46
jaeger-zipkin이 객체의 private 필드에 접근하지 않게 한다.
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.jaegertracing.zipkin.internal.V2SpanConverter
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import java.lang.invoke.MethodHandles
import java.lang.invoke.VarHandle
class FixZipkinV2Reporter(gson: Gson = PRIVACY_RESPECTING_GSON) {
init {
@eungju
eungju / download-chapters.hs
Last active April 9, 2019 08:31
Download The Art of Science and Engineering
import Control.Applicative
import Control.Monad
import System.Directory
import System.FilePath
import System.Exit
import System.Process
import Text.Printf
type URL = String
@eungju
eungju / TRawHttpSocket.java
Last active May 11, 2018 00:58
RawHTTP libthrift TTransport
import com.athaydes.rawhttp.core.EagerBodyReader;
import com.athaydes.rawhttp.core.EagerHttpResponse;
import com.athaydes.rawhttp.core.MethodLine;
import com.athaydes.rawhttp.core.RawHttp;
import com.athaydes.rawhttp.core.RawHttpHeaders;
import com.athaydes.rawhttp.core.RawHttpRequest;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@eungju
eungju / CircuitBreakerMetrics.kt
Last active April 12, 2018 13:59
Timeout CircuitBreaker
interface CircuitBreakerMetrics {
fun executionCompleted(name: String, latency: Double) = Unit
}
@eungju
eungju / pool.py
Last active March 23, 2018 11:35
class KyotoTycoon(object):
...
def get(self, key):
with self.pool.context() as c:
return c.get(key, db=self.db)
class ConnectionPool(object):
...
def context(self):
@eungju
eungju / RxTest.kt
Last active November 26, 2017 14:13
Catch UndeliverableException
@Test
fun simple() {
val observable = Observable.create<Int> { emitter ->
emitter.onNext(1)
emitter.onError(IOException("Timeout"))
}
observable
.take(1)
.subscribe({ assertEquals(1, it) }, { assertTrue(it is UndeliverableException) })
}
@eungju
eungju / Correct.kt
Last active August 29, 2017 02:00
Rx에서 reactive base type 만들 때 자주 하는 실수
override fun connectDevice(userDeviceRef: UserDeviceRef, deviceAttributes: DeviceAttributes, nodeRef: PilsnerRef): Completable
= Completable.fromRunnable {
rwLock.write {
devicePresences[userDeviceRef.deviceId] = Presence(userDeviceRef, deviceAttributes, nodeRef)
userDevices.put(userDeviceRef.userId, userDeviceRef.deviceId)
}
}
override fun getPresencesOfUser(userId: UserId): Single<List<Presence>>
= Single.fromCallable {
@eungju
eungju / Headers.kt
Last active July 21, 2017 04:55
HTTP 헤더 이름은 왜 타입을 안만드나?
class Http {
data class HeaderName(val name: String) : Comparable<HeaderName> {
override fun compareTo(other: HeaderName): Int = name.compareTo(other.name, true)
fun get(headers: MultiMap): String? = headers.get(name)
fun set(headers: MultiMap, value: String): Unit { headers.set(name, value) }
fun copyIfExist(from: MultiMap, to: MultiMap) {
get(from)?.let { set(to, it) }
}
@eungju
eungju / RxJavaTest.java
Created June 15, 2017 01:26
RxJava 2.1.0+withLatestFrom+cache+firstOrError causes NoSuchElementException
@Test
fun withLatestFromCausesNoSuchElementException() {
val state = Observable.just(42).cacheWithInitialCapacity(1)
var error = 0
val threads = (0..4 - 1)
.map {
Thread {
Observable.just(Unit)
.withLatestFrom(state, BiFunction { _: Unit, state: Int -> state })
.firstOrError()
@eungju
eungju / threaddump.md
Last active March 6, 2017 10:28
파이썬에서 데드락 해결하기 위해 쓰레드 덤프 얻는 방법.

데드락이 걸렸는데 코드를 봐도 어디인지 모를 때가 있다. 자바는 JVM이 쓰레드 덤프 기능을 제공해서 어떤 쓰레드가 어디서 멈춰 있는지 확인하기 쉬웠는데 파이썬은 쓰레드 덤프를 얻으려면 약간의 코딩이 필요하다.

아래와 같이 모든 쓰레드의 스택 트레이스를 볼 수 있다.

for ident, stack in sys._current_frames().items():
    logger.info(("%d" % ident) + "".join(traceback.format_list(traceback.extract_stack(stack))))

어떤 쓰레드가 멈춰 있는지 안다면 그 쓰레드의 스택 트레이스만 볼 수 있다.