harness-drone/server/worker/pool/context.go
2014-09-28 18:36:24 -07:00

31 lines
632 B
Go

package pool
import (
"code.google.com/p/go.net/context"
)
const reqkey = "pool"
// NewContext returns a Context whose Value method returns the
// worker pool.
func NewContext(parent context.Context, pool *Pool) context.Context {
return &wrapper{parent, pool}
}
type wrapper struct {
context.Context
pool *Pool
}
// Value returns the named key from the context.
func (c *wrapper) Value(key interface{}) interface{} {
if key == reqkey {
return c.pool
}
return c.Context.Value(key)
}
// FromContext returns the pool assigned to the context.
func FromContext(c context.Context) *Pool {
return c.Value(reqkey).(*Pool)
}