/** * Returns the {@link MeterRegistry} that collects various stats. */ public MeterRegistry meterRegistry() { return config().meterRegistry(); }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("config", config()) .add("activePorts", activePorts()) .add("state", startStop) .toString(); }
@Override public void serviceAdded(ServiceConfig cfg) { if (maxInboundMessageSizeBytes == NO_MAX_INBOUND_MESSAGE_SIZE) { maxInboundMessageSizeBytes = (int) Math.min(cfg.server().config().defaultMaxRequestLength(), Integer.MAX_VALUE); } }
/** * Returns the hostname of the default {@link VirtualHost}, which is the hostname of the machine unless * configured explicitly via {@link ServerBuilder#defaultVirtualHost(VirtualHost)}. */ public String defaultHostname() { return config().defaultVirtualHost().defaultHostname(); }
/** * Returns a {@link EventLoop} from the worker group. This can be used for, e.g., scheduling background * tasks for the lifetime of the {@link Server} using * {@link EventLoop#scheduleAtFixedRate(Runnable, long, long, TimeUnit)}. It is very important that these * tasks do not block as this would block all requests in the server on that {@link EventLoop}. */ public EventLoop nextEventLoop() { return config().workerGroup().next(); }
void start() throws Exception { boolean success = false; try { assert armeriaServer != null; server = serverFactory.apply(armeriaServer.config().blockingTaskExecutor()); connector = new ArmeriaConnector(server); server.addConnector(connector); if (!server.isStarted()) { logger.info("Starting an embedded Jetty: {}", server); server.start(); startedServer = true; } else { startedServer = false; } success = true; } finally { if (!success) { server = null; connector = null; } } }
private static ServiceConfig findServiceConfig(Server server, String path, Service<?, ?> service) { for (ServiceConfig cfg : server.config().defaultVirtualHost().serviceConfigs()) { final Optional<String> exactPath = cfg.pathMapping().exactPath(); if (!exactPath.isPresent()) { continue; } if (!path.equals(exactPath.get())) { continue; } if (cfg.service().as(service.getClass()).isPresent()) { return cfg; } } throw new Error(); // Never reaches here. } }
@Override public void serverStarting(Server server) throws Exception { final ServerConfig config = server.config(); final List<VirtualHost> virtualHosts = config.findVirtualHosts(DocService.this); final List<ServiceConfig> services = config.serviceConfigs().stream() .filter(se -> virtualHosts.contains(se.virtualHost())) .collect(toImmutableList()); ServiceSpecification spec = generate(services); spec = addDocStrings(spec, services); spec = addExamples(spec); vfs(SPECIFICATION_INDEX).setContent(jsonMapper.writerWithDefaultPrettyPrinter() .writeValueAsBytes(spec)); } });
@Override public ExecutorService blockingTaskExecutor() { if (blockingTaskExecutor != null) { return blockingTaskExecutor; } return blockingTaskExecutor = makeContextAware(server().config().blockingTaskExecutor()); }
private void setupServerMetrics() { final MeterRegistry meterRegistry = config().meterRegistry(); final GracefulShutdownSupport gracefulShutdownSupport = this.gracefulShutdownSupport; assert gracefulShutdownSupport != null; meterRegistry.gauge("armeria.server.pendingResponses", gracefulShutdownSupport, GracefulShutdownSupport::pendingResponses); meterRegistry.gauge("armeria.server.connections", connectionLimitingHandler, ConnectionLimitingHandler::numConnections); }
@Override protected CompletionStage<Void> doStart() { if (config().gracefulShutdownQuietPeriod().isZero()) { gracefulShutdownSupport = GracefulShutdownSupport.createDisabled(); } else { gracefulShutdownSupport = GracefulShutdownSupport.create(config().gracefulShutdownQuietPeriod(), config().blockingTaskExecutor()); final List<ServerPort> ports = config().ports();
private RequestContextAwareLogger newLogger(ServiceConfig cfg) { String loggerName = cfg.loggerName().orElse(null); if (loggerName == null) { loggerName = cfg.pathMapping().loggerName(); } return new RequestContextAwareLogger(this, LoggerFactory.getLogger( cfg.server().config().serviceLoggerPrefix() + '.' + loggerName)); }
final ServerConfig serverCfg = cfg.server().config(); requestTimeoutMillis = serverCfg.defaultRequestTimeoutMillis(); maxRequestLength = serverCfg.defaultMaxRequestLength();
private static Service<?, ?> service(ServerBuilder sb) { final Server server = sb.build(); return server.config().defaultVirtualHost().serviceConfigs().get(0).service(); }
if (cause != null) { final HttpResponse errorRes; if (ctx.server().config().verboseResponses()) { errorRes = HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR, MediaType.PLAIN_TEXT_UTF_8,
static HttpHeaders statusToTrailers(ServiceRequestContext ctx, Status status, boolean headersSent) { final HttpHeaders trailers; if (headersSent) { // Normal trailers. trailers = new DefaultHttpHeaders(); } else { // Trailers only response trailers = new DefaultHttpHeaders(true, 3, true) .status(HttpStatus.OK) .set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto"); } trailers.add(GrpcHeaderNames.GRPC_STATUS, Integer.toString(status.getCode().value())); if (status.getDescription() != null) { trailers.add(GrpcHeaderNames.GRPC_MESSAGE, StatusMessageEscaper.escape(status.getDescription())); } if (ctx.server().config().verboseResponses() && status.getCause() != null) { final ThrowableProto proto = GrpcStatus.serializeThrowable(status.getCause()); trailers.add(GrpcHeaderNames.ARMERIA_GRPC_THROWABLEPROTO_BIN, Base64.getEncoder().encodeToString(proto.toByteArray())); } return trailers; }
appException = (TApplicationException) cause; } else { if (ctx.server().config().verboseResponses()) { appException = new TApplicationException( TApplicationException.INTERNAL_ERROR,
if (ctx.server().config().verboseResponses()) { errorRes = HttpResponse.of(HttpStatus.BAD_REQUEST, MediaType.PLAIN_TEXT_UTF_8, "Failed to decode a %s header: %s", serializationFormat,
server.config().defaultVirtualHost(), localAddress().getHostString(), path(),
@Override public void serviceAdded(ServiceConfig cfg) throws Exception { if (maxInboundMessageSizeBytes == NO_MAX_INBOUND_MESSAGE_SIZE) { maxInboundMessageSizeBytes = (int) cfg.server().config().defaultMaxRequestLength(); } }