com.linecorp.armeria.common.util.EventLoopGroups Java Examples

The following examples show how to use com.linecorp.armeria.common.util.EventLoopGroups. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(0);

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .setMaxInboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                               .addService(ServerInterceptors.intercept(
                                       new UnitTestServiceImpl(),
                                       REPLACE_EXCEPTION, ADD_TO_CONTEXT))
                               .enableUnframedRequests(true)
                               .supportedSerializationFormats(
                                       GrpcSerializationFormats.values())
                               .useBlockingTaskExecutor(true)
                               .build()
                               .decorate(LoggingService.newDecorator())
                               .decorate((delegate, ctx, req) -> {
                                   ctx.log().whenComplete().thenAccept(requestLogQueue::add);
                                   return delegate.serve(ctx, req);
                               }));
}
 
Example #2
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(0);

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(new UnitTestServiceImpl())
                               .build()
                               .decorate(LoggingService.newDecorator())
                               .decorate((delegate, ctx, req) -> {
                                   ctx.log().whenComplete().thenAccept(requestLogQueue::add);
                                   return delegate.serve(ctx, req);
                               }));
}
 
Example #3
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(Long.MAX_VALUE);

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(new UnitTestServiceImpl())
                               .build()
                               .decorate(LoggingService.newDecorator())
                               .decorate((delegate, ctx, req) -> {
                                   ctx.log().whenComplete().thenAccept(requestLogQueue::add);
                                   return delegate.serve(ctx, req);
                               }));
}
 
Example #4
Source File: HttpClientMaxConcurrentStreamTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setUp() {
    clientFactory = ClientFactory.builder()
                                 .workerGroup(EventLoopGroups.newEventLoopGroup(1), true)
                                 .connectionPoolListener(connectionPoolListenerWrapper)
                                 .build();
}
 
Example #5
Source File: TwoElementFixedStreamMessageTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void cancelOnFirstElement() {
    final ByteBuf obj1 = newBuffer("obj1");
    final ByteBuf obj2 = newBuffer("obj2");
    final TwoElementFixedStreamMessage<ByteBuf> streamMessage =
            new TwoElementFixedStreamMessage<>(obj1, obj2);
    streamMessage.subscribe(new CancelSubscriber(1), EventLoopGroups.directEventLoop(),
                            WITH_POOLED_OBJECTS);

    assertThat(obj1.refCnt()).isZero();
    assertThat(obj2.refCnt()).isZero();
}
 
Example #6
Source File: TwoElementFixedStreamMessageTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void cancelOnSecondElement() {
    final ByteBuf obj1 = newBuffer("obj1");
    final ByteBuf obj2 = newBuffer("obj2");
    final TwoElementFixedStreamMessage<ByteBuf> streamMessage =
            new TwoElementFixedStreamMessage<>(obj1, obj2);
    streamMessage.subscribe(new CancelSubscriber(2), EventLoopGroups.directEventLoop(),
                            WITH_POOLED_OBJECTS);

    assertThat(obj1.refCnt()).isZero();
    assertThat(obj2.refCnt()).isZero();
}
 
Example #7
Source File: CentralDogma.java    From centraldogma with Apache License 2.0 4 votes vote down vote up
private Server startServer(ProjectManager pm, CommandExecutor executor,
                           PrometheusMeterRegistry meterRegistry, @Nullable SessionManager sessionManager) {
    final ServerBuilder sb = Server.builder();
    sb.verboseResponses(true);
    cfg.ports().forEach(sb::port);

    if (cfg.ports().stream().anyMatch(ServerPort::hasTls)) {
        try {
            final TlsConfig tlsConfig = cfg.tls();
            if (tlsConfig != null) {
                sb.tls(tlsConfig.keyCertChainFile(), tlsConfig.keyFile(), tlsConfig.keyPassword());
            } else {
                logger.warn(
                        "Missing TLS configuration. Generating a self-signed certificate for TLS support.");
                sb.tlsSelfSigned();
            }
        } catch (Exception e) {
            Exceptions.throwUnsafely(e);
        }
    }

    sb.clientAddressSources(cfg.clientAddressSourceList());
    sb.clientAddressTrustedProxyFilter(cfg.trustedProxyAddressPredicate());

    cfg.numWorkers().ifPresent(
            numWorkers -> sb.workerGroup(EventLoopGroups.newEventLoopGroup(numWorkers), true));
    cfg.maxNumConnections().ifPresent(sb::maxNumConnections);
    cfg.idleTimeoutMillis().ifPresent(sb::idleTimeoutMillis);
    cfg.requestTimeoutMillis().ifPresent(sb::requestTimeoutMillis);
    cfg.maxFrameLength().ifPresent(sb::maxRequestLength);
    cfg.gracefulShutdownTimeout().ifPresent(
            t -> sb.gracefulShutdownTimeoutMillis(t.quietPeriodMillis(), t.timeoutMillis()));

    final MetadataService mds = new MetadataService(pm, executor);
    final WatchService watchService = new WatchService(meterRegistry);
    final AuthProvider authProvider = createAuthProvider(executor, sessionManager, mds);

    configureThriftService(sb, pm, executor, watchService, mds);

    sb.service("/title", webAppTitleFile(cfg.webAppTitle(), SystemInfo.hostname()).asService());

    sb.service(HEALTH_CHECK_PATH, HealthCheckService.of());

    // TODO(hyangtack): This service is temporarily added to support redirection from '/docs' to '/docs/'.
    //                  It would be removed if this kind of redirection is handled by Armeria.
    sb.service("/docs", new AbstractHttpService() {
        @Override
        protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req)
                throws Exception {
            return HttpResponse.of(
                    ResponseHeaders.of(HttpStatus.TEMPORARY_REDIRECT, HttpHeaderNames.LOCATION, "/docs/"));
        }
    });
    sb.serviceUnder("/docs/",
                    DocService.builder()
                              .exampleHttpHeaders(CentralDogmaService.class,
                                                  HttpHeaders.of(HttpHeaderNames.AUTHORIZATION,
                                                                 "Bearer " + CsrfToken.ANONYMOUS))
                              .build());

    configureHttpApi(sb, pm, executor, watchService, mds, authProvider, sessionManager);

    configureMetrics(sb, meterRegistry);

    // Configure access log format.
    final String accessLogFormat = cfg.accessLogFormat();
    if (isNullOrEmpty(accessLogFormat)) {
        sb.accessLogWriter(AccessLogWriter.disabled(), true);
    } else if ("common".equals(accessLogFormat)) {
        sb.accessLogWriter(AccessLogWriter.common(), true);
    } else if ("combined".equals(accessLogFormat)) {
        sb.accessLogWriter(AccessLogWriter.combined(), true);
    } else {
        sb.accessLogFormat(accessLogFormat);
    }

    final Server s = sb.build();
    s.start().join();
    return s;
}
 
Example #8
Source File: ClientBuilderFactory.java    From curiostack with MIT License 4 votes vote down vote up
@Inject
public ClientBuilderFactory(
    MeterRegistry meterRegistry,
    Tracing tracing,
    Function<HttpClient, LoggingClient> loggingClient,
    Optional<SelfSignedCertificate> selfSignedCertificate,
    Optional<TrustManagerFactory> caTrustManager,
    ServerConfig serverConfig) {
  this.tracing = tracing;
  this.meterRegistry = meterRegistry;
  this.loggingClient = loggingClient;
  final TrustManagerFactory trustManagerFactory;
  if (serverConfig.isDisableClientCertificateVerification()) {
    logger.warn("Disabling client SSL verification. This should only happen on local!");
    trustManagerFactory = InsecureTrustManagerFactory.INSTANCE;
  } else if (caTrustManager.isPresent()) {
    trustManagerFactory = caTrustManager.get();
  } else {
    trustManagerFactory = null;
  }

  final Consumer<SslContextBuilder> clientCertificateCustomizer;
  if (selfSignedCertificate.isPresent()) {
    SelfSignedCertificate certificate = selfSignedCertificate.get();
    clientCertificateCustomizer =
        sslContext -> sslContext.keyManager(certificate.certificate(), certificate.privateKey());
  } else if (serverConfig.getTlsCertificatePath().isEmpty()
      || serverConfig.getTlsPrivateKeyPath().isEmpty()) {
    throw new IllegalStateException(
        "No TLS configuration provided, Curiostack does not support clients without TLS "
            + "certificates. Use gradle-curio-cluster-plugin to set up a namespace and TLS.");
  } else {
    String certPath =
        !serverConfig.getClientTlsCertificatePath().isEmpty()
            ? serverConfig.getClientTlsCertificatePath()
            : serverConfig.getTlsCertificatePath();
    String keyPath =
        !serverConfig.getClientTlsPrivateKeyPath().isEmpty()
            ? serverConfig.getClientTlsPrivateKeyPath()
            : serverConfig.getTlsPrivateKeyPath();
    clientCertificateCustomizer =
        sslContext ->
            SslContextKeyConverter.execute(
                ResourceUtil.openStream(certPath),
                ResourceUtil.openStream(keyPath),
                sslContext::keyManager);
  }

  final Consumer<SslContextBuilder> clientTlsCustomizer;
  if (trustManagerFactory != null) {
    clientTlsCustomizer =
        sslContext -> {
          clientCertificateCustomizer.accept(sslContext);
          sslContext.trustManager(trustManagerFactory);
        };
  } else {
    clientTlsCustomizer = clientCertificateCustomizer;
  }
  ClientFactoryBuilder factoryBuilder =
      ClientFactory.builder().tlsCustomizer(clientTlsCustomizer).meterRegistry(meterRegistry);
  if (serverConfig.getDisableEdns()) {
    factoryBuilder.addressResolverGroupFactory(
        eventLoopGroup ->
            new DnsAddressResolverGroup(
                new DnsNameResolverBuilder()
                    .channelType(EventLoopGroups.datagramChannelType(eventLoopGroup))
                    .nameServerProvider(DnsServerAddressStreamProviders.platformDefault())
                    .optResourceEnabled(false)));
  }
  clientFactory = factoryBuilder.build();
}
 
Example #9
Source File: ArmeriaServerHttpRequestTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
private static ServiceRequestContext newRequestContext(HttpRequest httpRequest) {
    return ServiceRequestContext.builder(httpRequest)
                                .eventLoop(EventLoopGroups.directEventLoop())
                                .build();
}
 
Example #10
Source File: EventLoopGroupRuleDelegate.java    From armeria with Apache License 2.0 4 votes vote down vote up
public void before() throws Throwable {
    group = EventLoopGroups.newEventLoopGroup(numThreads, threadNamePrefix, useDaemonThreads);
}
 
Example #11
Source File: GrpcClientTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(MAX_MESSAGE_SIZE);
    sb.idleTimeoutMillis(0);
    sb.http(0);
    sb.https(0);
    sb.tlsSelfSigned();

    final ServerServiceDefinition interceptService =
            ServerInterceptors.intercept(
                    new TestServiceImpl(Executors.newSingleThreadScheduledExecutor()),
                    new ServerInterceptor() {
                        @Override
                        public <REQ, RESP> Listener<REQ> interceptCall(
                                ServerCall<REQ, RESP> call,
                                Metadata requestHeaders,
                                ServerCallHandler<REQ, RESP> next) {
                            final HttpHeadersBuilder fromClient = HttpHeaders.builder();
                            MetadataUtil.fillHeaders(requestHeaders, fromClient);
                            CLIENT_HEADERS_CAPTURE.set(fromClient.build());
                            return next.startCall(
                                    new SimpleForwardingServerCall<REQ, RESP>(call) {
                                        @Override
                                        public void close(Status status, Metadata trailers) {
                                            trailers.merge(requestHeaders);
                                            super.close(status, trailers);
                                        }
                                    }, requestHeaders);
                        }
                    });

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(interceptService)
                               .setMaxInboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                               .setMaxOutboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                               .useClientTimeoutHeader(false)
                               .build()
                               .decorate((client, ctx, req) -> {
                                   final HttpResponse res = client.serve(ctx, req);
                                   return new FilteredHttpResponse(res) {
                                       private boolean headersReceived;

                                       @Override
                                       protected HttpObject filter(HttpObject obj) {
                                           if (obj instanceof HttpHeaders) {
                                               if (!headersReceived) {
                                                   headersReceived = true;
                                               } else {
                                                   SERVER_TRAILERS_CAPTURE.set((HttpHeaders) obj);
                                               }
                                           }
                                           return obj;
                                       }
                                   };
                               }));
}
 
Example #12
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(0);

    sb.service(
            GrpcService.builder()
                       .setMaxInboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                       .addService(ServerInterceptors.intercept(
                               new UnitTestServiceImpl(),
                               REPLACE_EXCEPTION, ADD_TO_CONTEXT))
                       .enableUnframedRequests(true)
                       .supportedSerializationFormats(GrpcSerializationFormats.values())
                       .build(),
            service -> service
                    .decorate(LoggingService.newDecorator())
                    .decorate((delegate, ctx, req) -> {
                        ctx.log().whenComplete().thenAccept(requestLogQueue::add);
                        return delegate.serve(ctx, req);
                    }));

    // For simplicity, mount onto subpaths with custom options
    sb.serviceUnder(
            "/json-preserving/",
            GrpcService.builder()
                       .addService(new UnitTestServiceImpl())
                       .supportedSerializationFormats(GrpcSerializationFormats.values())
                       .jsonMarshallerFactory(serviceDescriptor -> {
                           return GrpcJsonMarshaller.builder()
                                                    .jsonMarshallerCustomizer(marshaller -> {
                                                        marshaller.preservingProtoFieldNames(true);
                                                    })
                                                    .build(serviceDescriptor);
                       })
                       .build());
    sb.serviceUnder(
            "/no-client-timeout/",
            GrpcService.builder()
                       .addService(new UnitTestServiceImpl())
                       .useClientTimeoutHeader(false)
                       .build());

    sb.service(
            GrpcService.builder()
                       .addService(ProtoReflectionService.newInstance())
                       .build(),
            service -> service.decorate(LoggingService.newDecorator()));
}
 
Example #13
Source File: ClientFactoryOptionsTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@BeforeAll
static void setUp() {
    executors = EventLoopGroups.newEventLoopGroup(1);
}
 
Example #14
Source File: RetryingClientTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void shouldGetExceptionWhenFactoryIsClosed() {
    final ClientFactory factory =
            ClientFactory.builder().workerGroup(EventLoopGroups.newEventLoopGroup(2), true).build();

    // Retry after 8000 which is slightly less than responseTimeoutMillis(10000).
    final Function<? super HttpClient, RetryingClient> retryingDecorator =
            RetryingClient.builder(RetryRule.builder()
                                            .onServerErrorStatus()
                                            .onException()
                                            .thenBackoff(Backoff.fixed(8000)))
                          .newDecorator();

    final WebClient client = WebClient.builder(server.httpUri())
                                      .factory(factory)
                                      .responseTimeoutMillis(10000)
                                      .decorator(retryingDecorator)
                                      .decorator(LoggingClient.newDecorator())
                                      .build();

    // There's no way to notice that the RetryingClient has scheduled the next retry.
    // The next retry will be after 8 seconds so closing the factory after 3 seconds should work.
    Executors.newSingleThreadScheduledExecutor().schedule(factory::close, 3, TimeUnit.SECONDS);

    // But it turned out that it's not working as expected in certain circumstance,
    // so we should handle all the cases.
    //
    // 1 - In RetryingClient, IllegalStateException("ClientFactory has been closed.") can be raised.
    // 2 - In HttpChannelPool, BootStrap.connect() can raise
    //     IllegalStateException("executor not accepting a task") wrapped by UnprocessedRequestException.
    // 3 - In HttpClientDelegate, addressResolverGroup.getResolver(eventLoop) can raise
    //     IllegalStateException("executor not accepting a task").
    //

    // Peel CompletionException first.
    Throwable t = peel(catchThrowable(() -> client.get("/service-unavailable").aggregate().join()));
    if (t instanceof UnprocessedRequestException) {
        final Throwable cause = t.getCause();
        assertThat(cause).isInstanceOf(IllegalStateException.class);
        t = cause;
    }
    assertThat(t).isInstanceOf(IllegalStateException.class)
                 .satisfies(cause -> assertThat(cause.getMessage()).matches(
                         "(?i).*(factory has been closed|not accepting a task).*"));
}
 
Example #15
Source File: ConnectionLimitingHandlerIntegrationTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxNumConnections(2);
    sb.serviceUnder("/", new AbstractHttpService() {});
}