question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FR: Revising the Threading Model and Async APIs

See original GitHub issue

There are several issues in the Admin SDK with respect to how it creates and uses threads:

  1. Most public APIs are asynchronous, and returns Task instances. This tends to make implementing simple use cases unnecessarily complex (e.g. creating a custom token and sending back as a servlet response).
  2. Main thread pool is initialized statically, and not configurable.
  3. ThreadFactory is initialized statically, and not configurable.
  4. Tasks API is visible to user code. A user can kick off arbitrary background tasks using this API, which will get executed on the main thread pool of the SDK, un-regulated.

In order to address these limitations, we can consider the following changes:

  • Expose synchronous/blocking variants of public APIs where it makes sense (most notably at FirebaseAuth).
BlockingFirebaseAuth auth = FirebaseAuth.getBlockingInstance();
String customToken = auth.createCustomToken("user1");
  • Make the thread pool and thread factory configurable via app options.
  • Hide or decommission the Tasks API. We can consider using the ApiFutures from Google Cloud Commons (this is similar to Guava’s ListenableFutures) as a replacement.
FirebaseAuth auth = FirebaseAuth.getInstance();
ApiFuture<String> customTokenFuture = auth.createCustomToken("user1");

// The following should not be supported 
// Tasks.call(new Callable(){...}); 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:32 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
samodadelacommented, Nov 29, 2017

Well… in the end I came up with this:

    SettableApiFuture<String> future = SettableApiFuture.create();
    final Query mDatabase = getDatabaseReference( firebaseProjectDatabase ).child( valueName );

    mDatabase.addListenerForSingleValueEvent( new ValueEventListener() {
        @Override
        public void onDataChange( DataSnapshot snapshot ) {
            String value = "got it";
            future.set( value );
        }

        @Override
        public void onCancelled( DatabaseError error ) {
            future.set( null );
        }
    } );

    String value = null;
    try {
        value = future.get( 10, TimeUnit.SECONDS );
    } catch( InterruptedException e ) {
        log.error( "getValue interrupted: {}", e );
    } catch( ExecutionException e ) {
        log.error( "getValue did not execute: {}", e );
    } catch( TimeoutException e ) {
        log.error( "getValue timed-out: {}", e );
    }
1reaction
pontellocommented, Nov 28, 2017

@samodadela , ApiFuture extends java.util.concurrent.Future.

This article should be enough to learn how to use it.

If you still need extra help, try another articles from here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous APIs in Qt 6 | Multithreaded Programming
Read about the changes to asynchronous APIs in Qt 6 and discover how these updates can improve parallel processing and multithreaded ...
Read more >
Asynchronous API Calls and Idling - The Building Coder
Application registers itself and its commands. · On one command, it kicks off a working thread (or a modeless dialog) and leaves it...
Read more >
Developing and Testing an Asynchronous API with FastAPI ...
This tutorial looks at how to develop and test an asynchronous API with FastAPI, Postgres, pytest, and Docker using Test-driven Development ...
Read more >
Task asynchronous programming model - Microsoft Learn
Asynchrony proves especially valuable for applications that access the UI thread because all UI-related activity usually shares one thread.
Read more >
Chapter 4: Threads & Concurrency
Multi-threading Models ... Describe how the Linux operating system represents threads ... A POSIX standard (IEEE 1003.1c) API for thread creation and.
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found