Giter VIP home page Giter VIP logo

gfc-util's Introduction

gfc-util Maven Central Build Status Coverage Status Join the chat at https://gitter.im/gilt/gfc

A library that contains a few scala utility classes. Part of the Gilt Foundation Classes.

Getting gfc-util

The latest version is 0.2.1, which is cross-built against Scala 2.10.x, 2.11.x and 2.12.x.

If you're using SBT, add the following line to your build file:

libraryDependencies += "com.gilt" %% "gfc-util" % "0.2.1"

For Maven and other build tools, you can visit search.maven.org. (This search will also list other available libraries from the gilt fundation classes.)

Contents and Example Usage

com.gilt.gfc.util.Retry

Allows a retry of a potentially failing function with or without an exponentially growing wait period:

// some potentially failing function
def inputNumber: Int = ???

// Retry the function up to 10 times or until it succeeds
val number: Int = retry(10)(inputNumber)
// some potentially failing function
def readFile: Seq[String] = ???

// Retry the function up to 10 times until it succeeds, with an exponential backoff,
// starting at 10 ms and doubling each iteration until it reaches 1 second, i.e.
// 10ms, 20ms, 40ms, 80ms, 160ms, 320ms, 640ms, 1s, 1s, 1s
val contents: Seq[String] = retryWithExponentialDelay(maxRetryTimes = 10,
                                                      maxRetryTimeout = 5 minutes fromNow,
                                                      initialDelay = 10 millis,
                                                      maxDelay = 1 second,
                                                      exponentFactor = 2)
                                                     (readFile)

Allows a retry of a function I => O via a function I => Either[I, O]:

val arr: Array[Boolean] = Array.fill(5)(false)

// Up to 10 times, set a random array index to 'true' and 
// return "success" when all array elements are 'true', or throw 'TooManyRetries'
Retry.retryFold(maxRetryTimes = 10)(arr){ a: Array[Boolean] =>
  if (a.forall(identity)) {
    Right("success")
  } else {
    a(scala.util.Random.nextInt(a.length)) = true
    Left(a)
  }
}
// Set a random array index to 'true' and return "success" when all array elements are 'true'.
// Retry this up to 10 times until it succeeds, with an exponential backoff,
// starting at 10 ms and doubling each iteration until it reaches 1 second, i.e.
// 10ms, 20ms, 40ms, 80ms, 160ms, 320ms, 640ms, 1s, 1s, 1s, or throw 'TooManyRetries'
def func(a: Array[Boolean]): Either[Array[Boolean], String] = {
  if (a.forall(identity)) {
    Right("success")
  } else {
    a(scala.util.Random.nextInt(a.length)) = true
    Left(a)
  }
}

val result: String = retryFoldWithExponentialDelay(maxRetryTimes = 10,
                                                   maxRetryTimeout = 5 minutes fromNow,
                                                   initialDelay = 10 millis,
                                                   maxDelay = 1 second,
                                                   exponentFactor = 2)
                                                  (arr)
                                                  (func)

com.gilt.gfc.util.RateLimiter and com.gilt.gfc.util.ThreadSafeRateLimiter

RateLimiter can be used to rate-limit calls to a work function, e.g. a function that writes to a db. ThreadSafeRateLimiter is a thread safe version of RateLimiter, that synchronizes calls to the limit function.

val rateLimiter = new ThreadSafeRateLimiter(100) // Limit to 100 calls/second

def writeObject(obj: DBObject) = rateLimiter.limit {
  db.insert(obj)
}

com.gilt.gfc.util.SingletonCache

A cache for objects that need their lifecycle to be managed and can't be just throw-away, e.g. for when ConcurrentHashMap.putIfAbsent(new Something()) may result in a Something instance that need to be closed if another thread's putIfAbsent was successful.

val inputStreamCache = new SingletonCache[File]

val file = new File("foo")

val is: InputStream = inputStreamCache(file) {
  new FileInputStream(file)
}

com.gilt.gfc.util.Throwables

Utility to unwind nested Throwable stacks

val e1 = new Exception()
val e2 = new Exception(e1)
val e3 = new Exception(e3)

val t: Throwable = Throwables.rootCause(e3)

t should be theSameInstanceAs e1

License

Copyright 2018 Gilt Groupe, Inc.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

gfc-util's People

Contributors

chavdarch avatar petergram avatar

Watchers

James Cloos avatar nashid avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.