Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require 'open3'
# What's the network interface name?
def ifaces
`iw dev`.lines.select { |l| l["Interface "] }.map { |l| l.split(/\s+/).last }
end
# First iface
@cp16net
cp16net / Preferences.sublime-settings
Created July 9, 2015 17:14
~/Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings
{
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"ensure_newline_at_eof_on_save": true,
"rulers": [80],
"save_on_focus_lost": true
}
(defn chain
"Composes functions, left to right, over the value `x`, returning a promise containing
the result. When composing, either `x` or the returned values may be values which can
be converted to a promise, causing the composition to be paused.
The returned promise will only be realized once all functions have been applied and their
return values realized.
(chain 1 inc #(future (inc %) => 3
@(chain (future 1) inc inc) => 3
public class SlidingTimeThreshold {
private final long[] samples;
private final long windowMillis;
private int index;
public SlidingTimeThreshold(final long window, final TimeUnit timeUnit, final int threshold) {
this.windowMillis = timeUnit.toMillis(window);
this.samples = new long[threshold];
}
@viktorklang
viktorklang / InterruptibleCancellableFuture.scala
Last active June 1, 2020 13:45
Interruptible-Cancellable-scala.concurrent.Future
/*
Copyright 2018 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@daschl
daschl / gist:db9fcc9d2b932115b679
Last active August 26, 2020 23:17
Draft: Writing Code for Production

Writing Resilient Reactive Applications

This guide is a first draft (that will end up in the official docs) on writing resilient code for production with the Couchbase Java SDK. At the end, the reader will be able to write code that withstands bugs, latency issues or anything else that can make their application fail.

Note that lots of concepts can be applied for both synchronous and asynchronous access. When necessary, both patterns are discussed separately. Also, the focus is on database interaction, but if you are using RxJava as part of your stack you can apply most of the principles there as well (and should!).

RxJava 101 Recap: Cold and Hot Observables

When working with Observables, it is important to understand the difference between cold and hot. Cold Observables will start to emit events once a Observer subscribes, and will do it "fresh" for each Observer. Hot Observables instead are starting to emit data as soon as it becomes available, and will return the same (or parts of the same)

@letmaik
letmaik / .travis.yml
Last active December 15, 2021 23:10
Deploy snapshots to Sonatype after Travis CI build
language: java
env:
global:
- SONATYPE_USERNAME=yourusername
- secure: "your encrypted SONATYPE_PASSWORD=pass"
after_success:
- python addServer.py
- mvn clean deploy --settings ~/.m2/mySettings.xml
@viktorklang
viktorklang / ExecutionContextExecutorServiceBridge.scala
Last active June 27, 2022 11:38
Turning an ExecutionContext to an ExecutorService (or rather and ExecutorService AND an ExecutionContext) using Scala 2.10+
/*
Copyright 2013 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
@viktorklang
viktorklang / Future-retry.scala
Last active July 23, 2023 23:48
Asynchronous retry for Future in Scala
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import akka.pattern.after
import akka.actor.Scheduler
/**
* Given an operation that produces a T, returns a Future containing the result of T, unless an exception is thrown,
* in which case the operation will be retried after _delay_ time, if there are more possible retries, which is configured through
* the _retries_ parameter. If the operation does not succeed and there is no retries left, the resulting Future will contain the last failure.
@aslakknutsen
aslakknutsen / start_testing_java8_today.asciidoc
Last active October 11, 2023 20:07
Example of how to use both JDK 7 and JDK 8 in one build.

JDK 8 Released

Most of us won’t be able to use/deploy JDK 8 in production for a looong time. But that shouldn’t stop us from using it, right?

It should be possible to sneak in JDK 8 in the back way, the same way we snuck in Groovy and other libraries we wanted to use.

The Test Suite to the rescue

The Maven compiler plugin run in two separate lifecycles, compile and testCompile. Those can be configured separately.