Skip to content

Instantly share code, notes, and snippets.

Adam Warski adamw

Block or report user

Report or block adamw

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Example4.scala
import zio._
import zio.clock.Clock
import zio.duration._
class Example4(emailService: EmailService) {
// Schedule an email to each address, but send at most 10 in parallel
def welcomeUsers(emails: List[String]): RIO[Clock, Unit] =
ZIO.foreachParN_(10)(emails)(sendWelcomeEmail)
// Try to send a welcome email at most 5 times,
View Example3.kt
import kotlinx.coroutines.future.await
import java.util.concurrent.CompletableFuture
internal class Example3(private val database: Database) {
suspend fun activateUser(userId: Long?): Boolean {
val user = database.findUser(userId).await()
if (user != null && !user.isActive) {
database.activateUser(userId).await()
return true
} else {
View Example2.java
class Example2 {
private final Database database;
Example2(Database database) {
this.database = database;
}
boolean activateUser(Long userId) {
User user = database.findUser(userId);
if (user != null && !user.isActive()) {
View Example1.java
import java.util.concurrent.CompletableFuture;
class Example1 {
private final Database database;
Example1(Database database) {
this.database = database;
}
CompletableFuture<Boolean> activateUser(Long userId) {
View x.scala
package sttp.tapir.example
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.model.StatusCode
import sttp.tapir.docs.openapi._
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.json.circe._
object Example extends App {
View x.scala
object CirceAnnotationCodecs {
import io.circe.{Decoder, Encoder}
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.tapir.json.circe._
@JsonCodec
case class Foo(x: String)
View Example1Test.scala
class Example1Test extends AnyFlatSpec with Matchers with BeforeAndAfterAll {
private var postgres: EmbeddedPostgres = _
private var transactor: Transactor[IO] = _
implicit private val ioContextShift: ContextShift[IO] =
IO.contextShift(ExecutionContext.global)
override protected def beforeAll(): Unit = {
super.beforeAll()
postgres = EmbeddedPostgres.builder().start()
transactor = Transactor.fromDriverManager[IO](
View Example1.scala
import java.util.UUID
import doobie._
import doobie.implicits._
import doobie.postgres.implicits._
class Points(dao: Dao) {
def increase(userId: UUID): ConnectionIO[Unit] =
for {
current <- dao.currentPoints(userId)
View useDirectives2.scala
import io.prometheus.client.{Gauge, Histogram}
def reportRequestMetrics(request: HttpRequest): Try[RouteResult] => Unit = {
requestsInProgress.inc()
val requestTimer = requestLatencySeconds.startTimer()
result => {
requestsInProgress.dec()
result match {
case Success(Complete(_)) => requestTimer.observeDuration()
View useDirective1.scala
def timeRequest(request: HttpRequest): Try[RouteResult] => Unit = {
val start = System.currentTimeMillis()
{
case Success(Complete(resp)) =>
val d = System.currentTimeMillis() - start
logger.info(s"[${resp.status.intValue()}] ${request.method.name} " +
s"${request.uri} took: ${d}ms")
case Success(Rejected(_)) =>
case Failure(_) =>
You can’t perform that action at this time.