chore: update the waiting time for publishing post (#4814)

#### What type of PR is this?
/kind improvement
/area core
/milestone 2.11.x

#### What this PR does / why we need it:
修改发布文章的等待时间以防止因数据库执行延迟较高导致的错误提示

最大等待时间为:`100ms * 2 ^ (retryNum - 1)` = `25600ms`
总共需等待时间为:`100ms * (2 ^ retryNum - 1)` = `31900ms` = `31.9 s`

#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/4819/head
guqing 2023-11-02 17:16:05 +08:00 committed by GitHub
parent 57b24261e7
commit b8755ff3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuil
import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.enums.ParameterIn;
import java.time.Duration; import java.time.Duration;
import java.util.Objects; import java.util.Objects;
import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.fn.builders.schema.Builder; import org.springdoc.core.fn.builders.schema.Builder;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder; import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
@ -42,9 +42,10 @@ import run.halo.app.extension.router.QueryParamBuildUtil;
*/ */
@Slf4j @Slf4j
@Component @Component
@AllArgsConstructor @RequiredArgsConstructor
public class PostEndpoint implements CustomEndpoint { public class PostEndpoint implements CustomEndpoint {
private int maxAttemptsWaitForPublish = 10;
private final PostService postService; private final PostService postService;
private final ReactiveExtensionClient client; private final ReactiveExtensionClient client;
@ -243,7 +244,7 @@ public class PostEndpoint implements CustomEndpoint {
}) })
.switchIfEmpty(Mono.error( .switchIfEmpty(Mono.error(
() -> new RetryException("Retry to check post publish status")))) () -> new RetryException("Retry to check post publish status"))))
.retryWhen(Retry.backoff(5, Duration.ofMillis(100)) .retryWhen(Retry.backoff(maxAttemptsWaitForPublish, Duration.ofMillis(100))
.filter(t -> t instanceof RetryException)); .filter(t -> t instanceof RetryException));
} }
@ -278,4 +279,11 @@ public class PostEndpoint implements CustomEndpoint {
return postService.listPost(postQuery) return postService.listPost(postQuery)
.flatMap(listedPosts -> ServerResponse.ok().bodyValue(listedPosts)); .flatMap(listedPosts -> ServerResponse.ok().bodyValue(listedPosts));
} }
/**
* Convenient for testing, to avoid waiting too long for post published when testing.
*/
public void setMaxAttemptsWaitForPublish(int maxAttempts) {
this.maxAttemptsWaitForPublish = maxAttempts;
}
} }

View File

@ -50,6 +50,7 @@ class PostEndpointTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
postEndpoint.setMaxAttemptsWaitForPublish(3);
webTestClient = WebTestClient webTestClient = WebTestClient
.bindToRouterFunction(postEndpoint.endpoint()) .bindToRouterFunction(postEndpoint.endpoint())
.build(); .build();
@ -170,7 +171,7 @@ class PostEndpointTest {
.is5xxServerError(); .is5xxServerError();
// Verify WebClient retry behavior // Verify WebClient retry behavior
verify(client, times(7)).get(eq(Post.class), eq("post-1")); verify(client, times(5)).get(eq(Post.class), eq("post-1"));
verify(client).update(any(Post.class)); verify(client).update(any(Post.class));
} }