GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cache/bulkhead.go at main · ubgo/cache

Expand file tree

/

bulkhead.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
136 lines (112 loc) · 4.76 KB
/

bulkhead.go

Copy path

File metadata and controls

136 lines (112 loc) · 4.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// bulkhead.go — NewBulkhead: bounded-concurrency Cache wrapper (package cache, github.com/ubgo/cache).
//
// Package role: cache is the root bytes-level cache contract of the
// ubgo/cache family; see doc.go for the package overview.
//
// This file: implements NewBulkhead which caps in-flight ops at
// maxConcurrent via a buffered-channel semaphore so one overloaded
// caller/namespace cannot exhaust backend connections and starve everyone
// else. The WHY: failure isolation under load. Invariant: acquire is
// context-aware — a slow backend surfaces as ctx.Err() (or ErrTimeout when
// ctx has no error) rather than unbounded goroutine growth; maxConcurrent
// < 1 is clamped to 1.
//
// AI-context: this is a Cache decorator wrapping every method through
// bulkheadGuard/bulkheadDo (acquire-defer-release); the `var _ Cache`
// assertion guards interface drift. Sibling of resilience.go — composes
// with breaker/retry; ordering is the caller's choice.
package cache
import (
"context"
"time"
)
// bulkhead bounds the number of in-flight operations so one overloaded
// caller/namespace cannot exhaust backend connections and starve everyone
// else. Acquire is context-aware: a slow backend surfaces as ctx.Err() (or
// ErrTimeout) rather than unbounded goroutine growth.
type bulkhead struct {
Cache
sem chan struct{}
}
// NewBulkhead wraps c, allowing at most maxConcurrent operations to be in
// flight at once; further ops block until a slot frees or their context is
// done. maxConcurrent < 1 is treated as 1.
func NewBulkhead(c Cache, maxConcurrent int) Cache {
if maxConcurrent < 1 {
maxConcurrent = 1
}
return &bulkhead{Cache: c, sem: make(chan struct{}, maxConcurrent)}
}
func (b *bulkhead) acquire(ctx context.Context) error {
select {
case b.sem <- struct{}{}:
return nil
case <-ctx.Done():
if err := ctx.Err(); err != nil {
return err
}
return ErrTimeout
}
}
func (b *bulkhead) release() { <-b.sem }
func bulkheadGuard[T any](ctx context.Context, b *bulkhead, fn func() (T, error)) (T, error) {
var zero T
if err := b.acquire(ctx); err != nil {
return zero, err
}
defer b.release()
return fn()
}
func bulkheadDo(ctx context.Context, b *bulkhead, fn func() error) error {
if err := b.acquire(ctx); err != nil {
return err
}
defer b.release()
return fn()
}
func (b *bulkhead) Get(ctx context.Context, key string) ([]byte, error) {
return bulkheadGuard(ctx, b, func() ([]byte, error) { return b.Cache.Get(ctx, key) })
}
func (b *bulkhead) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error) {
return bulkheadGuard(ctx, b, func() (map[string][]byte, error) { return b.Cache.GetMulti(ctx, keys) })
}
func (b *bulkhead) Has(ctx context.Context, key string) (bool, error) {
return bulkheadGuard(ctx, b, func() (bool, error) { return b.Cache.Has(ctx, key) })
}
func (b *bulkhead) TTL(ctx context.Context, key string) (time.Duration, error) {
return bulkheadGuard(ctx, b, func() (time.Duration, error) { return b.Cache.TTL(ctx, key) })
}
func (b *bulkhead) Set(ctx context.Context, key string, val []byte, ttl time.Duration) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Set(ctx, key, val, ttl) })
}
func (b *bulkhead) SetMulti(ctx context.Context, items map[string]Item) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.SetMulti(ctx, items) })
}
func (b *bulkhead) SetNX(ctx context.Context, key string, val []byte, ttl time.Duration) (bool, error) {
return bulkheadGuard(ctx, b, func() (bool, error) { return b.Cache.SetNX(ctx, key, val, ttl) })
}
func (b *bulkhead) Expire(ctx context.Context, key string, ttl time.Duration) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Expire(ctx, key, ttl) })
}
func (b *bulkhead) Touch(ctx context.Context, key string) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Touch(ctx, key) })
}
func (b *bulkhead) Incr(ctx context.Context, key string, delta int64) (int64, error) {
return bulkheadGuard(ctx, b, func() (int64, error) { return b.Cache.Incr(ctx, key, delta) })
}
func (b *bulkhead) Decr(ctx context.Context, key string, delta int64) (int64, error) {
return bulkheadGuard(ctx, b, func() (int64, error) { return b.Cache.Decr(ctx, key, delta) })
}
func (b *bulkhead) Del(ctx context.Context, keys ...string) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Del(ctx, keys...) })
}
func (b *bulkhead) DeleteByPrefix(ctx context.Context, prefix string) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.DeleteByPrefix(ctx, prefix) })
}
func (b *bulkhead) Flush(ctx context.Context) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Flush(ctx) })
}
func (b *bulkhead) Ping(ctx context.Context) error {
return bulkheadDo(ctx, b, func() error { return b.Cache.Ping(ctx) })
}
var _ Cache = (*bulkhead)(nil)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.