All Projects → twitter → Util

twitter / Util

Licence: apache-2.0
Wonderful reusable code from Twitter

Programming Languages

scala
5932 projects
java
68154 projects - #9 most used programming language
Starlark
911 projects
Mako
254 projects
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to Util

Sdcv
Stars: ✭ 171 (-93.13%)
Mutual labels:  utility
Paco
Small utility library for coroutine-driven asynchronous generic programming in Python 3.4+
Stars: ✭ 198 (-92.04%)
Mutual labels:  utility
Discord Image Downloader Go
A simple tool which downloads pictures posted in discord channels of your choice to a local folder.
Stars: ✭ 210 (-91.56%)
Mutual labels:  utility
Hcl Picker
🎨 Colorpicker for data
Stars: ✭ 178 (-92.85%)
Mutual labels:  utility
Code Notes
Tool to summarise all code annotation like TODO or FIXME
Stars: ✭ 192 (-92.28%)
Mutual labels:  utility
Dot
Yet another management tool for dotfiles
Stars: ✭ 199 (-92%)
Mutual labels:  utility
Fastenum
The world fastest enum utilities for C#/.NET
Stars: ✭ 165 (-93.37%)
Mutual labels:  utility
Facon
Tiny utility (272B) to create DOM elements with manner.
Stars: ✭ 212 (-91.48%)
Mutual labels:  utility
Ts Toolbelt
ts-toolbelt is the largest, and most tested type library available right now, featuring +200 utilities. Our type collection packages some of the most advanced mapped types, conditional types, and recursive types on the market.
Stars: ✭ 3,099 (+24.56%)
Mutual labels:  utility
Lxrunoffline
A full-featured utility for managing Windows Subsystem for Linux (WSL)
Stars: ✭ 3,005 (+20.78%)
Mutual labels:  utility
Vue Breakpoints
🍬 🙈 Vue.js utility component to show and hide components based on breakpoints
Stars: ✭ 179 (-92.81%)
Mutual labels:  utility
Draxt
draxt.js – NodeList/jQuery-like package for File System (node.js)
Stars: ✭ 192 (-92.28%)
Mutual labels:  utility
Util
A collection of useful utility functions
Stars: ✭ 201 (-91.92%)
Mutual labels:  utility
Finatra
Fast, testable, Scala services built on TwitterServer and Finagle
Stars: ✭ 2,126 (-14.55%)
Mutual labels:  finagle
Electron Is
An 'is' utility for Electron which provides a set of handy functions, with a self-descriptive name.
Stars: ✭ 210 (-91.56%)
Mutual labels:  utility
Restyle
Stars: ✭ 171 (-93.13%)
Mutual labels:  utility
Pudl
The Public Utility Data Liberation Project
Stars: ✭ 200 (-91.96%)
Mutual labels:  utility
Synth Shell
Boost your terminal, script by script
Stars: ✭ 217 (-91.28%)
Mutual labels:  utility
Powerkey
Remap your Macbook's power key to Forward Delete
Stars: ✭ 212 (-91.48%)
Mutual labels:  utility
Fwd
🚂 The little forwarder that could
Stars: ✭ 203 (-91.84%)
Mutual labels:  utility

Twitter Util

Build Status Codecov Project status Gitter Maven Central

A bunch of idiomatic, small, general purpose tools.

See the Scaladoc here or check out the user guide.

Status

This project is used in production at Twitter (and many other organizations), and is being actively developed and maintained.

Releases

Releases are done on an approximately monthly schedule. While semver is not followed, the changelogs are detailed and include sections on public API breaks and changes in runtime behavior.

Contributing

We feel that a welcoming community is important and we ask that you follow Twitter's Open Source Code of Conduct in all interactions with the community.

The release branch of this repository contains the latest stable release of Util, and weekly snapshots are published to the develop branch. In general pull requests should be submitted against develop. See CONTRIBUTING.md for more details about how to contribute.

Using in your project

An example SBT dependency string for the util-core library would look like this:

val utilCore = "com.twitter" %% "util-core" % "21.12.0"

Units

Time

import com.twitter.conversions.DurationOps._

val duration1 = 1.second
val duration2 = 2.minutes
duration1.inMillis // => 1000L

Space

import com.twitter.conversions.StorageUnitOps._
val amount = 8.megabytes
amount.inBytes // => 8388608L
amount.inKilobytes // => 8192L

Futures

A Non-actor re-implementation of Scala Futures.

import com.twitter.conversions.DurationOps._
import com.twitter.util.{Await, Future, Promise}

val f = new Promise[Int]
val g = f.map { result => result + 1 }
f.setValue(1)
Await.result(g, 1.second) // => this blocks for the futures result (and eventually returns 2)

// Another option:
g.onSuccess { result =>
  println(result) // => prints "2"
}

// Using for expressions:
val xFuture = Future(1)
val yFuture = Future(2)

for {
  x <- xFuture
  y <- yFuture
} {
  println(x + y) // => prints "3"
}

Future interrupts

Method raise on Future (def raise(cause: Throwable)) raises the interrupt described by cause to the producer of this Future. Interrupt handlers are installed on a Promise using setInterruptHandler, which takes a partial function:

val p = new Promise[T]
p.setInterruptHandler {
  case exc: MyException =>
    // deal with interrupt..
}

Interrupts differ in semantics from cancellation in important ways: there can only be one interrupt handler per promise, and interrupts are only delivered if the promise is not yet complete.

Object Pool

The pool order is FIFO.

A pool of constants

import scala.collection.mutable
import com.twitter.util.{Await, SimplePool}

val queue = new mutable.Queue[Int] ++ List(1, 2, 3)
val pool = new SimplePool(queue)

// Note that the pool returns Futures, it doesn't block on exhaustion.
assert(Await.result(pool.reserve()) == 1)
pool.reserve().onSuccess { item =>
  println(item) // prints "2"
}

A pool of dynamically created objects

Here is a pool of even-number generators. It stores 4 numbers at a time:

import com.twitter.util.{Future, FactoryPool}

val pool = new FactoryPool[Int](4) {
  var count = 0
  def makeItem() = { count += 1; Future(count) }
  def isHealthy(i: Int) = i % 2 == 0
}

It checks the health when you successfully reserve an object (i.e., when the Future yields).

Hashing

util-hashing is a collection of hash functions and hashing distributors (eg. ketama).

To use one of the available hash functions:

import com.twitter.hashing.KeyHasher

KeyHasher.FNV1_32.hashKey("string".getBytes)

Available hash functions are:

FNV1_32
FNV1A_32
FNV1_64
FNV1A_64
KETAMA
CRC32_ITU
HSIEH

To use KetamaDistributor:

import com.twitter.hashing.{KetamaDistributor, KetamaNode, KeyHasher}

val nodes = List(KetamaNode("host:port", 1 /* weight */, "foo" /* handle */))
val distributor = new KetamaDistributor(nodes, 1 /* num reps */)
distributor.nodeForHash("abc".##) // => client

Time and Duration

Like arithmetic on doubles, Time and Duration arithmetic is now free of overflows. Instead, they overflow to Top and Bottom values, which are analogous to positive and negative infinity.

Since the resolution of Time.now has been reduced (and is also more expensive due to its use of system time), a new Stopwatch API has been introduced in order to calculate durations of time.

It's used simply:

import com.twitter.util.{Duration, Stopwatch}
val elapsed: () => Duration = Stopwatch.start()

which is read by applying elapsed:

val duration: Duration = elapsed()

License

Copyright 2010 Twitter, Inc.

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].