-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbf6693
commit 5bc83f2
Showing
13 changed files
with
522 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
74 changes: 74 additions & 0 deletions
74
...e/src/main/java/org/elasticsearch/xpack/inference/external/http/sender/TimedListener.java
This file contains 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,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.external.http.sender; | ||
|
||
import org.elasticsearch.ElasticsearchStatusException; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.support.ListenerTimeouts; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.core.Nullable; | ||
import org.elasticsearch.core.TimeValue; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static org.elasticsearch.xpack.inference.InferencePlugin.UTILITY_THREAD_POOL_NAME; | ||
|
||
/** | ||
* Provides a way to set a timeout on the listener. If the time expires, the original listener's | ||
* {@link ActionListener#onFailure(Exception)} is called with an error indicating there was a timeout. | ||
* | ||
* @param <Response> the type of the value that is passed in {@link ActionListener#onResponse(Object)} | ||
*/ | ||
public class TimedListener<Response> { | ||
|
||
private final ActionListener<Response> listenerWithTimeout; | ||
private final AtomicBoolean finished = new AtomicBoolean(); | ||
|
||
public TimedListener(@Nullable TimeValue timeout, ActionListener<Response> listener, ThreadPool threadPool) { | ||
listenerWithTimeout = getListener(Objects.requireNonNull(listener), timeout, Objects.requireNonNull(threadPool)); | ||
} | ||
|
||
private ActionListener<Response> getListener( | ||
ActionListener<Response> origListener, | ||
@Nullable TimeValue timeout, | ||
ThreadPool threadPool | ||
) { | ||
ActionListener<Response> notificationListener = ActionListener.wrap(result -> { | ||
finished.set(true); | ||
origListener.onResponse(result); | ||
}, e -> { | ||
finished.set(true); | ||
origListener.onFailure(e); | ||
}); | ||
|
||
if (timeout == null) { | ||
return notificationListener; | ||
} | ||
|
||
return ListenerTimeouts.wrapWithTimeout( | ||
threadPool, | ||
timeout, | ||
threadPool.executor(UTILITY_THREAD_POOL_NAME), | ||
notificationListener, | ||
(ignored) -> notificationListener.onFailure( | ||
new ElasticsearchStatusException(Strings.format("Request timed out after [%s]", timeout), RestStatus.REQUEST_TIMEOUT) | ||
) | ||
); | ||
} | ||
|
||
public boolean hasCompleted() { | ||
return finished.get(); | ||
} | ||
|
||
public ActionListener<Response> getListener() { | ||
return listenerWithTimeout; | ||
} | ||
} |
Oops, something went wrong.