-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
Date
header to responses by default (#2773)
* Add the `Date` header to responses by default * Don't add header for 1xx or 5xx responses * Update GH workflow * fmt
- Loading branch information
1 parent
146e441
commit 21c8ae5
Showing
5 changed files
with
175 additions
and
6 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
27 changes: 27 additions & 0 deletions
27
zio-http-benchmarks/src/main/scala/zhttp.benchmarks/CachedDateHeaderBenchmark.scala
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,27 @@ | ||
package zio.http.netty | ||
|
||
import java.time.ZonedDateTime | ||
import java.util.concurrent.TimeUnit | ||
|
||
import zio.http.internal.DateEncoding | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode(Array(Mode.AverageTime)) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Threads(16) | ||
@Fork(1) | ||
@Warmup(iterations = 3, time = 1) | ||
@Measurement(iterations = 5, time = 1) | ||
class CachedDateHeaderBenchmark { | ||
private val dateHeaderCache = new CachedDateHeader() | ||
|
||
@Benchmark | ||
def benchmarkCached() = | ||
dateHeaderCache.get() | ||
|
||
@Benchmark | ||
def benchmarkFresh() = | ||
DateEncoding.default.encodeDate(ZonedDateTime.now()) | ||
} |
51 changes: 51 additions & 0 deletions
51
zio-http/jvm/src/main/scala/zio/http/netty/CachedDateHeader.scala
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,51 @@ | ||
package zio.http.netty | ||
|
||
import java.time.{Clock, LocalDateTime, ZoneOffset} | ||
import java.util.concurrent.locks.LockSupport | ||
|
||
import zio.http.internal.DateEncoding | ||
|
||
private object CachedDateHeader { | ||
lazy val default: CachedDateHeader = new CachedDateHeader() | ||
} | ||
|
||
final private class CachedDateHeader( | ||
clock: Clock = Clock.tickSeconds(ZoneOffset.UTC), | ||
dateEncoding: DateEncoding = DateEncoding.default, | ||
) { | ||
private var _headerValue = renderDateHeaderValue(clock.millis()) | ||
|
||
{ | ||
val t = new Ticker | ||
t.setDaemon(true) | ||
t.setName(s"zio.http.netty.DateHeaderEncoder.Scheduler") | ||
t.setPriority(Thread.MAX_PRIORITY) | ||
t.start() | ||
} | ||
|
||
def get(): String = _headerValue | ||
|
||
private final class Ticker extends Thread { | ||
override def run(): Unit = { | ||
val clock0 = clock | ||
var currentMillis = clock0.millis() | ||
while (!isInterrupted) { | ||
LockSupport.parkUntil(currentMillis + 1000) | ||
currentMillis = clock0.millis() | ||
updateHeaderValue(currentMillis) | ||
} | ||
} | ||
} | ||
|
||
private def renderDateHeaderValue(epochMilli: Long): String = { | ||
val dt = LocalDateTime | ||
.ofEpochSecond(epochMilli / 1000L, 0, ZoneOffset.UTC) | ||
.atZone(ZoneOffset.UTC) | ||
|
||
dateEncoding.encodeDate(dt) | ||
} | ||
|
||
private def updateHeaderValue(epochMilli: Long): Unit = | ||
_headerValue = renderDateHeaderValue(epochMilli) | ||
|
||
} |
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
36 changes: 36 additions & 0 deletions
36
zio-http/jvm/src/test/scala/zio/http/netty/CachedDateHeaderSpec.scala
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,36 @@ | ||
package zio.http.netty | ||
|
||
import java.time.ZonedDateTime | ||
|
||
import zio._ | ||
import zio.test.{Spec, TestAspect, TestEnvironment, assertCompletes} | ||
|
||
import zio.http.ZIOHttpSpec | ||
import zio.http.internal.DateEncoding | ||
|
||
object CachedDateHeaderSpec extends ZIOHttpSpec { | ||
private val dateHeaderCache = CachedDateHeader.default | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("CachedDateHeader")( | ||
test("yields the same date header value as DateEncoding") { | ||
val f = ZIO.suspendSucceed { | ||
val uncached = DateEncoding.default.encodeDate(ZonedDateTime.now()) | ||
val cached = dateHeaderCache.get() | ||
|
||
ZIO | ||
.fail( | ||
new Exception( | ||
s"Mismatch in cached and uncached date header value:\n\tuncached: $uncached\n\tcached: $cached", | ||
), | ||
) | ||
.unless(uncached == cached) | ||
} | ||
.delay(5.milli) | ||
.retryN(1) | ||
|
||
ZIO.foreachDiscard((1 to 300).toList)(_ => f) *> assertCompletes | ||
} | ||
@@ TestAspect.withLiveClock, | ||
) | ||
} |