Skip to content

Commit

Permalink
Merge pull request #1217 from guardian/LIVE-6447-create-a-connection-…
Browse files Browse the repository at this point in the history
…for-each-message

LIVE-6447 Create a connection for the processing of each SQS message
  • Loading branch information
waisingyiu authored Apr 17, 2024
2 parents 8d86dd7 + b5eb60e commit 53a4efc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import java.time.Instant
import java.util.UUID
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import com.gu.notifications.worker.delivery.fcm.FcmFirebase
import scala.util.Try

class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Option[String], val metricNs: String) extends SenderRequestHandler[FcmClient] {

Expand All @@ -37,8 +39,10 @@ class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Opt
override implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec)
override implicit val timer: Timer[IO] = IO.timer(ec)

override val deliveryService: IO[Fcm[IO]] =
FcmClient(config.fcmConfig, firebaseAppName).fold(e => IO.raiseError(e), c => IO.delay(new Fcm(c)))
val fcmFirebase: Try[FcmFirebase] = FcmFirebase(config.fcmConfig, firebaseAppName)
override val deliveryService: IO[Fcm[IO]] =
fcmFirebase.fold(e => IO.raiseError(e), c => IO.delay(new Fcm(FcmClient(c))))

override val maxConcurrency = config.concurrencyForIndividualSend
override val batchConcurrency = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ trait DeliveryClient {
type Payload <: DeliveryPayload
type BatchSuccess <: BatchDeliverySuccess

def close(): Unit
def sendNotification(notificationId: UUID, token: String, payload: Payload, dryRun: Boolean)
(onComplete: Either[DeliveryException, Success] => Unit)
(implicit ece: ExecutionContextExecutor): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ApnsClient(private val underlying: PushyApnsClient, val config: ApnsConfig
"DeviceTokenNotForTopic"
)

def close(): Unit = underlying.close().get

def payloadBuilder: Notification => Option[ApnsPayload] = apnsPayloadBuilder.apply _

def sendNotification(notificationId: UUID, token: String, payload: Payload, dryRun: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp,
ErrorCode.PERMISSION_DENIED
)

def close(): Unit = firebaseApp.delete()

private final val FCM_URL: String = s"https://fcm.googleapis.com/v1/projects/${projectId}/messages:send";

private val fcmTransport: FcmTransport = new FcmTransportJdkImpl(credential, FCM_URL, jsonFactory)
Expand Down Expand Up @@ -176,8 +174,10 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp,

}

object FcmClient {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmClient] =
case class FcmFirebase(firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp, config: FcmConfig, projectId: String, credential: GoogleCredentials, jsonFactory: JsonFactory)

object FcmFirebase {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmFirebase] =
Try {
val credential = GoogleCredentials.fromStream(new ByteArrayInputStream(config.serviceAccountKey.getBytes))
val firebaseOptions: FirebaseOptions = FirebaseOptions.builder()
Expand All @@ -193,10 +193,15 @@ object FcmClient {
case s: ServiceAccountCredentials => s.getProjectId()
case _ => ""
}
new FcmClient(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
new FcmFirebase(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
}
}

object FcmClient {
def apply(firebase: FcmFirebase): FcmClient =
new FcmClient(firebase.firebaseMessaging, firebase.firebaseApp, firebase.config, firebase.projectId, firebase.credential, firebase.jsonFactory)
}

object FirebaseHelpers {

implicit class RichApiFuture[T](val af: ApiFuture[T]) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ import java.net.http.HttpRequest
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpResponse
import org.slf4j.{Logger, LoggerFactory}

trait FcmTransport {
def sendAsync(token: String, payload: FcmPayload, dryRun: Boolean): Future[String]
}

class FcmTransportJdkImpl(credential: GoogleCredentials, url: String, jsonFactory: JsonFactory) extends FcmTransport {

implicit val logger: Logger = LoggerFactory.getLogger(this.getClass)

private val httpClient: HttpClient = HttpClient.newHttpClient()

logger.info("HttpClient is instantiated")

private val charSet = StandardCharsets.UTF_8

private val mediaType = "application/json; charset=UTF-8"
Expand Down

0 comments on commit 53a4efc

Please sign in to comment.