|
| 1 | +# Public API |
| 2 | + |
| 3 | +```go |
| 4 | +package chanprobe |
| 5 | + |
| 6 | +var ( |
| 7 | + ErrClosed error |
| 8 | + ErrFull error |
| 9 | +) |
| 10 | + |
| 11 | +type DropPolicy int |
| 12 | + |
| 13 | +const ( |
| 14 | + Block DropPolicy = iota |
| 15 | + DropNewest |
| 16 | + DropOldest |
| 17 | +) |
| 18 | + |
| 19 | +type Option func(*Options) |
| 20 | + |
| 21 | +type Options struct { |
| 22 | + DropPolicy DropPolicy |
| 23 | + Registry *Registry |
| 24 | +} |
| 25 | + |
| 26 | +func WithDropPolicy(policy DropPolicy) Option |
| 27 | +func WithRegistry(reg *Registry) Option |
| 28 | + |
| 29 | +type Queue[T any] struct{} |
| 30 | + |
| 31 | +func New[T any](name string, capacity int, opts ...Option) *Queue[T] |
| 32 | + |
| 33 | +func (q *Queue[T]) Name() string |
| 34 | +func (q *Queue[T]) Send(ctx context.Context, item T) error |
| 35 | +func (q *Queue[T]) Recv(ctx context.Context) (T, bool) |
| 36 | +func (q *Queue[T]) TrySend(item T) bool |
| 37 | +func (q *Queue[T]) TryRecv() (T, bool) |
| 38 | +func (q *Queue[T]) Close() |
| 39 | +func (q *Queue[T]) Len() int |
| 40 | +func (q *Queue[T]) Cap() int |
| 41 | +func (q *Queue[T]) Snapshot() Snapshot |
| 42 | + |
| 43 | +type Snapshot struct { |
| 44 | + Name string `json:"name"` |
| 45 | + Len int `json:"len"` |
| 46 | + Cap int `json:"cap"` |
| 47 | + Closed bool `json:"closed"` |
| 48 | + SentTotal uint64 `json:"sent_total"` |
| 49 | + ReceivedTotal uint64 `json:"received_total"` |
| 50 | + DroppedTotal uint64 `json:"dropped_total"` |
| 51 | + SendBlockedTotal uint64 `json:"send_blocked_total"` |
| 52 | + RecvBlockedTotal uint64 `json:"recv_blocked_total"` |
| 53 | + SendWaitTotal time.Duration `json:"send_wait_total"` |
| 54 | + RecvWaitTotal time.Duration `json:"recv_wait_total"` |
| 55 | + ItemWaitTotal time.Duration `json:"item_wait_total"` |
| 56 | + OldestItemAge time.Duration `json:"oldest_item_age"` |
| 57 | +} |
| 58 | + |
| 59 | +type Snapshoter interface { |
| 60 | + Snapshot() Snapshot |
| 61 | +} |
| 62 | + |
| 63 | +type Registry struct{} |
| 64 | + |
| 65 | +func NewRegistry() *Registry |
| 66 | +func DefaultRegistry() *Registry |
| 67 | +func (r *Registry) Register(name string, snapper Snapshoter) |
| 68 | +func (r *Registry) Unregister(name string) |
| 69 | +func (r *Registry) Snapshots() []Snapshot |
| 70 | + |
| 71 | +func PublishExpvar(name string, reg *Registry) |
| 72 | +``` |
| 73 | + |
| 74 | +## Notes |
| 75 | + |
| 76 | +- `New` panics for an empty name or non-positive capacity. |
| 77 | +- `Send` returns `ErrClosed` after `Close`. |
| 78 | +- `Send` returns `ctx.Err()` when a blocking send is canceled. |
| 79 | +- `Recv` returns `ok=false` when the queue is closed and drained, or when a |
| 80 | + blocking receive is canceled. |
| 81 | +- `PublishExpvar` uses `DefaultRegistry()` when the registry argument is nil. |
0 commit comments