Skip to content

Commit ef803c8

Browse files
authored
Switch Kryo serialization library (1.x) (#76)
* Switch Kryo serialization library * Remove crossversion * Use latest version of scala kryo
1 parent 27808e4 commit ef803c8

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ val zioCatsInteropVersion = "13.0.0.1"
99
val sttpVersion = "3.7.0"
1010
val calibanVersion = "1.4.3"
1111
val redis4catsVersion = "1.2.0"
12-
val chillVersion = "0.9.5"
12+
val scalaKryoVersion = "1.0.0"
1313
val testContainersVersion = "0.40.9"
1414

1515
inThisBuild(
@@ -132,7 +132,7 @@ lazy val serializationKryo = project
132132
.settings(
133133
libraryDependencies ++=
134134
Seq(
135-
("com.twitter" %% "chill" % chillVersion).cross(CrossVersion.for3Use2_13)
135+
"io.altoo" %% "scala-kryo-serialization" % scalaKryoVersion
136136
)
137137
)
138138

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package com.devsisters.shardcake
22

33
import com.devsisters.shardcake.interfaces.Serialization
4-
import com.twitter.chill.{ KryoInstantiator, KryoPool, ScalaKryoInstantiator }
4+
import com.typesafe.config.{ Config, ConfigFactory }
5+
import io.altoo.serialization.kryo.scala.ScalaKryoSerializer
56
import zio.{ Has, Task, ZIO, ZLayer }
67

78
object KryoSerialization {
89

910
/**
10-
* A layer that returns a serialization implementation using the Kryo library
11+
* A layer that returns a serialization implementation using the Kryo library.
1112
*/
1213
val live: ZLayer[Any, Throwable, Has[Serialization]] =
14+
ZIO.effect(ConfigFactory.defaultReference()).flatMap(make).toLayer
15+
16+
/**
17+
* A layer that returns a serialization implementation using the Kryo library, taking a Config object.
18+
* See https://github.com/altoo-ag/scala-kryo-serialization for more details about configuration.
19+
*/
20+
def liveWithConfig(config: Config): ZLayer[Any, Throwable, Has[Serialization]] =
21+
make(config).toLayer
22+
23+
private def make(config: Config): Task[Serialization] =
1324
ZIO.effect {
14-
def kryoInstantiator: KryoInstantiator = new ScalaKryoInstantiator
15-
def poolSize: Int = 4 * java.lang.Runtime.getRuntime.availableProcessors
16-
KryoPool.withByteArrayOutputStream(poolSize, kryoInstantiator)
17-
}.map(kryoPool =>
25+
new ScalaKryoSerializer(config, getClass.getClassLoader)
26+
}.map(serializer =>
1827
new Serialization {
19-
def encode(message: Any): Task[Array[Byte]] = ZIO.effect(kryoPool.toBytesWithClass(message))
20-
def decode[A](bytes: Array[Byte]): Task[A] = ZIO.effect(kryoPool.fromBytes(bytes).asInstanceOf[A])
28+
def encode(message: Any): Task[Array[Byte]] = ZIO.fromTry(serializer.serialize(message))
29+
def decode[A](bytes: Array[Byte]): Task[A] = ZIO.fromTry(serializer.deserialize[A](bytes))
2130
}
22-
).toLayer
31+
)
2332
}

0 commit comments

Comments
 (0)