diff --git a/zio-http/src/main/scala/zio/http/netty/server/NettyDriver.scala b/zio-http/src/main/scala/zio/http/netty/server/NettyDriver.scala index 3d5e9ef59d..f412c73331 100644 --- a/zio-http/src/main/scala/zio/http/netty/server/NettyDriver.scala +++ b/zio-http/src/main/scala/zio/http/netty/server/NettyDriver.scala @@ -116,7 +116,6 @@ private[zio] object NettyDriver { ZLayer.succeed( new AtomicReference[(HttpApp[Any], ZEnvironment[Any])]((HttpApp.empty, ZEnvironment.empty)), ), - ZLayer.succeed(ServerTime.make(1000.millis)), NettyRuntime.live, ServerChannelInitializer.layer, ServerInboundHandler.live, diff --git a/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala b/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala index cf2fd4d3ca..5df9e87a94 100644 --- a/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala +++ b/zio-http/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala @@ -43,7 +43,6 @@ private[zio] final case class ServerInboundHandler( appRef: AppRef, config: Server.Config, runtime: NettyRuntime, - time: ServerTime, )(implicit trace: Trace) extends SimpleChannelInboundHandler[HttpObject](false) { self => @@ -93,7 +92,7 @@ private[zio] final case class ServerInboundHandler( Exit.succeed(Response.fromThrowable(throwable)) } else app(req) - if (!attemptImmediateWrite(ctx, exit, time)) { + if (!attemptImmediateWrite(ctx, exit)) { writeResponse(ctx, env, exit, jReq)(releaseRequest) } else { releaseRequest() @@ -139,7 +138,6 @@ private[zio] final case class ServerInboundHandler( private def attemptFastWrite( ctx: ChannelHandlerContext, response: Response, - time: ServerTime, ): Boolean = { def fastEncode(response: Response, bytes: Array[Byte]) = { @@ -165,7 +163,6 @@ private[zio] final case class ServerInboundHandler( ctx: ChannelHandlerContext, response: Response, jRequest: HttpRequest, - time: ServerTime, ): Option[Task[Unit]] = { response.body match { case WebsocketBody(socketApp) if response.status == Status.SwitchingProtocols => @@ -185,11 +182,10 @@ private[zio] final case class ServerInboundHandler( private def attemptImmediateWrite( ctx: ChannelHandlerContext, exit: ZIO[Any, Response, Response], - time: ServerTime, ): Boolean = { exit match { case Exit.Success(response) if response ne null => - attemptFastWrite(ctx, response, time) + attemptFastWrite(ctx, response) case _ => false } } @@ -292,7 +288,7 @@ private[zio] final case class ServerInboundHandler( private def writeNotFound(ctx: ChannelHandlerContext, jReq: HttpRequest): Unit = { val response = Response.notFound(jReq.uri()) - attemptFastWrite(ctx, response, time) + attemptFastWrite(ctx, response) } private def writeResponse( @@ -316,9 +312,9 @@ private[zio] final case class ServerInboundHandler( }.flatMap { response => ZIO.attempt { if (response ne null) { - val done = attemptFastWrite(ctx, response, time) + val done = attemptFastWrite(ctx, response) if (!done) - attemptFullWrite(ctx, response, jReq, time) + attemptFullWrite(ctx, response, jReq) else None } else { @@ -329,7 +325,7 @@ private[zio] final case class ServerInboundHandler( } }.flatMap(_.getOrElse(ZIO.unit)).catchSomeCause { case cause => ZIO.attempt( - attemptFastWrite(ctx, withDefaultErrorResponse(cause.squash), time), + attemptFastWrite(ctx, withDefaultErrorResponse(cause.squash)), ) } } @@ -350,7 +346,7 @@ private[zio] final case class ServerInboundHandler( object ServerInboundHandler { val live: ZLayer[ - ServerTime with Server.Config with NettyRuntime with AppRef, + Server.Config with NettyRuntime with AppRef, Nothing, ServerInboundHandler, ] = { @@ -360,9 +356,8 @@ object ServerInboundHandler { appRef <- ZIO.service[AppRef] rtm <- ZIO.service[NettyRuntime] config <- ZIO.service[Server.Config] - time <- ZIO.service[ServerTime] - } yield ServerInboundHandler(appRef, config, rtm, time) + } yield ServerInboundHandler(appRef, config, rtm) } } diff --git a/zio-http/src/main/scala/zio/http/netty/server/ServerTime.scala b/zio-http/src/main/scala/zio/http/netty/server/ServerTime.scala deleted file mode 100644 index 85f2da5493..0000000000 --- a/zio-http/src/main/scala/zio/http/netty/server/ServerTime.scala +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2021 - 2023 Sporta Technologies PVT LTD & the ZIO HTTP contributors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package zio.http.netty.server - -import io.netty.util.AsciiString - -import java.text.SimpleDateFormat -import java.util.Date // scalafix:ok; -import zio.stacktracer.TracingImplicits.disableAutoTrace -private[zio] final class ServerTime(minDuration: Long) { - - private var last: Long = System.currentTimeMillis() - private var lastString: CharSequence = ServerTime.format(new Date(last)) - - def refresh(): Boolean = { - val now = System.currentTimeMillis() - val diff = now - last - if (diff > minDuration) { - last = now - lastString = ServerTime.format(new Date(last)) - true - } else { - false - } - } - - def get: CharSequence = lastString - - def refreshAndGet(): CharSequence = { - refresh() - get - } -} - -object ServerTime { - private val format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z") - - def format(d: Date): CharSequence = new AsciiString(format.format(d)) - - def make(interval: zio.Duration): ServerTime = new ServerTime(interval.toMillis) - - def parse(s: CharSequence): Date = format.parse(s.toString) -}