mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-02-10 03:40:43 +08:00
move errors to a separate file
This commit is contained in:
parent
e5cf68da47
commit
628c5461e7
81
error.go
Normal file
81
error.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package rcmgr
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/libp2p/go-libp2p-core/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
type errStreamOrConnLimitExceeded struct {
|
||||||
|
current, attempted, limit int
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errStreamOrConnLimitExceeded) Error() string { return e.err.Error() }
|
||||||
|
func (e *errStreamOrConnLimitExceeded) Unwrap() error { return e.err }
|
||||||
|
|
||||||
|
// edge may be "" if this is not an edge error
|
||||||
|
func logValuesStreamLimit(scope, edge string, dir network.Direction, stat network.ScopeStat, err error) []interface{} {
|
||||||
|
logValues := make([]interface{}, 0, 2*8)
|
||||||
|
logValues = append(logValues, "scope", scope)
|
||||||
|
if edge != "" {
|
||||||
|
logValues = append(logValues, "edge", edge)
|
||||||
|
}
|
||||||
|
logValues = append(logValues, "direction", dir)
|
||||||
|
var e *errStreamOrConnLimitExceeded
|
||||||
|
if errors.As(err, &e) {
|
||||||
|
logValues = append(logValues,
|
||||||
|
"current", e.current,
|
||||||
|
"attempted", e.attempted,
|
||||||
|
"limit", e.limit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return append(logValues, "stat", stat, "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// edge may be "" if this is not an edge error
|
||||||
|
func logValuesConnLimit(scope, edge string, dir network.Direction, usefd bool, stat network.ScopeStat, err error) []interface{} {
|
||||||
|
logValues := make([]interface{}, 0, 2*9)
|
||||||
|
logValues = append(logValues, "scope", scope)
|
||||||
|
if edge != "" {
|
||||||
|
logValues = append(logValues, "edge", edge)
|
||||||
|
}
|
||||||
|
logValues = append(logValues, "direction", dir, "usefd", usefd)
|
||||||
|
var e *errStreamOrConnLimitExceeded
|
||||||
|
if errors.As(err, &e) {
|
||||||
|
logValues = append(logValues,
|
||||||
|
"current", e.current,
|
||||||
|
"attempted", e.attempted,
|
||||||
|
"limit", e.limit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return append(logValues, "stat", stat, "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type errMemoryLimitExceeded struct {
|
||||||
|
current, attempted, limit int64
|
||||||
|
priority uint8
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *errMemoryLimitExceeded) Error() string { return e.err.Error() }
|
||||||
|
func (e *errMemoryLimitExceeded) Unwrap() error { return e.err }
|
||||||
|
|
||||||
|
// edge may be "" if this is not an edge error
|
||||||
|
func logValuesMemoryLimit(scope, edge string, stat network.ScopeStat, err error) []interface{} {
|
||||||
|
logValues := make([]interface{}, 0, 2*8)
|
||||||
|
logValues = append(logValues, "scope", scope)
|
||||||
|
if edge != "" {
|
||||||
|
logValues = append(logValues, "edge", edge)
|
||||||
|
}
|
||||||
|
var e *errMemoryLimitExceeded
|
||||||
|
if errors.As(err, &e) {
|
||||||
|
logValues = append(logValues,
|
||||||
|
"current", e.current,
|
||||||
|
"attempted", e.attempted,
|
||||||
|
"priority", e.priority,
|
||||||
|
"limit", e.limit,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return append(logValues, "stat", stat, "error", err)
|
||||||
|
}
|
75
scope.go
75
scope.go
@ -1,7 +1,6 @@
|
|||||||
package rcmgr
|
package rcmgr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -19,80 +18,6 @@ type resources struct {
|
|||||||
memory int64
|
memory int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type errStreamOrConnLimitExceeded struct {
|
|
||||||
current, attempted, limit int
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errStreamOrConnLimitExceeded) Error() string { return e.err.Error() }
|
|
||||||
func (e *errStreamOrConnLimitExceeded) Unwrap() error { return e.err }
|
|
||||||
|
|
||||||
// edge may be "" if this is not an edge error
|
|
||||||
func logValuesStreamLimit(scope, edge string, dir network.Direction, stat network.ScopeStat, err error) []interface{} {
|
|
||||||
logValues := make([]interface{}, 0, 2*8)
|
|
||||||
logValues = append(logValues, "scope", scope)
|
|
||||||
if edge != "" {
|
|
||||||
logValues = append(logValues, "edge", edge)
|
|
||||||
}
|
|
||||||
logValues = append(logValues, "direction", dir)
|
|
||||||
var e *errStreamOrConnLimitExceeded
|
|
||||||
if errors.As(err, &e) {
|
|
||||||
logValues = append(logValues,
|
|
||||||
"current", e.current,
|
|
||||||
"attempted", e.attempted,
|
|
||||||
"limit", e.limit,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return append(logValues, "stat", stat, "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// edge may be "" if this is not an edge error
|
|
||||||
func logValuesConnLimit(scope, edge string, dir network.Direction, usefd bool, stat network.ScopeStat, err error) []interface{} {
|
|
||||||
logValues := make([]interface{}, 0, 2*9)
|
|
||||||
logValues = append(logValues, "scope", scope)
|
|
||||||
if edge != "" {
|
|
||||||
logValues = append(logValues, "edge", edge)
|
|
||||||
}
|
|
||||||
logValues = append(logValues, "direction", dir, "usefd", usefd)
|
|
||||||
var e *errStreamOrConnLimitExceeded
|
|
||||||
if errors.As(err, &e) {
|
|
||||||
logValues = append(logValues,
|
|
||||||
"current", e.current,
|
|
||||||
"attempted", e.attempted,
|
|
||||||
"limit", e.limit,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return append(logValues, "stat", stat, "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
type errMemoryLimitExceeded struct {
|
|
||||||
current, attempted, limit int64
|
|
||||||
priority uint8
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *errMemoryLimitExceeded) Error() string { return e.err.Error() }
|
|
||||||
func (e *errMemoryLimitExceeded) Unwrap() error { return e.err }
|
|
||||||
|
|
||||||
// edge may be "" if this is not an edge error
|
|
||||||
func logValuesMemoryLimit(scope, edge string, stat network.ScopeStat, err error) []interface{} {
|
|
||||||
logValues := make([]interface{}, 0, 2*8)
|
|
||||||
logValues = append(logValues, "scope", scope)
|
|
||||||
if edge != "" {
|
|
||||||
logValues = append(logValues, "edge", edge)
|
|
||||||
}
|
|
||||||
var e *errMemoryLimitExceeded
|
|
||||||
if errors.As(err, &e) {
|
|
||||||
logValues = append(logValues,
|
|
||||||
"current", e.current,
|
|
||||||
"attempted", e.attempted,
|
|
||||||
"priority", e.priority,
|
|
||||||
"limit", e.limit,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
return append(logValues, "stat", stat, "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A resourceScope can be a DAG, where a downstream node is not allowed to outlive an upstream node
|
// A resourceScope can be a DAG, where a downstream node is not allowed to outlive an upstream node
|
||||||
// (ie cannot call Done in the upstream node before the downstream node) and account for resources
|
// (ie cannot call Done in the upstream node before the downstream node) and account for resources
|
||||||
// using a linearized parent set.
|
// using a linearized parent set.
|
||||||
|
Loading…
Reference in New Issue
Block a user