forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
63 lines (54 loc) · 2.16 KB
/
resource.go
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
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package mon
import (
"github.com/ruiaylin/pgparser/pgwire/pgcode"
"github.com/ruiaylin/pgparser/pgwire/pgerror"
"github.com/cockroachdb/errors"
)
// Resource is an interface used to abstract the specifics of tracking bytes
// usage by different types of resources.
type Resource interface {
NewBudgetExceededError(requestedBytes int64, reservedBytes int64, budgetBytes int64) error
}
// memoryResource is a Resource that represents memory.
type memoryResource struct{}
// MemoryResource is a utility singleton used as an argument when creating a
// BytesMonitor to indicate that the monitor will be tracking memory usage.
var MemoryResource Resource = memoryResource{}
// NewBudgetExceededError implements the Resource interface.
func (m memoryResource) NewBudgetExceededError(
requestedBytes int64, reservedBytes int64, budgetBytes int64,
) error {
return pgerror.WithCandidateCode(
errors.Newf(
"memory budget exceeded: %d bytes requested, %d currently allocated, %d bytes in budget",
errors.Safe(requestedBytes),
errors.Safe(reservedBytes),
errors.Safe(budgetBytes),
), pgcode.OutOfMemory)
}
// diskResource is a Resource that represents disk.
type diskResource struct{}
// DiskResource is a utility singleton used as an argument when creating a
// BytesMonitor to indicate that the monitor will be tracking disk usage.
var DiskResource Resource = diskResource{}
// NewBudgetExceededError implements the Resource interface.
func (d diskResource) NewBudgetExceededError(
requestedBytes int64, reservedBytes int64, budgetBytes int64,
) error {
return pgerror.WithCandidateCode(
errors.Newf(
"disk budget exceeded: %d bytes requested, %d currently allocated, %d bytes in budget",
errors.Safe(requestedBytes),
errors.Safe(reservedBytes),
errors.Safe(budgetBytes),
), pgcode.DiskFull)
}