• This repository has been archived on 28/Dec/2020
  • Stars
    star
    1,214
  • Rank 37,030 (Top 0.8 %)
  • Language
    Go
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cheat sheet for some of the common concurrent flows in Go

coop

Build Status

Note: This package became obsolete. I started it when I was learning Go a couple of years ago. I see so many better ways to implement them all now, so don't keep using this package as an ultimate reference.

coop contains some of the most common concurrent program flows I personally use in Go. I'm suggesting you to use this package as a snippets reference/cheat sheet instead of a library. The functionally provided in this package can be obtained in many different ways, and frankly with more performant implementations depending on the type of your problem.

coop contains implementations for the following flows:

coop.At(time, fn)

Runs fn at t, returns a boolean channel that will receive a message after fn returns. The following example, prints "Hello World" in a minute and blocks the goroutine its running in until fn is completed.

done := coop.At(time.Now().Add(time.Minute), func() {
    fmt.Println("Hello world")
})
<-done // wait for fn to be done

coop.Until(time, duration, fn)

Runs fn once in every provided duration until t, returns a boolean channel that will receive a message after fn returns. The following example prints "Hello world" every minute until tomorrow, and blocks the goroutine its running in until the job is completed.

done := coop.Until(time.Now().Add(24*time.Hour), time.Minute, func() {
    fmt.Println("Hello world")
})
<-done

coop.After(duration, fn)

Runs fn after duration, returns a boolean channel that will receive a message after fn returns. The following example prints "Hello world" after a second and blocks until fn is completed.

done := coop.After(time.Second, func() {
    fmt.Println("Hello world")
})
<-done

coop.Every(duration, fn)

Runs fn once in every duration, and never stops. The following example will print "Hello World" once in every second.

coop.Every(time.Second, func() {
    fmt.Println("Hello world")
})

coop.Timeout(duration, fn)

Runs fn, and cancels the running job if timeout is exceeded. The following example will timeout and fn will return immediately ("Hello world will not printed"), the value read from the done channel will be false if timeout occurs, true if fn is completed.

done := coop.Timeout(time.Second, func() {
    time.Sleep(time.Hour)
    fmt.Println("Hello world")
})
<-done // will return false, because timeout occurred

coop.All(fns...)

Runs the list of fns concurrently, returns a boolean channel that will receive a message after all of the fns are completed. The following example will start 4 printing jobs concurrently and wait until all of them are completed.

printFn := func() {
    fmt.Println("Hello world")
}
<-coop.All(printFn, printFn, printFn, printFn)

coop.AllWithThrottle(num, fns...)

Similar to coop.All, but with limiting. Runs the list of fns concurrently, but at most num fns at a time. Returns a boolean channel that will receive a message after all of the fns are completed. The following example will start 3 printing jobs immediately, and run the left out one once the first 3 is completed. It will block the goroutine until all 4 are finished.

printFn := func() {
    fmt.Println("Hello world")
}
<-coop.AllWithThrottle(3, printFn, printFn, printFn, printFn)

coop.Replicate(n, fn)

Runs fn n time concurrently, returns a boolean channel that indicates all runs are completed. The following example prints "Hello world" 5 times, and waits for all printing jobs are finished.

<-coop.Replicate(5, func() {
    fmt.Println("Hello world")
})

License

Copyright 2014 Google Inc. All Rights Reserved.

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 limitations under the License. Analytics

More Repositories

1

hey

HTTP load generator, ApacheBench (ab) replacement
Go
17,071
star
2

boom

HTTP(S) load generator, ApacheBench (ab) replacement, written in Go
Go
4,745
star
3

statik

Embed files into a Go executable
Go
3,649
star
4

go-hardware

A directory of hardware related libs, tools, and tutorials for Go
Go
1,329
star
5

gom

A visual interface to work with runtime profiling data for Go
Go
1,295
star
6

gotest

go test with colors
Go
1,242
star
7

autopprof

Pprof made easy at development time for Go
Go
738
star
8

drive

Pull or push Google Drive files
Go
465
star
9

openai-go

Go client libraries for OpenAI
Go
443
star
10

go-test-trace

Go test with tracing.
Go
381
star
11

govalidate

Validates your Go installation and dependencies.
Go
335
star
12

portmidi

Go bindings for libportmidi
Go
282
star
13

globalconf

Persist flag values into an ini file
Go
275
star
14

magicmime

Go bindings for libmagic to detect MIME types
Go
216
star
15

ticktock

A cron job scheduler for Go
Go
209
star
16

go2xcode

Go package to Xcode project generator
Go
177
star
17

fake-it-til-you-make-it

A program that demonstrates that GitHub contribution graph can be cheated
Go
147
star
18

golambda

AWS Lambda Go functions made easy...
Go
137
star
19

launchpad

Talk to your Novation Launchpad in Go
Go
130
star
20

pprof-merge

Merge multiple pprof profile files into a single file
Go
118
star
21

events2prom

A metric collection daemon allows you to configure aggregations in runtime
Go
107
star
22

goproxy-s3

Go proxy that serves from S3
Go
104
star
23

go-sql-driver-spanner

Google Cloud Spanner driver for Go
Go
89
star
24

awesome-spanner

A curated list of awesome Google Cloud Spanner references, tools, libraries and more.
82
star
25

coinsensus

Decentralized distributed systems consensus
62
star
26

opencensus-grpc-demo

Export metrics and traces from gRPC servers and clients
Java
59
star
27

spannerz

Google Cloud Spanner Query Planner Visualizer
Go
59
star
28

aws-containers

My personal mind map of container platforms and tools on AWS
53
star
29

gowiki

Personal fork of Go wiki
47
star
30

command

CLI subcommands for Go
Go
44
star
31

trace

Drafts of a Dapper-style tracing client for Go
Go
44
star
32

obs-luts

LUT files to use with OBS
43
star
33

littlebits

littleBits Go module -- an io.Reader and io.Writer to read from and write to a circuit
Go
40
star
34

dfanout

A simple HTTP/2 fanout server
Go
38
star
35

goutil

Various Go utility packages.
Go
37
star
36

gce-metadata

GCE metadata command line tool
Go
32
star
37

myko

A simple attribution engine.
Go
28
star
38

log-to-xray

Write log entries, get X-Ray traces.
Go
27
star
39

pprof-upload

Upload pprof output to continuous profiler
Go
26
star
40

keys

Let your golang programs store and access passwords in a secure way
Go
26
star
41

go-numa

NUMA bindings for Go, requires libnuma.
Go
24
star
42

httpaa

Where http.HaandleFunc and alike lives.
Go
23
star
43

drummachine

Akai MPD18 replica written in Go, runs on mobile devices
Go
21
star
44

go-xcode

Reference Xcode project to build iOS apps with Go
Makefile
18
star
45

firmata

Firmata client for Go
Arduino
18
star
46

k8s-helloworld-leaderelection

Go
16
star
47

audio

Go
15
star
48

bubblr

Android client for App Engine Channels API
Java
15
star
49

rrqueue

Simple priority queues and round robin consumer for golang
Go
15
star
50

cosmicbackgroundmusic

Go
13
star
51

mysql-args-anonymizer

Go
13
star
52

sensors

Experiments with Android NDK sensors and Go
Go
13
star
53

redfail

Simple command to colorize the stderr of a target program
Go
12
star
54

opentelemetry-metric-go

Experiments, no real code.
Go
12
star
55

node-plussignin

Google+ Sign In middleware for Connect.
JavaScript
12
star
56

gcp-connectivitytests

Google Cloud Connectivity Tests
Go
11
star
57

perf2cloudprofiler

Upload perf output to Google Cloud profiler from anywhere
Go
10
star
58

drivefuse

Google Drive for Linux, BSD and MacOSX
C
10
star
59

grpcutil

Go
9
star
60

spanner-dotfiles

A set of personal aliases to use with Google Cloud Spanner.
7
star
61

talks

7
star
62

go-brillo

Go development notes for the Brillo targets. (External contributors who are willing to contribute are welcomed to comment.)
7
star
63

go-benchmarks

6
star
64

al

OpenAL bindings for Go (work-in-progress)
6
star
65

janis

The missing components of Android SDK
Java
6
star
66

pizza

rakyll.pizza
Go
6
star
67

rakyll.github.io

HTML
5
star
68

blinker

A tiny Raspberry Pi program controlled by a Go mobile app
Go
5
star
69

pubsub

Go
5
star
70

rakyll

Public profile...
5
star
71

refutil

Personal collection of reflection utils for Go
Go
4
star
72

benchmarks

Interesting Go benchmarks to watch
Go
4
star
73

experimental

Experimental Go packages
Go
3
star
74

node-ini

Simple ini reader for Node.js
JavaScript
3
star
75

snippetr

Extract code snippets from source code.
JavaScript
3
star
76

go0

$ docker run -i -t rakyll/go0
Dockerfile
2
star
77

ecs-metadata-proxy

Proxy to debug metadata server in ECS tasks
Go
2
star
78

aestaticdeploy

Takes a static directory and serves it on Google App Engine
Go
2
star
79

zigot

En extensible protocol for cloud storage, against of proprietary APIs.
2
star
80

appdatapreferences-localstorage

Syncs your local storage to Google Drive's appdata folder.
JavaScript
2
star
81

skeleton

skeletons for go projects
Go
1
star
82

travis-build

Go
1
star
83

trace2

Go
1
star
84

allgo

All Go is a repo to be used to test godoc command and godoc.org
Go
1
star
85

dockerfiles

All images are ARMv7
1
star
86

reviews

Catch all repo for reviewing things.
1
star