Skip to content

Commit

Permalink
refactor: optimize the way to obtain plugin configurations (halo-dev#135
Browse files Browse the repository at this point in the history
)

### What this PR does?
/kind improvement

优化插件配置的获取方式

2.17.0 已经对配置获取增加了缓存因此不必在需要插件手动处理

```release-note
None
```
  • Loading branch information
guqing authored and ruibaby committed Aug 8, 2024
1 parent 59f6d2c commit ccd1224
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 82 deletions.
26 changes: 2 additions & 24 deletions src/main/java/run/halo/comment/widget/DefaultCommentWidget.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package run.halo.comment.widget;

import java.util.Properties;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -29,7 +28,6 @@ public class DefaultCommentWidget implements CommentWidget {
static final PropertyPlaceholderHelper PROPERTY_PLACEHOLDER_HELPER = new PropertyPlaceholderHelper("${", "}");

private final PluginWrapper pluginWrapper;
private final SettingFetcher settingFetcher;
private final SettingConfigGetter settingConfigGetter;

@Override
Expand Down Expand Up @@ -65,15 +63,13 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
properties.setProperty("name", nameAttribute.getValue());
properties.setProperty("domId", domIdFrom(group, kindAttribute.getValue(), nameAttribute.getValue()));

var basicConfig = settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class)
.orElse(new BasicConfig());
var basicConfig = settingConfigGetter.getBasicConfig().blockOptional().orElseThrow();
properties.setProperty("size", String.valueOf(basicConfig.getSize()));
properties.setProperty("replySize", String.valueOf(basicConfig.getReplySize()));
properties.setProperty("withReplies", String.valueOf(basicConfig.isWithReplies()));
properties.setProperty("withReplySize", String.valueOf(basicConfig.getWithReplySize()));

var avatarConfig = settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class)
.orElse(new AvatarConfig());
var avatarConfig = settingConfigGetter.getAvatarConfig().blockOptional().orElseThrow();
properties.setProperty("useAvatarProvider", String.valueOf(avatarConfig.isEnable()));
properties.setProperty("avatarProvider", String.valueOf(avatarConfig.getProvider()));
properties.setProperty("avatarProviderMirror", String.valueOf(avatarConfig.getProviderMirror()));
Expand Down Expand Up @@ -111,24 +107,6 @@ private String commentHtml(IAttribute groupAttribute, IAttribute kindAttribute,
""", properties);
}

@Data
private static class BasicConfig {
public static final String GROUP = "basic";
private int size;
private int replySize;
private boolean withReplies;
private int withReplySize;
}

@Data
private static class AvatarConfig {
public static final String GROUP = "avatar";
private boolean enable;
private String provider;
private String providerMirror;
private String policy;
}

private String domIdFrom(String group, String kind, String name) {
Assert.notNull(name, "The name must not be null.");
Assert.notNull(kind, "The kind must not be null.");
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/run/halo/comment/widget/SettingConfigGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@

public interface SettingConfigGetter {

/**
* Never {@link Mono#empty()}.
*/
Mono<BasicConfig> getBasicConfig();

/**
* Never {@link Mono#empty()}.
*/
Mono<AvatarConfig> getAvatarConfig();

/**
* Never {@link Mono#empty()}.
*/
Mono<SecurityConfig> getSecurityConfig();

@Data
@Accessors(chain = true)
class SecurityConfig {
public static final String GROUP = "security";

@Getter(onMethod_ = @NonNull)
private CaptchaConfig captcha = CaptchaConfig.empty();

Expand Down Expand Up @@ -46,4 +61,22 @@ public static CaptchaConfig empty() {
return new CaptchaConfig();
}
}

@Data
class BasicConfig {
public static final String GROUP = "basic";
private int size;
private int replySize;
private boolean withReplies;
private int withReplySize;
}

@Data
class AvatarConfig {
public static final String GROUP = "avatar";
private boolean enable;
private String provider;
private String providerMirror;
private String policy;
}
}
68 changes: 11 additions & 57 deletions src/main/java/run/halo/comment/widget/SettingConfigGetterImpl.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,30 @@
package run.halo.comment.widget;

import static run.halo.app.extension.index.query.QueryFactory.equal;

import java.util.function.Function;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.DefaultExtensionMatcher;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.controller.Controller;
import run.halo.app.extension.controller.ControllerBuilder;
import run.halo.app.extension.controller.Reconciler;
import run.halo.app.extension.router.selector.FieldSelector;
import run.halo.app.plugin.ReactiveSettingFetcher;

@Component
@RequiredArgsConstructor
public class SettingConfigGetterImpl implements SettingConfigGetter {
private final ReactiveSettingFetcher settingFetcher;
private final SettingConfigCache settingConfigCache;

@Override
public Mono<SecurityConfig> getSecurityConfig() {
return settingConfigCache.get("security",
key -> settingFetcher.fetch("security", SecurityConfig.class)
.defaultIfEmpty(SecurityConfig.empty())
);
public Mono<BasicConfig> getBasicConfig() {
return settingFetcher.fetch(BasicConfig.GROUP, BasicConfig.class)
.defaultIfEmpty(new BasicConfig());
}

interface SettingConfigCache {
<T> Mono<T> get(String key, Function<String, Mono<T>> loader);
@Override
public Mono<AvatarConfig> getAvatarConfig() {
return settingFetcher.fetch(AvatarConfig.GROUP, AvatarConfig.class)
.defaultIfEmpty(new AvatarConfig());
}

@Component
@RequiredArgsConstructor
static class SettingConfigCacheImpl implements Reconciler<Reconciler.Request>, SettingConfigCache {
private static final String CONFIG_NAME = "plugin-comment-widget-configmap";

private final Cache<String, Object> cache = CacheBuilder.newBuilder()
.maximumSize(10)
.build();

private final ExtensionClient client;

@SuppressWarnings("unchecked")
public <T> Mono<T> get(String key, Function<String, Mono<T>> loader) {
return Mono.justOrEmpty(cache.getIfPresent(key))
.switchIfEmpty(loader.apply(key).doOnNext(value -> cache.put(key, value)))
.map(object -> (T) object);
}

@Override
public Result reconcile(Request request) {
cache.invalidateAll();
return Result.doNotRetry();
}

@Override
public Controller setupWith(ControllerBuilder builder) {
var extension = new ConfigMap();
var extensionMatcher = DefaultExtensionMatcher.builder(client, extension.groupVersionKind())
.fieldSelector(FieldSelector.of(equal("metadata.name", CONFIG_NAME)))
.build();
return builder
.extension(extension)
.syncAllOnStart(false)
.onAddMatcher(extensionMatcher)
.onUpdateMatcher(extensionMatcher)
.build();
}
@Override
public Mono<SecurityConfig> getSecurityConfig() {
return settingFetcher.fetch(SecurityConfig.GROUP, SecurityConfig.class)
.defaultIfEmpty(SecurityConfig.empty());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
"store.halo.run/app-id": "app-YXyaD"
spec:
enabled: true
requires: ">=2.15.0"
requires: ">=2.17.0"
author:
name: Halo
website: https://github.com/halo-dev
Expand Down

0 comments on commit ccd1224

Please sign in to comment.