add peer-pubkeys endpoint

add LocalChannel class
This commit is contained in:
Carsten Otto
2021-11-12 13:24:04 +01:00
parent 367119c8aa
commit 2ec2ecce9a
13 changed files with 185 additions and 61 deletions

View File

@@ -4,53 +4,49 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.model.Channel;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Component
public class ChannelService {
private static final int MAXIMUM_SIZE = 500;
private static final int CACHE_EXPIRY_MINUTES = 5;
private static final int CACHE_EXPIRY_MINUTES = 1;
private final GrpcChannels grpcChannels;
private final LoadingCache<Pubkey, List<ChannelId>> channelsWithPeerCache;
private final LoadingCache<String, Set<LocalChannel>> channelsCache;
public ChannelService(GrpcChannels grpcChannels) {
this.grpcChannels = grpcChannels;
CacheLoader<Pubkey, List<ChannelId>> loader = new CacheLoader<>() {
channelsCache = initializeChannelsCache();
}
public Set<LocalChannel> getOpenChannels() {
return channelsCache.getUnchecked("");
}
public Set<LocalChannel> getOpenChannelsWith(Pubkey peer) {
return getOpenChannels().stream()
.filter(c -> peer.equals(c.getRemotePubkey()))
.collect(Collectors.toSet());
}
private LoadingCache<String, Set<LocalChannel>> initializeChannelsCache() {
CacheLoader<String, Set<LocalChannel>> loader = new CacheLoader<>() {
@Nonnull
@Override
public List<ChannelId> load(@Nonnull Pubkey peer) {
return getOpenChannelsWithWithoutCache(peer);
public Set<LocalChannel> load(@Nonnull String ignored) {
return grpcChannels.getChannels();
}
};
channelsWithPeerCache = CacheBuilder.newBuilder()
return CacheBuilder.newBuilder()
.expireAfterWrite(CACHE_EXPIRY_MINUTES, TimeUnit.MINUTES)
.maximumSize(MAXIMUM_SIZE)
.build(loader);
}
public List<ChannelId> getOpenChannelsWith(Pubkey peer) {
return channelsWithPeerCache.getUnchecked(peer);
}
public List<ChannelId> getOpenChannelsWith(Node peer) {
return getOpenChannelsWith(peer.pubkey());
}
public List<ChannelId> getOpenChannelsWithWithoutCache(Pubkey peer) {
return grpcChannels.getChannels().stream()
.filter(c -> c.getPubkeys().contains(peer))
.map(Channel::getId)
.sorted()
.collect(Collectors.toList());
}
}