From c3d7952d70afc911e4826bb3f2b8e851156775bb Mon Sep 17 00:00:00 2001 From: zsfVishnu Date: Sat, 16 Apr 2022 17:58:48 +0530 Subject: [PATCH 1/6] feat: added channel option to Server --- .../src/main/scala/zhttp/service/Server.scala | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 5200ccb2a0..4203429d9d 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -1,7 +1,7 @@ package zhttp.service import io.netty.bootstrap.ServerBootstrap -import io.netty.channel.ChannelPipeline +import io.netty.channel.{ChannelOption, ChannelPipeline} import io.netty.util.ResourceLeakDetector import zhttp.http.Http._ import zhttp.http.{Http, HttpApp} @@ -32,6 +32,7 @@ sealed trait Server[-R, +E] { self => case UnsafeChannelPipeline(init) => s.copy(channelInitializer = init) case RequestDecompression(enabled, strict) => s.copy(requestDecompression = (enabled, strict)) case ObjectAggregator(maxRequestSize) => s.copy(objectAggregator = maxRequestSize) + case ServerOption(channelOption) => s.copy(option = channelOption) } def make(implicit @@ -140,7 +141,6 @@ sealed trait Server[-R, +E] { self => def withObjectAggregator(maxRequestSize: Int = Int.MaxValue): Server[R, E] = Concat(self, ObjectAggregator(maxRequestSize)) } - object Server { private[zhttp] final case class Config[-R, +E]( leakDetectionLevel: LeakDetectionLevel = LeakDetectionLevel.SIMPLE, @@ -157,6 +157,7 @@ object Server { channelInitializer: ChannelPipeline => Unit = null, requestDecompression: (Boolean, Boolean) = (false, false), objectAggregator: Int = -1, + option: List[ServerChannelOption] = List(), ) { def useAggregator: Boolean = objectAggregator >= 0 } @@ -179,6 +180,7 @@ object Server { private final case class UnsafeChannelPipeline(init: ChannelPipeline => Unit) extends UServer private final case class RequestDecompression(enabled: Boolean, strict: Boolean) extends UServer private final case class ObjectAggregator(maxRequestSize: Int) extends UServer + private final case class ServerOption(channelOption: List[ServerChannelOption]) extends UServer def app[R, E](http: HttpApp[R, E]): Server[R, E] = Server.App(http) def port(port: Int): UServer = Server.Address(new InetSocketAddress(port)) @@ -197,8 +199,9 @@ object Server { val paranoidLeakDetection: UServer = LeakDetection(LeakDetectionLevel.PARANOID) val disableKeepAlive: UServer = Server.KeepAlive(false) val consolidateFlush: UServer = ConsolidateFlush(true) - def unsafePipeline(pipeline: ChannelPipeline => Unit): UServer = UnsafeChannelPipeline(pipeline) - def enableObjectAggregator(maxRequestSize: Int = Int.MaxValue): UServer = ObjectAggregator(maxRequestSize) + def unsafePipeline(pipeline: ChannelPipeline => Unit): UServer = UnsafeChannelPipeline(pipeline) + def enableObjectAggregator(maxRequestSize: Int = Int.MaxValue): UServer = ObjectAggregator(maxRequestSize) + def withServerOption(channelOptionList: List[ServerChannelOption]): UServer = ServerOption(channelOptionList) /** * Creates a server from a http app. @@ -252,6 +255,10 @@ object Server { reqHandler = settings.app.compile(zExec, settings, ServerTime.make) init = ServerChannelInitializer(zExec, settings, reqHandler) serverBootstrap = new ServerBootstrap().channelFactory(channelFactory).group(eventLoopGroup) + _ = if (settings.option.nonEmpty) + settings.option.foldLeft[ServerBootstrap](serverBootstrap)((serverBootstrap, i) => + serverBootstrap.option(i.channelOption, i.value), + ) chf <- ZManaged.effect(serverBootstrap.childHandler(init).bind(settings.address)) _ <- ChannelFuture.asManaged(chf) port <- ZManaged.effect(chf.channel().localAddress().asInstanceOf[InetSocketAddress].getPort) @@ -261,3 +268,5 @@ object Server { } } } + +case class ServerChannelOption(channelOption: ChannelOption[Any], value: Any) From 3ce8ee42e198126c8aaecd1dec6d7d907ed74160 Mon Sep 17 00:00:00 2001 From: zsfVishnu Date: Tue, 19 Apr 2022 20:29:39 +0530 Subject: [PATCH 2/6] feat: added unsafe unsafe serverbootstrap --- .../src/main/scala/zhttp/service/Server.scala | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 4203429d9d..8617049755 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -1,7 +1,7 @@ package zhttp.service import io.netty.bootstrap.ServerBootstrap -import io.netty.channel.{ChannelOption, ChannelPipeline} +import io.netty.channel.ChannelPipeline import io.netty.util.ResourceLeakDetector import zhttp.http.Http._ import zhttp.http.{Http, HttpApp} @@ -32,7 +32,7 @@ sealed trait Server[-R, +E] { self => case UnsafeChannelPipeline(init) => s.copy(channelInitializer = init) case RequestDecompression(enabled, strict) => s.copy(requestDecompression = (enabled, strict)) case ObjectAggregator(maxRequestSize) => s.copy(objectAggregator = maxRequestSize) - case ServerOption(channelOption) => s.copy(option = channelOption) + case UnsafeServerbootstrap(init) => s.copy(serverbootstrapInitializer = init) } def make(implicit @@ -126,6 +126,10 @@ sealed trait Server[-R, +E] { self => def withUnsafeChannelPipeline(unsafePipeline: ChannelPipeline => Unit): Server[R, E] = Concat(self, UnsafeChannelPipeline(unsafePipeline)) + /** createing a new serverBoostrap. Will modify this TODO */ + def withUnsafeServerBootstrap(unsafeServerbootstrap: ServerBootstrap => Unit): Server[R, E] = + Concat(self, UnsafeServerbootstrap(unsafeServerbootstrap)) + /** * Creates a new server with netty's HttpContentDecompressor to decompress * Http requests (@see Unit = null, requestDecompression: (Boolean, Boolean) = (false, false), objectAggregator: Int = -1, - option: List[ServerChannelOption] = List(), + serverbootstrapInitializer: ServerBootstrap => Unit = null, ) { def useAggregator: Boolean = objectAggregator >= 0 } @@ -180,7 +184,7 @@ object Server { private final case class UnsafeChannelPipeline(init: ChannelPipeline => Unit) extends UServer private final case class RequestDecompression(enabled: Boolean, strict: Boolean) extends UServer private final case class ObjectAggregator(maxRequestSize: Int) extends UServer - private final case class ServerOption(channelOption: List[ServerChannelOption]) extends UServer + private final case class UnsafeServerbootstrap(init: ServerBootstrap => Unit) extends UServer def app[R, E](http: HttpApp[R, E]): Server[R, E] = Server.App(http) def port(port: Int): UServer = Server.Address(new InetSocketAddress(port)) @@ -199,9 +203,9 @@ object Server { val paranoidLeakDetection: UServer = LeakDetection(LeakDetectionLevel.PARANOID) val disableKeepAlive: UServer = Server.KeepAlive(false) val consolidateFlush: UServer = ConsolidateFlush(true) - def unsafePipeline(pipeline: ChannelPipeline => Unit): UServer = UnsafeChannelPipeline(pipeline) - def enableObjectAggregator(maxRequestSize: Int = Int.MaxValue): UServer = ObjectAggregator(maxRequestSize) - def withServerOption(channelOptionList: List[ServerChannelOption]): UServer = ServerOption(channelOptionList) + def unsafePipeline(pipeline: ChannelPipeline => Unit): UServer = UnsafeChannelPipeline(pipeline) + def enableObjectAggregator(maxRequestSize: Int = Int.MaxValue): UServer = ObjectAggregator(maxRequestSize) + def unsafeServerbootstrap(serverBootstrap: ServerBootstrap => Unit): UServer = UnsafeServerbootstrap(serverBootstrap) /** * Creates a server from a http app. @@ -255,10 +259,6 @@ object Server { reqHandler = settings.app.compile(zExec, settings, ServerTime.make) init = ServerChannelInitializer(zExec, settings, reqHandler) serverBootstrap = new ServerBootstrap().channelFactory(channelFactory).group(eventLoopGroup) - _ = if (settings.option.nonEmpty) - settings.option.foldLeft[ServerBootstrap](serverBootstrap)((serverBootstrap, i) => - serverBootstrap.option(i.channelOption, i.value), - ) chf <- ZManaged.effect(serverBootstrap.childHandler(init).bind(settings.address)) _ <- ChannelFuture.asManaged(chf) port <- ZManaged.effect(chf.channel().localAddress().asInstanceOf[InetSocketAddress].getPort) @@ -268,5 +268,3 @@ object Server { } } } - -case class ServerChannelOption(channelOption: ChannelOption[Any], value: Any) From 6183cc4d2cb7eb7c4b2146c48a50b409e021d8d8 Mon Sep 17 00:00:00 2001 From: zsfVishnu Date: Tue, 19 Apr 2022 20:34:32 +0530 Subject: [PATCH 3/6] feat: minor change in the fn description --- zio-http/src/main/scala/zhttp/service/Server.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 8617049755..ca85dec56e 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -126,7 +126,7 @@ sealed trait Server[-R, +E] { self => def withUnsafeChannelPipeline(unsafePipeline: ChannelPipeline => Unit): Server[R, E] = Concat(self, UnsafeChannelPipeline(unsafePipeline)) - /** createing a new serverBoostrap. Will modify this TODO */ + /** Creates a new serverBoostrap with custom configs **/ def withUnsafeServerBootstrap(unsafeServerbootstrap: ServerBootstrap => Unit): Server[R, E] = Concat(self, UnsafeServerbootstrap(unsafeServerbootstrap)) From 268e6fa871868cb427d550aa85d5cf64c80e52e3 Mon Sep 17 00:00:00 2001 From: zsfVishnu Date: Tue, 19 Apr 2022 20:38:29 +0530 Subject: [PATCH 4/6] feat: formatting fix --- zio-http/src/main/scala/zhttp/service/Server.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index ca85dec56e..b540c9ada8 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -126,7 +126,7 @@ sealed trait Server[-R, +E] { self => def withUnsafeChannelPipeline(unsafePipeline: ChannelPipeline => Unit): Server[R, E] = Concat(self, UnsafeChannelPipeline(unsafePipeline)) - /** Creates a new serverBoostrap with custom configs **/ + /** Creates a new serverBoostrap with custom configs * */ def withUnsafeServerBootstrap(unsafeServerbootstrap: ServerBootstrap => Unit): Server[R, E] = Concat(self, UnsafeServerbootstrap(unsafeServerbootstrap)) From c097f9e92dd60af2023e155f7ad1a85e421e49f1 Mon Sep 17 00:00:00 2001 From: zsfVishnu <34836841+zsfVishnu@users.noreply.github.com> Date: Fri, 22 Apr 2022 15:04:45 +0530 Subject: [PATCH 5/6] feat: update doc Co-authored-by: Tushar Mathur --- zio-http/src/main/scala/zhttp/service/Server.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index b540c9ada8..5cfa64c7d6 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -126,7 +126,10 @@ sealed trait Server[-R, +E] { self => def withUnsafeChannelPipeline(unsafePipeline: ChannelPipeline => Unit): Server[R, E] = Concat(self, UnsafeChannelPipeline(unsafePipeline)) - /** Creates a new serverBoostrap with custom configs * */ + /** + * Provides unsafe access to netty's ServerBootstrap. + * Modifying server bootstrap is generally not advised unless you know what you are doing. + */ def withUnsafeServerBootstrap(unsafeServerbootstrap: ServerBootstrap => Unit): Server[R, E] = Concat(self, UnsafeServerbootstrap(unsafeServerbootstrap)) From 7026e2df3b84488c01fc77204bd566b14dc85835 Mon Sep 17 00:00:00 2001 From: zsfVishnu Date: Fri, 22 Apr 2022 16:39:45 +0530 Subject: [PATCH 6/6] fix: doc formatting --- zio-http/src/main/scala/zhttp/service/Server.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zio-http/src/main/scala/zhttp/service/Server.scala b/zio-http/src/main/scala/zhttp/service/Server.scala index 5cfa64c7d6..bb3520761b 100644 --- a/zio-http/src/main/scala/zhttp/service/Server.scala +++ b/zio-http/src/main/scala/zhttp/service/Server.scala @@ -126,9 +126,9 @@ sealed trait Server[-R, +E] { self => def withUnsafeChannelPipeline(unsafePipeline: ChannelPipeline => Unit): Server[R, E] = Concat(self, UnsafeChannelPipeline(unsafePipeline)) - /** - * Provides unsafe access to netty's ServerBootstrap. - * Modifying server bootstrap is generally not advised unless you know what you are doing. + /** + * Provides unsafe access to netty's ServerBootstrap. Modifying server + * bootstrap is generally not advised unless you know what you are doing. */ def withUnsafeServerBootstrap(unsafeServerbootstrap: ServerBootstrap => Unit): Server[R, E] = Concat(self, UnsafeServerbootstrap(unsafeServerbootstrap))