Skip to content

Commit b66bd56

Browse files
Merge pull request #5 from davenverse/allowSynchronousPersistence
Allow Synchronous Persistence
2 parents 3e93b61 + d9a852c commit b66bd56

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

core/shared/src/main/scala/io/chrisdavenport/snickerdoodle/SnCookieJar.scala

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ private[snickerdoodle] object SnCookieJar {
1212
private[snickerdoodle] class Http4sPersistenceCookieJarImpl[F[_]: Concurrent: Clock](
1313
persistence: SnCookiePersistence[F],
1414
supervisor: Supervisor[F],
15+
synchronousPersistence: Boolean,
1516
ref: Ref[F, Map[SnCookie.SnCookieKey, SnCookie]],
1617
isPublicSuffix: String => Boolean
1718
) extends CookieJar[F]{
19+
20+
def persist[A](fa: F[A]): F[Unit] = {
21+
if (synchronousPersistence) fa.void
22+
else supervisor.supervise(fa).void
23+
}
24+
1825
def evictExpired: F[Unit] = for {
1926
now <- HttpDate.current[F].map(_.epochSecond)
2027
_ <- ref.update(_.filter(t => now <= t._2.expiry))
21-
_ <- supervisor.supervise(persistence.clearExpired(now))
28+
_ <- persist(persistence.clearExpired(now))
2229
} yield ()
2330

2431
def evictAll: F[Unit] =
25-
ref.set(Map.empty) >>
26-
persistence.clear
32+
ref.set(Map.empty) >>
33+
persist(persistence.clear)
2734

2835
def addCookies[G[_]: Foldable](cookies: G[(ResponseCookie, Uri)]): F[Unit] =
2936
for {
@@ -36,7 +43,7 @@ private[snickerdoodle] object SnCookieJar {
3643
snO.fold(m)(v => m + v)
3744
}
3845
_ <- ref.update(m => m ++ map)
39-
_ <- supervisor.supervise(map.toList.traverse_{ case (_, c) => if (c.persist) persistence.create(c) else Applicative[F].unit}).void
46+
_ <- persist(map.toList.traverse_{ case (_, c) => if (c.persist) persistence.create(c) else Applicative[F].unit}).void
4047
} yield ()
4148

4249
def enrichRequest[G[_]](r: Request[G]): F[Request[G]] = for {
@@ -51,7 +58,7 @@ private[snickerdoodle] object SnCookieJar {
5158
}
5259
(m ++ updatedMap, filtered)
5360
}
54-
_ <- supervisor.supervise(cookies.traverse_(c => if (c.persist) persistence.updateLastAccessed(SnCookie.SnCookieKey.fromCookie(c), c.lastAccessed) else Applicative[F].unit))
61+
_ <- persist(cookies.traverse_(c => if (c.persist) persistence.updateLastAccessed(SnCookie.SnCookieKey.fromCookie(c), c.lastAccessed) else Applicative[F].unit))
5562
} yield cookies.foldLeft(r){ case (r, c) => r.addCookie(SnCookie.toRequestCookie(c))}
5663

5764
}

core/shared/src/main/scala/io/chrisdavenport/snickerdoodle/SnCookieJarBuilder.scala

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ final class SnCookieJarBuilder[F[_]: Async] private (
1010
private val supervisorO: Option[Supervisor[F]],
1111
private val persistenceO: Option[Resource[F, SnCookiePersistence[F]]],
1212
private val isPublicSuffixO: Option[Resource[F, String => Boolean]],
13-
){ self =>
13+
private val synchronousPersistence: Boolean,
14+
){ self =>
1415
def copy(
1516
supervisorO: Option[Supervisor[F]] = self.supervisorO,
1617
persistenceO: Option[Resource[F, SnCookiePersistence[F]]] = self.persistenceO,
17-
isPublicSuffixO: Option[Resource[F, String => Boolean]] = self.isPublicSuffixO
18-
): SnCookieJarBuilder[F] = new SnCookieJarBuilder[F](supervisorO, persistenceO, isPublicSuffixO)
18+
isPublicSuffixO: Option[Resource[F, String => Boolean]] = self.isPublicSuffixO,
19+
synchronousPersistence: Boolean = self.synchronousPersistence
20+
): SnCookieJarBuilder[F] = new SnCookieJarBuilder[F](supervisorO, persistenceO, isPublicSuffixO, synchronousPersistence)
1921

2022
def withSupervisor(s: Supervisor[F]) = copy(supervisorO = s.some)
2123

@@ -24,6 +26,9 @@ final class SnCookieJarBuilder[F[_]: Async] private (
2426
def withPersistence(c: SnCookiePersistence[F]) =
2527
copy(persistenceO = c.pure[Resource[F, *]].some)
2628

29+
def withSynchronousPersistence = copy(synchronousPersistence = true)
30+
def withAsynchronousPersistence = copy(synchronousPersistence = false)
31+
2732
def withoutPersistence = copy(persistenceO = None)
2833

2934
def withIsPublicSuffix(f: String => Boolean) = copy(isPublicSuffixO = f.pure[Resource[F, *]].some)
@@ -44,7 +49,7 @@ final class SnCookieJarBuilder[F[_]: Async] private (
4449
)
4550
)
4651
isPublicSuffix <- isPublicSuffixO.getOrElse({(_: String) => false}.pure[Resource[F, *]])
47-
out = tO.fold[CookieJar[F]](new SnCookieJar.Http4sMemoryCookieJarImpl[F](state, isPublicSuffix)){ case (cp, s) => new SnCookieJar.Http4sPersistenceCookieJarImpl[F](cp, s, state, isPublicSuffix)}
52+
out = tO.fold[CookieJar[F]](new SnCookieJar.Http4sMemoryCookieJarImpl[F](state, isPublicSuffix)){ case (cp, s) => new SnCookieJar.Http4sPersistenceCookieJarImpl[F](cp, s, synchronousPersistence, state, isPublicSuffix)}
4853
_ <- Resource.eval(out.evictExpired)
4954
} yield out
5055

@@ -66,16 +71,17 @@ final class SnCookieJarBuilder[F[_]: Async] private (
6671
)
6772
)
6873
isPublicSuffix <- isPublicSuffixO.getOrElse({(_: String) => false}.pure[Resource[F, *]])
69-
cj = tO.fold[CookieJar[F]](new SnCookieJar.Http4sMemoryCookieJarImpl[F](state, isPublicSuffix)){ case (cp, s) => new SnCookieJar.Http4sPersistenceCookieJarImpl[F](cp, s, state, isPublicSuffix)}
74+
cj = tO.fold[CookieJar[F]](new SnCookieJar.Http4sMemoryCookieJarImpl[F](state, isPublicSuffix)){ case (cp, s) => new SnCookieJar.Http4sPersistenceCookieJarImpl[F](cp, s, synchronousPersistence, state, isPublicSuffix)}
7075
_ <- Resource.eval(cj.evictExpired)
7176
} yield (cj, state)
7277
}
7378
}
7479

7580
object SnCookieJarBuilder {
76-
def default[F[_]: Async]: SnCookieJarBuilder[F] = new SnCookieJarBuilder[F](None, None, Defaults.isPublicSuffix)
81+
def default[F[_]: Async]: SnCookieJarBuilder[F] = new SnCookieJarBuilder[F](None, None, Defaults.isPublicSuffix, Defaults.synchronousPersistence)
7782

7883
private object Defaults extends SnCookieJarBuilderDefaultsPlaftom {
84+
val synchronousPersistence = true
7985

8086
}
8187
}

0 commit comments

Comments
 (0)