This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package concurrency | |
import ( | |
"context" | |
"fmt" | |
"golang.org/x/sync/errgroup" | |
) | |
const ( | |
doThingWorkers = 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env true; exec /usr/bin/env go run "$0" "$@" | |
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"path/filepath" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go install github.com/fullstorydev/go/examples/chatterbox/...@latest | |
chatterbox server | |
chatterbox monitor # in another terminal | |
chatterbox client # in another terminal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type LogReplicatedModel struct { | |
mu sync.RWMutex | |
snapshot Model | |
stream eventstream.EventStream | |
} | |
func (m *LogReplicatedModel) ReadAndSubscribe() (Model, eventstream.Promise) { | |
m.mu.RLock() | |
defer m.mu.RUnlock() | |
return m.snapshot, m.stream.Subscribe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for it := eventStream.Subscribe().Iterator(); true; { | |
v, err := it.Next(ctx) | |
if err == eventstream.ErrDone { | |
return nil | |
} else if err != nil { | |
return err | |
} | |
msg := v.(bwamp.Event) | |
// [Send msg to client] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
promise := eventStream.Subscribe() | |
for { | |
select { | |
case <-ctx.Done(): | |
return ctx.Err() | |
case <-promise.Ready(): | |
v, nextPromise := promise.Next() | |
if v == nil { | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/base64" | |
"flag" | |
"fmt" | |
"log" | |
container "google.golang.org/api/container/v1beta1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder