calltracker

package module
v0.0.0-...-80f56b3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2017 License: MIT Imports: 3 Imported by: 4

README

calltracker

Utility library tracking outstanding calls in the Go programming language

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.

func (Call) Elapsed

func (c Call) Elapsed() time.Duration

Elapsed returns the amount of time that has elapsed since the call started.

func (Call) Number

func (c Call) Number() uint64

Number returns the number of calls that have taken place before this call.

func (Call) When

func (c Call) When() time.Time

When returns the time at which the call started.

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

func (q Queue) Index(number uint64) int

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.

func (*Queue) Push

func (q *Queue) Push(c Call)

Push inserts the call c at the end of the queue without verifying the correctness of its ordering.

func (*Queue) Remove

func (q *Queue) Remove(i int)

Remove removes the element at index i from the queue. The complexity is O(log(n)) where n = len(q).

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.

func (*Tracker) Value

func (t *Tracker) Value() (value Value)

Value returns a point-in-time view of the tracked calls.

type Update

type Update struct {
	Sequence uint64
	Value    Value
}

Update is an update to the tracker's value

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

func (v Value) Elapsed() time.Duration

Elapsed returns the total amount of time that has elapsed across all oustanding calls.

func (Value) Len

func (v Value) Len() int

Len returns the total number of outstanding calls.

func (Value) MaxElapsed

func (v Value) MaxElapsed() time.Duration

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

func (v Value) MinElapsed() time.Duration

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL