Documentation ¶
Overview ¶
Example ¶
In this example we spawn a large number of goroutines that are blocking on simluated calls of varying duration. While the calls are running we monitor the tracker to observe the backlog.
package main import ( "fmt" "math/rand" "time" "github.com/gentlemanautomaton/calltracker" ) func main() { const ( // How many calls should we make in parallel? calls = 1000 // What's the lower bound of a typical call duration? lower = int64(time.Millisecond * 20) // What's the upper bound of a typical call duration? upper = int64(time.Second * 4) // What's the maximum duration of a call? max = int64(time.Second * 8) ) // Create trackers simply by declaring them. // The zero value is safe to use. // The tracker's methods are thread-safe. var t calltracker.Tracker // Simulate some number of simultaneous calls. go func() { fmt.Printf("Starting first call (1 of %d)\n", calls) for i := 0; i < calls; i++ { // Register the start of a new call with the tracker as early as possible. call := t.Add() // For this example, arbitrarily decide how long the call should take. d := time.Duration(rand.Int63n(upper-lower) + lower) // Occassionally thrown in a call that has a really long or short // duration. if rand.Intn(50) == 0 { d = time.Duration(rand.Int63n(max)) } // Make the call in its own goroutine. go func() { // Notify the tracker when we're finished. defer call.Done() // Simulate some long-running process. time.Sleep(d) }() // Sleep a random amount after every call to stagger the rate time.Sleep(time.Duration(rand.Int63n(lower / 2))) } fmt.Printf("Starting last call (%d of %d)\n", calls, calls) }() // Monitor the call statistics until all of the calls have finished. ticker := time.NewTicker(time.Second) defer ticker.Stop() for { // Wait until it's time to report. <-ticker.C // Grab a snapshot of the current call backlog. v := t.Value() // Report values fmt.Printf("%d calls outstanding. Elapsed min: %v max: %v: combined: %v\n", v.Len(), v.MinElapsed(), v.MaxElapsed(), v.Elapsed()) // Check whether all calls have finished. if v.Len() == 0 { break } } fmt.Println("Done.") }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
Call is an oustanding call.
type Queue ¶
type Queue []Call
Queue is an ordered queue of calls. The oldest calls are at the beginning, and the newest calls are at the end. Age is determined by comparing the number of each call.
func (Queue) Index ¶
Index returns the index of the first call with the given number in the queue. It will return -1 if such a call is not present in the queue.
type Subscriber ¶
type Subscriber func(update Update)
Subscriber is a function that receivers tracker event notifications.
type TrackedCall ¶
type TrackedCall struct { Call // contains filtered or unexported fields }
TrackedCall is a call for which completion can be signaled.
func (*TrackedCall) Done ¶
func (tc *TrackedCall) Done()
Done should be called when a tracked call has been completed.
It is safe to call Done() more than once, but it is not safe to call it simulataneously from separate goroutines.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker tracks a set of outstanding calls. Its zero value is safe for use. It is safe to call its functions from more than one goroutine simulataneously.
func (*Tracker) Add ¶
func (t *Tracker) Add() (call TrackedCall)
Add will add a call to the tracker. The call's start time will be the time at which Add() was called.
The returned value is a tracked call that allows the caller to indicate when the call has finished. The caller must call its Done() function to remove the call from the tracker's list of tracked calls.
func (*Tracker) Subscribe ¶
func (t *Tracker) Subscribe(s Subscriber)
Subscribe will add the given subscriber function as a listener that will receive updates whenever the tracker's value changes.
type Value ¶
type Value []Call
Value is a point-in-time view of outstanding calls. The calls are ordered from oldest to newest.
func (Value) Elapsed ¶
Elapsed returns the total amount of time that has elapsed across all oustanding calls.
func (Value) MaxElapsed ¶
MaxElapsed returns the greatest amount of time that has elapsed for any one of the outstanding calls. If there are no outstanding calls it will return a duration of zero.
func (Value) MinElapsed ¶
MinElapsed returns the smallest amount of time that has elapsed for any one of the outstanding calls. If there are no outstanding calls it will return a duration of zero.