All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redislabs.picocliredis.LettuceOptions Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.redislabs.picocliredis;

import java.io.File;
import java.time.Duration;

import io.lettuce.core.ClientOptions;
import io.lettuce.core.RedisURI;
import io.lettuce.core.SslOptions;
import io.lettuce.core.event.DefaultEventPublisherOptions;
import io.lettuce.core.event.metrics.CommandLatencyEvent;
import io.lettuce.core.metrics.DefaultCommandLatencyCollectorOptions;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
import lombok.Data;
import picocli.CommandLine.Option;

public @Data class LettuceOptions {

	@Option(names = "--api", description = "API: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE})", paramLabel = "")
	private Api api = Api.Async;
	@Option(names = "--ks", description = "Path to keystore", paramLabel = "")
	private File keystore;
	@Option(names = "--ks-password", arity = "0..1", interactive = true, description = "Keystore password", paramLabel = "")
	private String keystorePassword;
	@Option(names = "--ts", description = "Path to truststore", paramLabel = "")
	private File truststore;
	@Option(names = "--ts-password", arity = "0..1", interactive = true, description = "Truststore password", paramLabel = "")
	private String truststorePassword;
	@Option(names = "--comp-threads", description = "Number of computation threads (default: ${DEFAULT-VALUE})", paramLabel = "")
	private int computationThreadPoolSize = DefaultClientResources.DEFAULT_COMPUTATION_THREADS;
	@Option(names = "--io-threads", description = "Number of threads for I/O operations (default: ${DEFAULT-VALUE})", paramLabel = "")
	private int ioThreadPoolSize = DefaultClientResources.DEFAULT_IO_THREADS;
	@Option(names = "--command-timeout", description = "Timeout for sync command execution (default: ${DEFAULT-VALUE})", paramLabel = "")
	private long commandTimeout = RedisURI.DEFAULT_TIMEOUT;
	@Option(names = "--metrics", description = "Show metrics")
	private boolean showMetrics;
	@Option(names = "--publish-on-sched", description = "Enable publish on scheduler (default: ${DEFAULT-VALUE})")
	private boolean publishOnScheduler = ClientOptions.DEFAULT_PUBLISH_ON_SCHEDULER;
	@Option(names = "--no-auto-reconnect", description = "Disable auto-reconnect")
	private boolean noAutoReconnect = false;
	@Option(names = "--request-queue", description = "Per-connection request queue size (default: max)", paramLabel = "")
	private int requestQueueSize = ClientOptions.DEFAULT_REQUEST_QUEUE_SIZE;
	@Option(names = "--ssl-provider", description = "SSL provider: ${COMPLETION-CANDIDATES} (default: ${DEFAULT-VALUE})", paramLabel = "")
	private SslProvider sslProvider = SslProvider.Jdk;

	public ClientOptions clientOptions(boolean ssl, ClientOptions.Builder builder) {
		if (ssl) {
			builder.sslOptions(sslOptions());
		}
		builder.publishOnScheduler(publishOnScheduler);
		builder.autoReconnect(!noAutoReconnect);
		builder.requestQueueSize(requestQueueSize);
		return builder.build();
	}

	private SslOptions sslOptions() {
		SslOptions.Builder builder = SslOptions.builder();
		switch (sslProvider) {
		case OpenSsl:
			builder.openSslProvider();
			break;
		default:
			builder.jdkSslProvider();
			break;
		}
		if (keystore != null) {
			if (keystorePassword == null) {
				builder.keystore(keystore);
			} else {
				builder.keystore(keystore, keystorePassword.toCharArray());
			}
		}
		if (truststore != null) {
			if (truststorePassword == null) {
				builder.truststore(truststore);
			} else {
				builder.truststore(truststore, truststorePassword);
			}
		}
		return builder.build();
	}

	public ClientResources clientResources() {
		DefaultClientResources.Builder builder = DefaultClientResources.builder();
		builder.computationThreadPoolSize(computationThreadPoolSize);
		builder.ioThreadPoolSize(ioThreadPoolSize);
		if (showMetrics) {
			builder.commandLatencyCollectorOptions(DefaultCommandLatencyCollectorOptions.builder().enable().build());
			builder.commandLatencyPublisherOptions(
					DefaultEventPublisherOptions.builder().eventEmitInterval(Duration.ofSeconds(1)).build());
		}
		ClientResources resources = builder.build();
		if (showMetrics) {
			resources.eventBus().get().filter(redisEvent -> redisEvent instanceof CommandLatencyEvent)
					.cast(CommandLatencyEvent.class).subscribe(e -> System.out.println(e.getLatencies()));
		}
		return resources;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy