-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pubsub source v2 uses non-streaming pull
- Loading branch information
Showing
5 changed files
with
83 additions
and
176 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
39 changes: 39 additions & 0 deletions
39
modules/gcp/src/main/scala/common-streams-extensions/v2/FutureInterop.scala
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,39 @@ | ||
/* | ||
* Copyright (c) 2023-present Snowplow Analytics Ltd. All rights reserved. | ||
* | ||
* This program is licensed to you under the Snowplow Community License Version 1.0, | ||
* and you may not use this file except in compliance with the Snowplow Community License Version 1.0. | ||
* You may obtain a copy of the Snowplow Community License Version 1.0 at https://docs.snowplow.io/community-license-1.0 | ||
*/ | ||
package com.snowplowanalytics.snowplow.sources.pubsub.v2 | ||
|
||
import cats.effect.Async | ||
import cats.implicits._ | ||
import com.google.api.core.{ApiFuture, ApiFutureCallback, ApiFutures} | ||
import com.google.common.util.concurrent.MoreExecutors | ||
|
||
object FutureInterop { | ||
def fromFuture[F[_]: Async, A](fut: ApiFuture[A]): F[A] = | ||
Async[F] | ||
.async[A] { cb => | ||
val cancel = Async[F].delay { | ||
fut.cancel(false) | ||
}.void | ||
Async[F].delay { | ||
addCallback(fut, cb) | ||
Some(cancel) | ||
} | ||
} | ||
|
||
def fromFuture_[F[_]: Async, A](fut: ApiFuture[A]): F[Unit] = | ||
fromFuture(fut).void | ||
|
||
private def addCallback[A](fut: ApiFuture[A], cb: Either[Throwable, A] => Unit): Unit = { | ||
val apiFutureCallback = new ApiFutureCallback[A] { | ||
def onFailure(t: Throwable): Unit = cb(Left(t)) | ||
def onSuccess(result: A): Unit = cb(Right(result)) | ||
} | ||
ApiFutures.addCallback(fut, apiFutureCallback, MoreExecutors.directExecutor) | ||
} | ||
|
||
} |
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