Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions driver/memcached/alloc_guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestAllocBudgets(t *testing.T) {
// per-request slab allocation that replaces the per-value copies
// — tracked for v1.4.1.
{name: "GetMulti_10", budget: 26, bench: benchGetMulti10},
// GET with a context that can be cancelled (WithCancel, WithTimeout, an
// HTTP/2 stream's): on the direct path the blocked read is woken when the
// context ends, which costs context.AfterFunc's two allocations on top of
// the 3 of GET_small (celeris#1218). The wake's own state lives on the conn.
{name: "GET_small_cancellable", budget: 5, bench: benchGETSmallCancellable},
{name: "PoolAcquireRelease", budget: 0, bench: benchPoolAcquireRelease},
}

Expand Down Expand Up @@ -102,6 +107,28 @@ func benchGETSmall(b *testing.B) {
}
}

func benchGETSmallCancellable(b *testing.B) {
fake := startFakeB(b)
fake.store.kv["k"] = memItem{value: []byte("v"), cas: 1}
c, err := NewClient(fake.Addr())
if err != nil {
b.Fatal(err)
}
defer func() { _ = c.Close() }()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if _, err := c.Get(ctx, "k"); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := c.Get(ctx, "k"); err != nil {
b.Fatal(err)
}
}
}

func benchSETSmall(b *testing.B) {
fake := startFakeB(b)
c, err := NewClient(fake.Addr())
Expand Down
3 changes: 2 additions & 1 deletion driver/memcached/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ type ClusterConfig struct {
// [Config.MaxOpen]; zero means NumWorkers * 4 per node.
MaxOpen int

// Timeout is the advisory per-op deadline forwarded to each node client.
// Timeout is the per-operation deadline forwarded to each node client: see
// [Config.Timeout].
Timeout time.Duration

// DialTimeout is forwarded to each node client: see [Config.DialTimeout]
Expand Down
63 changes: 58 additions & 5 deletions driver/memcached/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,66 @@ func asBytes(v any) ([]byte, error) {
}
}

// run acquires a conn, runs fn with it, and releases the conn. On ctx
// cancellation or context deadline the conn is discarded because the server
// may still send a stale reply.
// opTimeout is the per-operation bound of the client: Config.Timeout, the default
// (3 s) when it is zero, none when it is negative.
func (c *Client) opTimeout() time.Duration {
t := c.cfg.Timeout
if t == 0 {
t = defaultTimeout
if h := testHookDefaultOpTimeout.Load(); h != nil {
t = *h
}
}
return max(t, 0)
}

// opLimit works out the per-operation limit of one command (celeris#1218), as a
// value: no context is built, so a command pays no allocation for it. It returns
// the timeout and the time it ends, or zero values when the command is not limited
// by it: the timeout is off, or the caller's deadline is not later than it (the
// caller's deadline then ends the command, and its error is the caller's).
func (c *Client) opLimit(ctx context.Context) (timeout time.Duration, deadline time.Time) {
t := c.opTimeout()
if t == 0 {
return 0, time.Time{}
}
dl := time.Now().Add(t)
if cdl, ok := ctx.Deadline(); ok && !cdl.After(dl) {
return 0, time.Time{}
}
return t, dl
}

// beginOp puts the per-operation limit on conn for one command and, for a direct
// conn (which waits in a socket read that does not look at a context), arms the wake
// of its read on the end of a cancellable ctx (armWake). A ctx that cannot be
// cancelled costs nothing; a cancellable one costs context.AfterFunc's two
// allocations. Pair it with endOp, after opError.
func (c *Client) beginOp(ctx context.Context, conn *memcachedConn) {
conn.opTimeout, conn.opDeadline = c.opLimit(ctx)
if conn.useDirect && ctx.Done() != nil {
conn.armWake(ctx)
}
}

// endOp ends the limit of the command that ran on conn and disarms the wake.
func endOp(conn *memcachedConn) {
conn.disarmWake()
conn.opTimeout, conn.opDeadline = 0, time.Time{}
}

// run acquires a conn, runs fn with it, and releases the conn. fn runs under the
// per-operation timeout (beginOp); acquiring the conn does not (a new conn has its
// own DialTimeout). On ctx cancellation or context deadline the conn is discarded
// because the server may still send a stale reply.
func (c *Client) run(ctx context.Context, fn func(ctx context.Context, conn *memcachedConn) error) error {
conn, err := c.pool.acquire(ctx, workerFromCtx(ctx))
if err != nil {
return err
}
ferr := fn(ctx, conn)
c.beginOp(ctx, conn)
ferr := conn.opError(ctx, fn(ctx, conn))
endOp(conn)
if ferr != nil && (errors.Is(ferr, context.Canceled) || errors.Is(ferr, context.DeadlineExceeded)) {
c.pool.discard(conn)
return ferr
Expand Down Expand Up @@ -851,7 +902,9 @@ func (c *Client) Ping(ctx context.Context) error {
if err != nil {
return err
}
perr := conn.Ping(ctx)
c.beginOp(ctx, conn)
perr := conn.opError(ctx, conn.Ping(ctx))
endOp(conn)
if perr != nil {
if errors.Is(perr, context.Canceled) || errors.Is(perr, context.DeadlineExceeded) {
c.pool.discard(conn)
Expand Down
Loading
Loading