util: Fix procedure syntax
This is reviewed at https://github.com/twitter/util/pull/215
Problem
procedure syntax is deprecated since Scala 2.13 https://github.com/scala/scala/commit/b5799032760d5aaab5739a41816fb74653bcc82c
Solution
rewrite by scalafix (version 0.6.0-M5) https://scalacenter.github.io/scalafix/docs/rules/ProcedureSyntax
scalafix --rules ProcedureSyntaxResult
There is no semantic changes. source and binary compatible.
Signed-off-by: Yufan Gong yufang@twitter.com
Differential Revision: https://phabricator.twitter.biz/D177477
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
Twitter Util
A bunch of idiomatic, small, general purpose tools.
See the Scaladoc here.
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
The
masterbranch of this repository contains the latest stable release of Util, and weekly snapshots are published to thedevelopbranch. In general pull requests should be submitted againstdevelop. See CONTRIBUTING.md for more details about how to contribute.Using in your project
An example SBT dependency string for the
util-collectiontools would look like this:Units
Time
Space
Futures
A Non-actor re-implementation of Scala Futures.
Collections
LruMap
The LruMap is an LRU with a maximum size passed in. If the map is full it expires items in FIFO order. Reading a value will move an item to the top of the stack.
Object Pool
The pool order is FIFO.
A pool of constants
A pool of dynamically created objects
Here is a pool of even-number generators. It stores 4 numbers at a time:
It checks the health when you successfully reserve an object (i.e., when the Future yields).
Hashing
util-hashingis a collection of hash functions and hashing distributors (eg. ketama).To use one of the available hash functions:
Available hash functions are:
To use
KetamaDistributor:Logging
util-loggingis a small wrapper around Java’s built-in logging to make it more Scala-friendly.Using
To access logging, you can usually just use:
This creates a
Loggerobject that uses the current class or object’s package name as the logging node, so class “com.example.foo.Lamp” will log to nodecom.example.foo(generally showing “foo” as the name in the logfile). You can also get a logger explicitly by name:Logger objects wrap everything useful from
java.util.logging.Logger, as well as adding some convenience methods:Each of the log levels (from “fatal” to “trace”) has these two convenience methods. You may also use
logdirectly:An advantage to using sprintf (“%s”, etc) conversion, as opposed to:
is that Java & Scala perform string concatenation at runtime, even if nothing will be logged because the log file isn’t writing debug messages right now. With
sprintfparameters, the arguments are just bundled up and passed directly to the logging level before formatting. If no log message would be written to any file or device, then no formatting is done and the arguments are thrown away. That makes it very inexpensive to include verbose debug logging which can be turned off without recompiling and re-deploying.If you prefer, there are also variants that take lazily evaluated parameters, and only evaluate them if logging is active at that level:
The logging classes are done as an extension to the
java.util.loggingAPI, and so you can use the Java interface directly, if you want to. Each of the Java classes (Logger, Handler, Formatter) is just wrapped by a Scala class.Configuring
In the Java style, log nodes are in a tree, with the root node being “” (the empty string). If a node has a filter level set, only log messages of that priority or higher are passed up to the parent. Handlers are attached to nodes for sending log messages to files or logging services, and may have formatters attached to them.
Logging levels are, in priority order of highest to lowest:
FATAL- the server is about to exitCRITICAL- an event occurred that is bad enough to warrant paging someoneERROR- a user-visible error occurred (though it may be limited in scope)WARNING- a coder may want to be notified, but the error was probably not user-visibleINFO- normal informational messagesDEBUG- coder-level debugging informationTRACE- intensive debugging informationEach node may also optionally choose to not pass messages up to the parent node.
The
LoggerFactorybuilder is used to configure individual log nodes, by filling in fields and calling theapplymethod. For example, to configure the root logger to filter atINFOlevel and write to a file:As many
LoggerFactorys can be configured as you want, so you can attach to several nodes if you like. To remove all previous configurations, use:Handlers
QueueingHandlerQueues log records and publishes them in another thread thereby enabling “async logging”.
ConsoleHandlerLogs to the console.
FileHandlerLogs to a file, with an optional file rotation policy. The policies are:
Policy.Never- always use the same logfile (default)Policy.Hourly- roll to a new logfile at the top of every hourPolicy.Daily- roll to a new logfile at midnight every nightPolicy.Weekly(n)- roll to a new logfile at midnight on day N (0 = Sunday)Policy.SigHup- reopen the logfile on SIGHUP (for logrotate and similar services)When a logfile is rolled, the current logfile is renamed to have the date (and hour, if rolling hourly) attached, and a new one is started. So, for example,
test.logmay becometest-20080425.log, andtest.logwill be reopened as a new file.SyslogHandlerLog to a syslog server, by host and port.
ScribeHandlerLog to a scribe server, by host, port, and category. Buffering and backoff can also be configured: You can specify how long to collect log lines before sending them in a single burst, the maximum burst size, and how long to backoff if the server seems to be offline.
ThrottledHandlerWraps another handler, tracking (and squelching) duplicate messages. If you use a format string like
"Error %d at %s", the log messages will be de-duped based on the format string, even if they have different parameters.Formatters
Handlers usually have a formatter attached to them, and these formatters generally just add a prefix containing the date, log level, and logger name.
FormatterA standard log prefix like
"ERR [20080315-18:39:05.033] jobs: ", which can be configured to truncate log lines to a certain length, limit the lines of an exception stack trace, and use a special time zone.You can override the format string used to generate the prefix, also.
BareFormatterConfigNo prefix at all. May be useful for logging info destined for scripts.
SyslogFormatterConfigA formatter required by the syslog protocol, with configurable syslog priority and date format.
Future interrupts
Method
raiseonFuture(def raise(cause: Throwable)) raises the interrupt described bycauseto the producer of thisFuture. Interrupt handlers are installed on aPromiseusingsetInterruptHandler, which takes a partial function: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.
Time and Duration
Like arithmetic on doubles,
TimeandDurationarithmetic is now free of overflows. Instead, they overflow toTopandBottomvalues, which are analogous to positive and negative infinity.Since the resolution of
Time.nowhas been reduced (and is also more expensive due to its use of system time), a newStopwatchAPI has been introduced in order to calculate durations of time.It’s used simply:
which is read by applying
elapsed: