Skip to content

Instantly share code, notes, and snippets.

View lestrrat's full-sized avatar

lestrrat lestrrat

View GitHub Profile
@lestrrat
lestrrat / cleanup.pl
Last active December 12, 2017 02:19
Delete dangling TCP Load Balancers on GCP Created By Kubernetes Services
use strict;
use JSON ();
my $src = qx(gcloud compute forwarding-rules list --format json);
my $json = JSON->new;
my $rules = $json->decode( $src );
for my $rule (@$rules) {
print "forwarding-rule $rule->{name}:\n";
if ($rule->{description} =~ m{kubernetes\.io/service-name}) { # TCP (k8s Service with external address)
my ($target_pool) = ($rule->{target} =~ m{/([^/]+)$});
// Wrap the handler with a context that holds the transaction
func withTx(h http.Handler) http.Handler {
return http.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
tx, err := globalDB.BeginTx(r.Context())
if err != nil {
http.Error(...)
return
}
r = r.WithContext(context.WithTx(r.Context(), tx))
h.ServeHTTP(w, r)
@lestrrat
lestrrat / stages.md
Last active May 18, 2022 02:19
Seven Stages of Becoming a Go Programmer
  • stage 1: You believe you can make Go do object oriented programming. You want to do this by using clever struct embedding.
  • stage 2: You believe goroutines will solve all of your problems. You want to use goroutines for anything and everything that you can, who cares if the code becomes a bit more complicated
  • stage 3: You believe that instead of object oriented programming, interfaces will solve all of your problems. You want to define everything in terms of interfaces
  • stage 4: You believe channels will solve all of your problems. You want to do everything from synchronization, returning values, and flow control using channels.
  • stage 5: You now believe Go is not as powerful as people claim it to be. You feel like you're stripped of all of the nice tools and constructs that other languages provide.
  • stage 6: You realize that stages 1~5 were all just your imagination. You just didn't want to accept the Go way. Everything starts to make sense.
  • stage 7: You are now at peace
use strict;
use feature 'say';
sub is_anagram {
my ($s1, $s2) = @_;
my %chars1;
my %chars2;
for my $c (split //, $s1) {
$chars1{$c}++;
@lestrrat
lestrrat / wc.go
Last active September 25, 2016 05:33
おもしろそうだったので自分もwc書いてみたけど、元のCで書かれたwcをあんまり改良するところがなかった…
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
"sync"
func TestParallelBatch(t *testing.T) {
dir, err := ioutil.TempDir("", "leveldb-test-")
if !assert.NoError(t, err, "ioutil.TempDir should succeed") {
return
}
defer os.RemoveAll(dir)
db, err := leveldb.OpenFile(dir, nil)
if !assert.NoError(t, err, "leveldb.OpenFile should succeed") {
return
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"sync"
@lestrrat
lestrrat / bundling.go
Created July 6, 2016 13:57
俺が本当は欲しかったもの
package main
import (
"fmt"
"math/rand"
"time"
"golang.org/x/net/context"
)
@lestrrat
lestrrat / app.go
Created March 1, 2016 09:55
automatically generated by hsup
package app
import (
"net/http"
"strings"
"github.com/gorilla/mux"
"golang.org/x/context"
)