-
Notifications
You must be signed in to change notification settings - Fork 224
feat: Maven plugin progress upload for model descriptor #2580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cristianonicolai
wants to merge
1
commit into
TimefoldAI:main
Choose a base branch
from
cristianonicolai:feat/upload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...maven-plugin/src/main/java/ai/timefold/solver/tools/maven/http/CountingBodyPublisher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package ai.timefold.solver.tools.maven.http; | ||
|
|
||
| import java.net.http.HttpRequest.BodyPublisher; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.Flow.Subscriber; | ||
| import java.util.concurrent.Flow.Subscription; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| /** | ||
| * Wraps another {@link BodyPublisher} and counts the bytes handed over to the HTTP client's write pipeline, so that | ||
| * the thread waiting for the response can tell whether the request body is still moving. | ||
| * <p> | ||
| * The count is <em>not</em> the number of bytes acknowledged by the peer, it is the number of bytes the client has | ||
| * accepted for writing. The overshoot is bounded by the socket send buffer plus, for HTTP/2, the peer's flow control | ||
| * window, because the client only requests more buffers from this publisher once the previous ones have been written | ||
| * and the window allows more. On a slow connection that overshoot is a few hundred kilobytes at most and roughly | ||
| * constant, which makes the count a usable approximation of upload progress - and a slow connection is the only | ||
| * situation in which the count is ever reported. | ||
| */ | ||
| public final class CountingBodyPublisher implements BodyPublisher { | ||
|
|
||
| private final BodyPublisher delegate; | ||
| private final AtomicLong transferredBytes = new AtomicLong(); | ||
|
|
||
| public CountingBodyPublisher(BodyPublisher delegate) { | ||
| this.delegate = Objects.requireNonNull(delegate, "delegate"); | ||
| } | ||
|
|
||
| /** | ||
| * Delegated unchanged on purpose. Returning anything else (for example -1) makes the client fall back to chunked | ||
| * transfer encoding instead of sending a Content-Length header, which changes the request on the wire. | ||
| */ | ||
| @Override | ||
| public long contentLength() { | ||
| return delegate.contentLength(); | ||
| } | ||
|
|
||
| /** | ||
| * Safe to call from any thread, in particular from the thread waiting for the response. | ||
| */ | ||
| public long getTransferredBytes() { | ||
| return transferredBytes.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public void subscribe(Subscriber<? super ByteBuffer> subscriber) { | ||
| Objects.requireNonNull(subscriber, "subscriber"); | ||
| // The client may subscribe more than once for a single logical request, for example when it resends the | ||
| // request after a redirect. Every subscription restarts the body from the beginning, so the counter has to | ||
| // restart too, otherwise it runs past contentLength() and progress exceeds 100%. | ||
| transferredBytes.set(0); | ||
| delegate.subscribe(new CountingSubscriber(subscriber)); | ||
| } | ||
|
|
||
| private final class CountingSubscriber implements Subscriber<ByteBuffer> { | ||
|
|
||
| private final Subscriber<? super ByteBuffer> downstream; | ||
|
|
||
| private CountingSubscriber(Subscriber<? super ByteBuffer> downstream) { | ||
| this.downstream = downstream; | ||
| } | ||
|
|
||
| @Override | ||
| public void onSubscribe(Subscription subscription) { | ||
| downstream.onSubscribe(subscription); | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(ByteBuffer item) { | ||
| // remaining() has to be read before forwarding: the downstream subscriber consumes the buffer, which | ||
| // advances its position, so remaining() is 0 by the time onNext returns. The counter is only updated | ||
| // after the forward, so a buffer rejected by a throwing subscriber is not counted. | ||
| int count = item.remaining(); | ||
| downstream.onNext(item); | ||
| transferredBytes.addAndGet(count); | ||
| } | ||
|
|
||
| @Override | ||
| public void onError(Throwable throwable) { | ||
| downstream.onError(throwable); | ||
| } | ||
|
|
||
| @Override | ||
| public void onComplete() { | ||
| downstream.onComplete(); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.