Skip to content

Instantly share code, notes, and snippets.

@bradclawsie
Created January 16, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradclawsie/497046559deea191963c85999dd9b347 to your computer and use it in GitHub Desktop.
Save bradclawsie/497046559deea191963c85999dd9b347 to your computer and use it in GitHub Desktop.
channel-pool.go
package main
import (
"errors"
"log"
"math/rand"
"sync"
"time"
)
// T can be imagined to be a much more interesting type that has
// complex state that is expensive to be initialized. Here it is
// trivial for illustrative purposes.
type T struct {
A, B, C int
}
// Ts is the channel "pool" of *T instances that are pre-allocated for use.
var Ts chan *T
// Count the number of extra *T instances we could not use from the channel "pool".
var allocated, deleted int
// retireT will attempt to put a *T back into the pool or just delete it.
func retireT(t *T) error {
if t == nil {
return errors.New("retire nil")
}
t.A, t.B = 0, 0
select {
case Ts <- t:
default:
deleted = deleted + 1
t = nil
}
return nil
}
// getT will get a *T from the pool or just allocate a new one.
func getT() (*T, error) {
select {
case t := <-Ts:
return t, nil
default:
allocated = allocated + 1
return &T{}, nil
}
}
func main() {
Ts = make(chan *T, 50)
var wg sync.WaitGroup
wg.Add(2)
go func() {
for i := 0; i < 100; i++ {
_, _ = getT()
log.Printf("+")
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
wg.Done()
}()
go func() {
for i := 0; i < 100; i++ {
_ = retireT(&T{A: i, B: i, C: i})
log.Printf("-")
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
}
wg.Done()
}()
wg.Wait()
log.Printf("total allocated %d total deleted %d", allocated, deleted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment