Skip to content

Commit abd1be8

Browse files
committed
Add ProtocolFeatureStore cache in DriverChannel
No need to pull this information all the time from the channel. Let's cache it when it is populated to the channel and use it.
1 parent ea65be4 commit abd1be8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

core/src/main/java/com/datastax/oss/driver/internal/core/channel/DriverChannel.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public class DriverChannel {
7676
private final ProtocolVersion protocolVersion;
7777
private final AtomicBoolean closing = new AtomicBoolean();
7878
private final AtomicBoolean forceClosing = new AtomicBoolean();
79+
private ProtocolFeatureStore featureStore;
7980

8081
DriverChannel(
8182
EndPoint endPoint,
@@ -146,18 +147,35 @@ public Map<String, List<String>> getOptions() {
146147
return channel.attr(OPTIONS_KEY).get();
147148
}
148149

150+
public ProtocolFeatureStore getSupportedFeatures() {
151+
if (featureStore != null) {
152+
return featureStore;
153+
}
154+
155+
ProtocolFeatureStore fromChannel = ProtocolFeatureStore.loadFromChannel(channel);
156+
if (fromChannel == null) {
157+
return ProtocolFeatureStore.Empty;
158+
}
159+
// Features can't be renegotiated.
160+
// Once features is populated into channel it is enough to update cache and no need to
161+
// invalidate it further.
162+
163+
featureStore = fromChannel;
164+
return featureStore;
165+
}
166+
149167
public int getShardId() {
150-
ConnectionShardingInfo info = ProtocolFeatureStore.loadFromChannel(channel).getShardingInfo();
168+
ConnectionShardingInfo info = getSupportedFeatures().getShardingInfo();
151169
return info != null ? info.shardId : 0;
152170
}
153171

154172
public ShardingInfo getShardingInfo() {
155-
ConnectionShardingInfo info = ProtocolFeatureStore.loadFromChannel(channel).getShardingInfo();
173+
ConnectionShardingInfo info = getSupportedFeatures().getShardingInfo();
156174
return info != null ? info.shardingInfo : null;
157175
}
158176

159177
public LwtInfo getLwtInfo() {
160-
return ProtocolFeatureStore.loadFromChannel(channel).getLwtFeatureInfo();
178+
return getSupportedFeatures().getLwtFeatureInfo();
161179
}
162180

163181
/**

core/src/main/java/com/datastax/oss/driver/internal/core/protocol/ProtocolFeatureStore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class ProtocolFeatureStore {
1414
private final ShardingInfo.ConnectionShardingInfo shardingInfo;
1515
private final TabletInfo tabletInfo;
1616

17+
public static final ProtocolFeatureStore Empty = new ProtocolFeatureStore(null, null, null);
18+
1719
ProtocolFeatureStore(
1820
LwtInfo lwtInfo, ShardingInfo.ConnectionShardingInfo shardingInfo, TabletInfo tabletInfo) {
1921
this.lwtInfo = lwtInfo;

0 commit comments

Comments
 (0)