Skip to content

Instantly share code, notes, and snippets.

View dragonsinth's full-sized avatar

Scott Blum dragonsinth

  • FullStory
  • Atlanta, GA
View GitHub Profile
@dragonsinth
dragonsinth / PinningTest.java
Created October 29, 2015 21:46
Demonstration of weak mapping class objects
package test;
import com.google.common.collect.MapMaker;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentMap;
@dragonsinth
dragonsinth / errgroup_withcontext.go
Created February 5, 2020 03:53
Using errgroup.WithContext() in Golang server handlers
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"sync/atomic"
"time"
@dragonsinth
dragonsinth / getfriends_select.go
Created February 14, 2020 21:50
GetFriends using select
func GetFriends(ctx context.Context, user int64) (map[string]*User, error) {
g, ctx := errgroup.WithContext(ctx)
friendIds := make(chan int64)
// Produce
g.Go(func() error {
defer close(friendIds)
for it := GetFriendIds(user); ; {
if id, err := it.Next(ctx); err != nil {
if err == io.EOF {
select {
case <-ctx.Done():
return ctx.Err()
case friendIds <- id:
}
func GetFriends(ctx context.Context, user int64) (map[string]*User, error) {
g, ctx := errgroup.WithContext(ctx)
friendIds := make(chan int64)
// Produce
g.Go(func() error {
defer close(friendIds)
for it := GetFriendIds(user); ; {
if id, err := it.Next(ctx); err != nil {
if err == io.EOF {
func GetFriends(ctx context.Context, user int64) (map[string]*User, error) {
friendIds := make(chan int64)
// Produce
go func() {
defer close(friendIds)
for it := GetFriendIds(user); ; {
if id, err := it.Next(ctx); err != nil {
if err == io.EOF {
break
func GetFriends(ctx context.Context, user int64) (map[string]*User, error) {
// Produce
var friendIds []int64
for it := GetFriendIds(user); ; {
if id, err := it.Next(ctx); err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("GetFriendIds %d: %w", user, err)
} else {
@dragonsinth
dragonsinth / gcp-kube.go
Last active August 1, 2024 13:47
Connect to Google Kubernetes with GCP credentials and pure Golang
package main
import (
"context"
"encoding/base64"
"flag"
"fmt"
"log"
container "google.golang.org/api/container/v1beta1"
type node struct {
value interface{} // the value of the event (nil until ready)
next *node // the next node in the stream (nil until ready)
ready chan struct{} // a channel whose closure marks readiness
}
promise := eventStream.Subscribe()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-promise.Ready():
v, nextPromise := promise.Next()
if v == nil {
return nil
}