Skip to content
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

Added TwitterClient.stopFilteredStream() variant with a timeout #406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/io/github/redouane59/twitter/ITwitterClientV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.github.redouane59.twitter.dto.user.UserList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public interface ITwitterClientV2 {
Expand Down Expand Up @@ -172,10 +173,20 @@ public interface ITwitterClientV2 {
*/
Future<Response> startFilteredStream(IAPIEventListener listener, int backfillMinutes);

/**
* Stops the filtered stream with the result of the startFilteredStream. It'll wait a maximum of timeout
* before giving up and returning false. If timeout isn't hit, it'll close the socket opened.
*
* @param responseFuture Future<Response> given by startFilteredStream
* @param timeout long How long to wait
* @param unit TimeUnit Units for timeout
*/
boolean stopFilteredStream(Future<Response> response, long timeout, TimeUnit unit);

/**
* Stops the filtered stream with the result of the startFilteredStream. It'll close the socket opened.
*
* @param response Future<Response> given by startFilteredStream
* @param responseFuture Future<Response> given by startFilteredStream
*/
boolean stopFilteredStream(Future<Response> response);

Expand Down Expand Up @@ -641,4 +652,4 @@ public interface ITwitterClientV2 {
boolean deleteTweet(String tweetId);


}
}
22 changes: 18 additions & 4 deletions src/main/java/io/github/redouane59/twitter/TwitterClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -1044,20 +1046,32 @@ public Future<Response> startFilteredStream(IAPIEventListener listener, int back
}

@Override
public boolean stopFilteredStream(Future<Response> response) {
public boolean stopFilteredStream(Future<Response> responseFuture, long timeout, TimeUnit unit) {
try {
if (response.get() == null) {
Response response;
if (timeout > 0 && unit != null) {
response = responseFuture.get(timeout, unit);
} else {
response = responseFuture.get();
}

if (response == null) {
return false;
}
response.get().getStream().close();
response.getStream().close();
return true;
} catch (IOException | InterruptedException | ExecutionException e) {
} catch (IOException | InterruptedException | ExecutionException | TimeoutException e) {
LOGGER.error("Couldn't stopFilteredstream ", e);
Thread.currentThread().interrupt();
}
return false;
}

@Override
public boolean stopFilteredStream(Future<Response> responseFuture) {
return stopFilteredStream(responseFuture, 0, null);
}

@Override
public List<StreamRule> retrieveFilteredStreamRules() {
String url = urlHelper.getFilteredStreamRulesUrl();
Expand Down