Skip to content

Register executors as components. #4288

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

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions firebase-annotations/firebase-annotations.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ java {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Werror"
}

dependencies {
implementation 'javax.inject:javax.inject:1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for long running tasks including disk IO, heavy CPU
* computations.
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Background {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for tasks that can block for long periods of time, e.g network
* IO.
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Blocking {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* An executor/coroutine dispatcher for lightweight tasks that never block(on IO or other tasks).
*/
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface Lightweight {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.annotations.concurrent;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/** An executor/coroutine dispatcher for work that must run on the UI thread. */
@Qualifier
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface UiThread {}
1 change: 1 addition & 0 deletions firebase-common/firebase-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
implementation project(':firebase-components')
implementation 'com.google.android.gms:play-services-basement:18.1.0'
implementation "com.google.android.gms:play-services-tasks:18.0.1"
implementation 'androidx.concurrent:concurrent-futures:1.1.0'

// FirebaseApp references storage, so storage needs to be on classpath when dokka runs.
javadocClasspath project(path: ':firebase-storage')
Expand Down
1 change: 1 addition & 0 deletions firebase-common/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

implementation project(':firebase-annotations')
implementation project(':firebase-common')
implementation project(':firebase-components')
implementation 'androidx.annotation:annotation:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ import android.content.Context
import androidx.annotation.Keep
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.annotations.concurrent.Background
import com.google.firebase.annotations.concurrent.Blocking
import com.google.firebase.annotations.concurrent.Lightweight
import com.google.firebase.annotations.concurrent.UiThread
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.components.Dependency
import com.google.firebase.components.Qualified
import com.google.firebase.platforminfo.LibraryVersionComponent
import java.util.concurrent.Executor
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher

/**
* Single access point to all firebase SDKs from Kotlin.
Expand All @@ -40,11 +49,11 @@ fun Firebase.initialize(context: Context): FirebaseApp? = FirebaseApp.initialize

/** Initializes and returns a FirebaseApp. */
fun Firebase.initialize(context: Context, options: FirebaseOptions): FirebaseApp =
FirebaseApp.initializeApp(context, options)
FirebaseApp.initializeApp(context, options)

/** Initializes and returns a FirebaseApp. */
fun Firebase.initialize(context: Context, options: FirebaseOptions, name: String): FirebaseApp =
FirebaseApp.initializeApp(context, options, name)
FirebaseApp.initializeApp(context, options, name)

/** Returns options of default FirebaseApp */
val Firebase.options: FirebaseOptions
Expand All @@ -57,6 +66,27 @@ internal const val LIBRARY_NAME: String = "fire-core-ktx"
class FirebaseCommonKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> {
return listOf(
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME),
coroutineDispatcher<Background>(),
coroutineDispatcher<Lightweight>(),
coroutineDispatcher<Blocking>(),
coroutineDispatcher<UiThread>()
)
}
}

private inline fun <reified T : Annotation> coroutineDispatcher(): Component<CoroutineDispatcher> =
Component.builder(
Qualified.qualified(T::class.java, CoroutineDispatcher::class.java)
).add(
Dependency.required(
Qualified.qualified(
T::class.java,
Executor::class.java
)
)
).factory { c ->
c.get(
Qualified.qualified(T::class.java, Executor::class.java)
).asCoroutineDispatcher()
}.build()
23 changes: 3 additions & 20 deletions firebase-common/src/main/java/com/google/firebase/FirebaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.GuardedBy;
Expand All @@ -47,6 +45,7 @@
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.ComponentRuntime;
import com.google.firebase.components.Lazy;
import com.google.firebase.concurrent.ExecutorsRegistrar;
import com.google.firebase.events.Publisher;
import com.google.firebase.heartbeatinfo.DefaultHeartBeatController;
import com.google.firebase.inject.Provider;
Expand All @@ -59,7 +58,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -97,16 +95,10 @@ public class FirebaseApp {

private static final Object LOCK = new Object();

private static final Executor UI_EXECUTOR = new UiExecutor();

/** A map of (name, FirebaseApp) instances. */
@GuardedBy("LOCK")
static final Map<String, FirebaseApp> INSTANCES = new ArrayMap<>();

private static final String FIREBASE_ANDROID = "fire-android";
private static final String FIREBASE_COMMON = "fire-core";
private static final String KOTLIN = "kotlin";

private final Context applicationContext;
private final String name;
private final FirebaseOptions options;
Expand Down Expand Up @@ -427,9 +419,10 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o

FirebaseTrace.pushTrace("Runtime");
componentRuntime =
ComponentRuntime.builder(UI_EXECUTOR)
ComponentRuntime.builder(com.google.firebase.concurrent.UiExecutor.INSTANCE)
.addLazyComponentRegistrars(registrars)
.addComponentRegistrar(new FirebaseCommonRegistrar())
.addComponentRegistrar(new ExecutorsRegistrar())
.addComponent(Component.of(applicationContext, Context.class))
.addComponent(Component.of(this, FirebaseApp.class))
.addComponent(Component.of(options, FirebaseOptions.class))
Expand Down Expand Up @@ -712,14 +705,4 @@ public void onBackgroundStateChanged(boolean background) {
}
}
}

private static class UiExecutor implements Executor {

private static final Handler HANDLER = new Handler(Looper.getMainLooper());

@Override
public void execute(@NonNull Runnable command) {
HANDLER.post(command);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.concurrent;

import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

class CustomThreadFactory implements ThreadFactory {
private static final ThreadFactory DEFAULT = Executors.defaultThreadFactory();
private final AtomicLong threadCount = new AtomicLong();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the counter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is common practice to give different names to threads, i.e. executor-foo-1, executor-foo-2 etc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't see it in the name, thanks!

private final String namePrefix;
private final int priority;

CustomThreadFactory(String namePrefix, int priority) {
this.namePrefix = namePrefix;
this.priority = priority;
}

@Override
public Thread newThread(Runnable r) {
Thread thread = DEFAULT.newThread(r);
thread.setPriority(priority);
thread.setName(
String.format(Locale.ROOT, "%s Thread #%d", namePrefix, threadCount.getAndIncrement()));
return thread;
}
}
Loading