|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending |
| 3 | +// |
| 4 | +package io.deephaven.client.impl; |
| 5 | + |
| 6 | +import io.deephaven.annotations.BuildableStyle; |
| 7 | +import io.grpc.ManagedChannel; |
| 8 | +import org.apache.arrow.memory.BufferAllocator; |
| 9 | +import org.immutables.value.Value.Default; |
| 10 | +import org.immutables.value.Value.Immutable; |
| 11 | + |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.concurrent.ScheduledExecutorService; |
| 14 | + |
| 15 | +@Immutable |
| 16 | +@BuildableStyle |
| 17 | +public abstract class BarrageSessionFactoryConfig { |
| 18 | + private static final SessionConfig SESSION_CONFIG_EMPTY = SessionConfig.builder().build(); |
| 19 | + |
| 20 | + public static Builder builder() { |
| 21 | + return ImmutableBarrageSessionFactoryConfig.builder(); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * The client configuration. |
| 26 | + */ |
| 27 | + public abstract ClientConfig clientConfig(); |
| 28 | + |
| 29 | + /** |
| 30 | + * The client channel factory. By default is {@link ClientChannelFactory#defaultInstance()}. |
| 31 | + */ |
| 32 | + @Default |
| 33 | + public ClientChannelFactory clientChannelFactory() { |
| 34 | + return ClientChannelFactory.defaultInstance(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * The default session config, used by the factory when {@link SessionConfig} is not provided. By default is |
| 39 | + * {@code SessionConfig.builder().build()}. |
| 40 | + */ |
| 41 | + @Default |
| 42 | + public SessionConfig sessionConfig() { |
| 43 | + return SESSION_CONFIG_EMPTY; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * The scheduler, used by the factory when {@link SessionConfig#scheduler()} is not set. |
| 48 | + */ |
| 49 | + public abstract ScheduledExecutorService scheduler(); |
| 50 | + |
| 51 | + /** |
| 52 | + * The allocator. |
| 53 | + */ |
| 54 | + public abstract BufferAllocator allocator(); |
| 55 | + |
| 56 | + /** |
| 57 | + * Creates a new factory with a new {@link ManagedChannel}. |
| 58 | + * |
| 59 | + * @return the factory |
| 60 | + */ |
| 61 | + public final Factory factory() { |
| 62 | + return new Factory(SessionFactoryConfig.builder() |
| 63 | + .clientConfig(clientConfig()) |
| 64 | + .clientChannelFactory(clientChannelFactory()) |
| 65 | + .sessionConfig(sessionConfig()) |
| 66 | + .scheduler(scheduler()) |
| 67 | + .build() |
| 68 | + .factory()); |
| 69 | + } |
| 70 | + |
| 71 | + public final class Factory implements BarrageSessionFactory { |
| 72 | + private final SessionFactoryConfig.Factory factory; |
| 73 | + |
| 74 | + private Factory(SessionFactoryConfig.Factory factory) { |
| 75 | + this.factory = Objects.requireNonNull(factory); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public BarrageSession newBarrageSession() { |
| 80 | + final Session session = factory.newSession(); |
| 81 | + return BarrageSession.of((SessionImpl) session, allocator(), factory.managedChannel()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Creates a new {@link BarrageSession} with {@code sessionConfig}. Closing the session does <b>not</b> close |
| 86 | + * the {@link #managedChannel()}. |
| 87 | + * |
| 88 | + * @param sessionConfig the session config |
| 89 | + * @return the new barrage session |
| 90 | + */ |
| 91 | + public BarrageSession newBarrageSession(SessionConfig sessionConfig) { |
| 92 | + final Session session = factory.newSession(sessionConfig); |
| 93 | + return BarrageSession.of((SessionImpl) session, allocator(), factory.managedChannel()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ManagedChannel managedChannel() { |
| 98 | + return factory.managedChannel(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // ------------------------------------------------ |
| 103 | + |
| 104 | + public interface Builder { |
| 105 | + |
| 106 | + Builder clientConfig(ClientConfig clientConfig); |
| 107 | + |
| 108 | + Builder clientChannelFactory(ClientChannelFactory clientChannelFactory); |
| 109 | + |
| 110 | + Builder sessionConfig(SessionConfig sessionConfig); |
| 111 | + |
| 112 | + Builder scheduler(ScheduledExecutorService scheduler); |
| 113 | + |
| 114 | + Builder allocator(BufferAllocator allocator); |
| 115 | + |
| 116 | + BarrageSessionFactoryConfig build(); |
| 117 | + } |
| 118 | +} |
0 commit comments