Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Akka HTTP 10.2.0 #1087

Merged
merged 9 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import java.security.spec.PKCS8EncodedKeySpec
import java.security.{ KeyFactory, KeyStore, SecureRandom }

import scala.concurrent.duration._

import akka.actor.ActorSystem
import akka.util.ByteString
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
import akka.http.scaladsl.{ Http2, HttpsConnectionContext }
import akka.http.scaladsl.{ ConnectionContext, Http2 }
import akka.stream.SystemMaterializer
import io.grpc.internal.testing.TestUtils
import javax.net.ssl.{ KeyManagerFactory, SSLContext }
Expand Down Expand Up @@ -81,7 +80,7 @@ case class AkkaGrpcServerScala(serverHandlerFactory: ActorSystem => HttpRequest
val context = SSLContext.getInstance("TLS")
context.init(keyManagerFactory.getKeyManagers, null, new SecureRandom)

new HttpsConnectionContext(context)
ConnectionContext.httpsServer(context)
}

override def getPort(binding: (ActorSystem, ServerBinding)): Int = binding._2.localAddress.getPort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import akka.actor.ActorSystem
import akka.grpc.internal.{ GrpcEntityHelpers, GrpcProtocolNative, GrpcResponseHelpers, Identity }
import akka.http.scaladsl.model.{ HttpEntity, HttpHeader }
import akka.http.scaladsl.server.{ Directive0, Directives, Route }
import akka.stream.{ Materializer, SystemMaterializer }
import akka.stream.Materializer
import akka.stream.scaladsl.Source
import io.grpc.Status
import io.grpc.testing.integration.messages.{ SimpleRequest, StreamingOutputCallRequest }
Expand All @@ -24,8 +24,6 @@ object AkkaHttpServerProviderScala extends AkkaHttpServerProvider with Directive
Set()

val server = AkkaGrpcServerScala(implicit sys => {
implicit val ec = sys.dispatcher

val testServiceImpl = new TestServiceImpl()
val testServiceHandler = TestServiceHandler(testServiceImpl)

Expand All @@ -37,8 +35,7 @@ object AkkaHttpServerProviderScala extends AkkaHttpServerProvider with Directive
// customStatusRoute(testServiceImpl) ~ handleWith(testServiceHandler)
}

implicit val mat: Materializer = SystemMaterializer(sys).materializer
Route.asyncHandler(Route.seal(route))
Route.toFunction(Route.seal(route))
})

// Directive to implement the 'custom_metadata' test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class LoadBalancingIntegrationSpec extends AnyWordSpec with Matchers with Before
val service1 = new CountingGreeterServiceImpl()
val service2 = new CountingGreeterServiceImpl()

val server1 = Http().bindAndHandleAsync(GreeterServiceHandler(service1), "127.0.0.1", 0).futureValue
val server2 = Http().bindAndHandleAsync(GreeterServiceHandler(service2), "127.0.0.1", 0).futureValue
val server1 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service1)).futureValue
val server2 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service2)).futureValue

val discovery = MutableServiceDiscovery(List(server1, server2))
val client = GreeterServiceClient(
Expand All @@ -60,8 +60,12 @@ class LoadBalancingIntegrationSpec extends AnyWordSpec with Matchers with Before
val service2 = new CountingGreeterServiceImpl()

val server1 =
Http().bindAndHandleAsync(GreeterServiceHandler(service1), "127.0.0.1", 0)(service1materializer).futureValue
val server2 = Http().bindAndHandleAsync(GreeterServiceHandler(service2), "127.0.0.1", 0).futureValue
Http()
.newServerAt("127.0.0.1", 0)
.withMaterializer(service1materializer)
.bind(GreeterServiceHandler(service1))
.futureValue
val server2 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service2)).futureValue

val discovery = MutableServiceDiscovery(List(server1))
val client = GreeterServiceClient(
Expand Down Expand Up @@ -96,7 +100,7 @@ class LoadBalancingIntegrationSpec extends AnyWordSpec with Matchers with Before

"select the right endpoint among invalid ones" in {
val service = new CountingGreeterServiceImpl()
val server = Http().bindAndHandleAsync(GreeterServiceHandler(service), "127.0.0.1", 0).futureValue
val server = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service)).futureValue
val discovery =
new MutableServiceDiscovery(
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class NonBalancingIntegrationSpec extends AnyWordSpec with Matchers with BeforeA
val service1 = new CountingGreeterServiceImpl()
val service2 = new CountingGreeterServiceImpl()

val server1 = Http().bindAndHandleAsync(GreeterServiceHandler(service1), "127.0.0.1", 0).futureValue
val server2 = Http().bindAndHandleAsync(GreeterServiceHandler(service2), "127.0.0.1", 0).futureValue
val server1 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service1)).futureValue
val server2 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service2)).futureValue

val discovery = MutableServiceDiscovery(List(server1, server2))
val client = GreeterServiceClient(GrpcClientSettings.usingServiceDiscovery("greeter", discovery).withTls(false))
Expand All @@ -56,8 +56,12 @@ class NonBalancingIntegrationSpec extends AnyWordSpec with Matchers with BeforeA
val service2 = new CountingGreeterServiceImpl()

val server1 =
Http().bindAndHandleAsync(GreeterServiceHandler(service1), "127.0.0.1", 0)(service1materializer).futureValue
val server2 = Http().bindAndHandleAsync(GreeterServiceHandler(service2), "127.0.0.1", 0).futureValue
Http()
.newServerAt("127.0.0.1", 0)
.withMaterializer(service1materializer)
.bind(GreeterServiceHandler(service1))
.futureValue
val server2 = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service2)).futureValue

val discovery = MutableServiceDiscovery(List(server1))
val client = GreeterServiceClient(GrpcClientSettings.usingServiceDiscovery("greeter", discovery).withTls(false))
Expand Down Expand Up @@ -88,7 +92,7 @@ class NonBalancingIntegrationSpec extends AnyWordSpec with Matchers with BeforeA

"select the right endpoint among invalid ones" in {
val service = new CountingGreeterServiceImpl()
val server = Http().bindAndHandleAsync(GreeterServiceHandler(service), "127.0.0.1", 0).futureValue
val server = Http().newServerAt("127.0.0.1", 0).bind(GreeterServiceHandler(service)).futureValue
val discovery =
new MutableServiceDiscovery(
List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package example.myapp.helloworld

import java.util.concurrent.CompletionStage

import akka.actor.ActorSystem
import akka.grpc.internal.GrpcProtocolNative
import akka.http.scaladsl.Http
Expand Down Expand Up @@ -32,9 +34,14 @@ class ErrorReportingSpec extends AnyWordSpec with Matchers with ScalaFutures wit

val handler = GreeterServiceHandlerFactory.create(new GreeterServiceImpl(mat), sys)
val binding = {
import akka.http.javadsl.{ ConnectHttp, Http }
import akka.http.javadsl.Http
import akka.http.javadsl.model.{ HttpRequest, HttpResponse }

Http(sys).bindAndHandleAsync(handler, ConnectHttp.toHost("127.0.0.1", 0), mat).toCompletableFuture.get
Http(sys)
.newServerAt("127.0.0.1", 0)
.bind((req => handler(req)): akka.japi.function.Function[HttpRequest, CompletionStage[HttpResponse]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can (and should) make this simpler (#1101), but we can do that in a separate PR

.toCompletableFuture
.get
}

"respond with an 'unimplemented' gRPC error status when calling an unknown method" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package example.myapp
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.actor.ActorSystem
import akka.http.scaladsl.{ Http, HttpConnectionContext }
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.model.HttpResponse
import akka.grpc.scaladsl.ServerReflection
Expand Down Expand Up @@ -50,23 +50,17 @@ object CombinedServer {
ServiceHandler.concatOrNotFound(greeterService, echoService, reflectionService)

Http()
.bindAndHandleAsync(
serviceHandlers,
interface = "127.0.0.1",
port = 8080,
connectionContext = HttpConnectionContext())
.newServerAt("127.0.0.1", 8080)
.bind(serviceHandlers)
//#concatOrNotFound
.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") }

//#grpc-web
val grpcWebServiceHandlers = WebHandler.grpcWebHandler(greeterService, echoService)

Http()
.bindAndHandleAsync(
grpcWebServiceHandlers,
interface = "127.0.0.1",
port = 8081,
connectionContext = HttpConnectionContext())
.newServerAt("127.0.0.1", 8081)
.bind(grpcWebServiceHandlers)
//#grpc-web
.foreach { binding => println(s"gRPC-Web server bound to: ${binding.localAddress}") }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package example.myapp.helloworld

import akka.actor.ActorSystem
import akka.http.scaladsl.{ Http, Http2, HttpConnectionContext }
import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
import akka.http.scaladsl.server.{ Directive0, Route, RouteResult }
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.{ Http, Http2, HttpConnectionContext }
import akka.stream.{ ActorMaterializer, Materializer }
import com.typesafe.config.ConfigFactory
import example.myapp.helloworld.grpc._
Expand Down Expand Up @@ -71,7 +71,7 @@ class AuthenticatedGreeterServer(system: ActorSystem) {

// Bind service handler servers to localhost:8082
val binding = Http2().bindAndHandleAsync(
Route.asyncHandler(route),
Route.toFunction(route),
interface = "127.0.0.1",
port = 8082,
connectionContext = HttpConnectionContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package example.myapp.helloworld

import akka.actor.ActorSystem
import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
import akka.http.scaladsl.{ Http, HttpConnectionContext }
import akka.http.scaladsl.Http
import akka.stream.{ ActorMaterializer, Materializer }
import com.typesafe.config.ConfigFactory
import example.myapp.helloworld.grpc._
Expand Down Expand Up @@ -39,11 +39,7 @@ class GreeterServer(system: ActorSystem) {
GreeterServiceHandler(new GreeterServiceImpl())

// Bind service handler servers to localhost:8080/8081
val binding = Http().bindAndHandleAsync(
service,
interface = "127.0.0.1",
port = 8080,
connectionContext = HttpConnectionContext())
val binding = Http().newServerAt("127.0.0.1", 8080).bind(service)

// report successful binding
binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package example.myapp.helloworld

import akka.actor.ActorSystem
import akka.http.scaladsl.model.{ HttpRequest, HttpResponse }
import akka.http.scaladsl.{ Http, HttpConnectionContext }
import akka.http.scaladsl.Http
import akka.stream.{ ActorMaterializer, Materializer }
import example.myapp.helloworld.grpc._

Expand All @@ -25,11 +25,7 @@ class PowerGreeterServer(system: ActorSystem) {
GreeterServicePowerApiHandler(new PowerGreeterServiceImpl(mat))

// Bind service handler servers to localhost:8080/8081
val binding = Http().bindAndHandleAsync(
service,
interface = "127.0.0.1",
port = 8081,
connectionContext = HttpConnectionContext())
val binding = Http().newServerAt("127.0.0.1", 8081).bind(service)

// report successful binding
binding.foreach { binding => println(s"gRPC server bound to: ${binding.localAddress}") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import akka.grpc.internal.GrpcProtocolNative
import akka.http.scaladsl.model.HttpEntity.{ Chunked, LastChunk }
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.{ Http, HttpConnectionContext }
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import example.myapp.helloworld.grpc.{ GreeterService, GreeterServiceHandler }
Expand Down Expand Up @@ -37,11 +37,8 @@ class ErrorReportingSpec extends AnyWordSpec with Matchers with ScalaFutures wit
"A gRPC server" should {

val binding = Http()
.bindAndHandleAsync(
GreeterServiceHandler(new GreeterServiceImpl())(system.asInstanceOf[ClassicActorSystemProvider]),
interface = "127.0.0.1",
port = 0,
connectionContext = HttpConnectionContext())
.newServerAt("127.0.0.1", 0)
.bind(GreeterServiceHandler(new GreeterServiceImpl())(system.asInstanceOf[ClassicActorSystemProvider]))
.futureValue

"respond with an 'unimplemented' gRPC error status when calling an unknown method" in {
Expand Down
4 changes: 2 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ object Dependencies {
// https://doc.akka.io//docs/akka/current/project/downstream-upgrade-strategy.html
val akka = "2.5.31"
val akkaBinary = "2.5"
val akkaHttp = "10.1.12"
val akkaHttpBinary = "10.1"
val akkaHttp = "10.2.0"
val akkaHttpBinary = "10.2"

val grpc = "1.30.2" // checked synced by GrpcVersionSyncCheckPlugin

Expand Down
6 changes: 3 additions & 3 deletions runtime/src/main/scala/akka/grpc/javadsl/RouteUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ object RouteUtils {
def toFunction(
route: Route,
system: ClassicActorSystemProvider): Function[HttpRequest, CompletionStage[HttpResponse]] = {
implicit val sys = system.classicSystem
implicit val ec: ExecutionContext = sys.dispatcher
val handler = scaladsl.server.Route.asyncHandler(route.asScala)
implicit val sys = system
implicit val ec: ExecutionContext = system.classicSystem.dispatcher
val handler = scaladsl.server.Route.toFunction(route.asScala)

(request: HttpRequest) => {
import FutureConverters._
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/main/scala/akka/grpc/scaladsl/WebHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object WebHandler {
corsSettings: CorsSettings = defaultCorsSettings): HttpRequest => Future[HttpResponse] = {
implicit val system = as.classicSystem
val servicesHandler = ServiceHandler.concat(handlers: _*)
Route.asyncHandler(cors(corsSettings) {
Route.toFunction(cors(corsSettings) {
handleWith(servicesHandler)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ object AkkaHttpServerProviderScala extends AkkaHttpServerProvider {
requestHandler(ctx.request).map(Complete)
}

implicit val mat: Materializer = SystemMaterializer(sys).materializer
Route.asyncHandler(Route.seal(route))
Route.toFunction(Route.seal(route))
})

val echoHeaders: Directive0 = extractRequest.flatMap(request => {
Expand Down Expand Up @@ -79,4 +78,4 @@ object AkkaHttpClientProviderScala extends AkkaHttpClientProvider {
val label: String = "akka-grpc scala client tester"

def client = AkkaGrpcClientScala(settings => implicit sys => new AkkaGrpcClientTester(settings))
}
}