|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.crt; |
| 17 | + |
| 18 | +import software.amazon.awssdk.annotations.SdkPreviewApi; |
| 19 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 20 | +import software.amazon.awssdk.crt.io.SocketOptions; |
| 21 | +import software.amazon.awssdk.utils.Validate; |
| 22 | + |
| 23 | +/** |
| 24 | + * Configuration that defines socket options for all connections established by |
| 25 | + * the {@link SocketOptionsConfiguration}. |
| 26 | + * |
| 27 | + * <b>NOTE:</b> This is a Preview API and is subject to change so it should not be used in production. |
| 28 | + */ |
| 29 | +@SdkPublicApi |
| 30 | +@SdkPreviewApi |
| 31 | +public final class SocketOptionsConfiguration { |
| 32 | + |
| 33 | + private final SocketOptions.SocketDomain domain; |
| 34 | + private final SocketOptions.SocketType type; |
| 35 | + private final int connectTimeoutMs; |
| 36 | + private final int keepAliveIntervalSecs; |
| 37 | + private final int keepAliveTimeoutSecs; |
| 38 | + |
| 39 | + private SocketOptionsConfiguration(DefaultSocketOptionsConfigurationBuilder builder) { |
| 40 | + this.domain = Validate.paramNotNull(builder.domain, |
| 41 | + "domain"); |
| 42 | + this.type = Validate.paramNotNull(builder.type, |
| 43 | + "type"); |
| 44 | + this.connectTimeoutMs = Validate.isPositive(builder.connectTimeoutMs, |
| 45 | + "connectTimeoutMs"); |
| 46 | + this.keepAliveIntervalSecs = Validate.isNotNegative(builder.keepAliveIntervalSecs, |
| 47 | + "keepAliveIntervalSecs"); |
| 48 | + this.keepAliveTimeoutSecs = Validate.isNotNegative(builder.keepAliveTimeoutSecs, |
| 49 | + "keepAliveTimeoutSecs"); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return socket domain |
| 54 | + */ |
| 55 | + public SocketOptions.SocketDomain domain() { |
| 56 | + return domain; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @return socket type |
| 61 | + */ |
| 62 | + public SocketOptions.SocketType type() { |
| 63 | + return type; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return number of milliseconds before a connection will be considered timed out |
| 68 | + */ |
| 69 | + public int connectTimeoutMs() { |
| 70 | + return connectTimeoutMs; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @return number of seconds between TCP keepalive packets being sent to the peer |
| 75 | + */ |
| 76 | + public int keepAliveIntervalSecs() { |
| 77 | + return keepAliveIntervalSecs; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return number of seconds to wait for a keepalive response before considering the connection timed out |
| 82 | + */ |
| 83 | + public int keepAliveTimeoutSecs() { |
| 84 | + return keepAliveTimeoutSecs; |
| 85 | + } |
| 86 | + |
| 87 | + public static Builder builder() { |
| 88 | + return new DefaultSocketOptionsConfigurationBuilder(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A builder for {@link SocketOptionsConfiguration}. |
| 93 | + * |
| 94 | + * <p>All implementations of this interface are mutable and not thread safe.</p> |
| 95 | + */ |
| 96 | + public interface Builder { |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets the socket domain |
| 100 | + * @param domain socket domain |
| 101 | + * @return Builder |
| 102 | + */ |
| 103 | + Builder domain(SocketOptions.SocketDomain domain); |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets the socket type |
| 107 | + * @param type socket type |
| 108 | + * @return Builder |
| 109 | + */ |
| 110 | + Builder type(SocketOptions.SocketType type); |
| 111 | + |
| 112 | + /** |
| 113 | + * Sets the number of milliseconds before a connection will be considered timed out |
| 114 | + * @param connectTimeoutMs number of milliseconds before a connection will be considered timed out |
| 115 | + * @return Builder |
| 116 | + */ |
| 117 | + Builder connectTimeoutMs(int connectTimeoutMs); |
| 118 | + |
| 119 | + /** |
| 120 | + * Sets the number of seconds between TCP keepalive packets being sent to the peer |
| 121 | + * @param keepAliveIntervalSecs number of seconds between TCP keepalive packets being sent to the peer |
| 122 | + * @return Builder |
| 123 | + */ |
| 124 | + Builder keepAliveIntervalSecs(int keepAliveIntervalSecs); |
| 125 | + |
| 126 | + /** |
| 127 | + * Sets the number of seconds to wait for a keepalive response before considering the connection timed out |
| 128 | + * @param keepAliveTimeoutSecs number of seconds to wait for a keepalive response before considering the connection timed out |
| 129 | + * @return Builder |
| 130 | + */ |
| 131 | + Builder keepAliveTimeoutSecs(int keepAliveTimeoutSecs); |
| 132 | + |
| 133 | + SocketOptionsConfiguration build(); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * An SDK-internal implementation of {@link Builder}. |
| 138 | + */ |
| 139 | + private static final class DefaultSocketOptionsConfigurationBuilder implements Builder { |
| 140 | + |
| 141 | + private SocketOptions.SocketDomain domain = SocketOptions.SocketDomain.IPv6; |
| 142 | + private SocketOptions.SocketType type = SocketOptions.SocketType.STREAM; |
| 143 | + private int connectTimeoutMs = 3000; |
| 144 | + private int keepAliveIntervalSecs = 0; |
| 145 | + private int keepAliveTimeoutSecs = 0; |
| 146 | + |
| 147 | + private DefaultSocketOptionsConfigurationBuilder() { |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sets the socket domain |
| 152 | + * Default: {@link SocketOptions.SocketDomain#IPv6} |
| 153 | + * @param domain socket domain |
| 154 | + * @return Builder |
| 155 | + */ |
| 156 | + @Override |
| 157 | + public Builder domain(SocketOptions.SocketDomain domain) { |
| 158 | + this.domain = domain; |
| 159 | + return this; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets the socket type |
| 164 | + * Default: {@link SocketOptions.SocketType#STREAM} |
| 165 | + * @param type socket type |
| 166 | + * @return Builder |
| 167 | + */ |
| 168 | + @Override |
| 169 | + public Builder type(SocketOptions.SocketType type) { |
| 170 | + this.type = type; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Sets the number of milliseconds before a connection will be considered timed out |
| 176 | + * Default: 3000ms |
| 177 | + * @param connectTimeoutMs number of milliseconds before a connection will be considered timed out |
| 178 | + * @return Builder |
| 179 | + */ |
| 180 | + @Override |
| 181 | + public Builder connectTimeoutMs(int connectTimeoutMs) { |
| 182 | + this.connectTimeoutMs = connectTimeoutMs; |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Sets the number of seconds between TCP keepalive packets being sent to the peer |
| 188 | + * Default: 0 disables keepalive |
| 189 | + * @param keepAliveIntervalSecs number of seconds between TCP keepalive packets being sent to the peer |
| 190 | + * @return Builder |
| 191 | + */ |
| 192 | + @Override |
| 193 | + public Builder keepAliveIntervalSecs(int keepAliveIntervalSecs) { |
| 194 | + this.keepAliveIntervalSecs = keepAliveIntervalSecs; |
| 195 | + return this; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Sets the number of seconds to wait for a keepalive response before considering the connection timed out |
| 200 | + * Default: 0 disables keepalive |
| 201 | + * @param keepAliveTimeoutSecs number of seconds to wait for a keepalive response before considering the connection timed out |
| 202 | + * @return Builder |
| 203 | + */ |
| 204 | + @Override |
| 205 | + public Builder keepAliveTimeoutSecs(int keepAliveTimeoutSecs) { |
| 206 | + this.keepAliveTimeoutSecs = keepAliveTimeoutSecs; |
| 207 | + return this; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public SocketOptionsConfiguration build() { |
| 212 | + return new SocketOptionsConfiguration(this); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments