Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Network Resource Manager interface #229

Merged
merged 57 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
cfce3f5
add resource manager interfaces
vyzo Dec 16, 2021
044fbea
add scope accessors to streams and conns
vyzo Dec 16, 2021
193ee25
add ResourceManager accessor to the Network interface
vyzo Dec 16, 2021
2afdeee
allow initially unattached streams.
vyzo Dec 16, 2021
46cbcfe
introduce service scopes, canonicalize ownership interface through se…
vyzo Dec 21, 2021
d4cfc93
make system scope explicit
vyzo Dec 21, 2021
586f1d0
make memory stat an int64
vyzo Dec 21, 2021
b3f9e1d
make the system scope a generic resource scope, introduce the DMZ
vyzo Dec 21, 2021
35ec4fd
fix typo
vyzo Dec 21, 2021
081233f
fix typo
vyzo Dec 21, 2021
5767cbb
Merge branch 'master' into feat/rcmgr
vyzo Dec 21, 2021
aeb9233
rename DMZ to transient scope, remove OpenConnection from PeerScope
vyzo Dec 22, 2021
d997bbb
remove ncopy param from GrowBuffer
vyzo Dec 22, 2021
8f759d3
remove protocols from OpenStream
vyzo Dec 22, 2021
dcef937
document nil receiver contract, fix protocol scope protocol accessor …
vyzo Dec 22, 2021
c52807c
remove nil receiver contract requirement
vyzo Dec 23, 2021
76a25c9
flesh out stat struct
vyzo Dec 23, 2021
25d4931
turn resource manager scope accessors into viewers
vyzo Dec 23, 2021
deb1c16
interface refiniments
vyzo Dec 24, 2021
1f5a414
add Buffer interface
vyzo Dec 27, 2021
6400bea
fix typo
vyzo Dec 28, 2021
3357752
fix typo
vyzo Dec 28, 2021
d99bef0
fix typo
vyzo Dec 28, 2021
c452665
rename user scopes to plain names, management scopes as such
vyzo Dec 28, 2021
219e9bc
rename BeginTxn to BeginTransaction
vyzo Dec 28, 2021
3ecf62c
RIP Buffers
vyzo Dec 28, 2021
535c095
make ErrResourceLimitExceeded a temporary error; move rcmgr errors wi…
vyzo Dec 29, 2021
caacd96
unexport TemporaryError
vyzo Dec 30, 2021
a5d2e41
null resource manager stub
vyzo Dec 31, 2021
b948cd7
unexport the null stubs, make entry point a variable
vyzo Dec 31, 2021
48c94b6
don't rely on typed nils but instead use actual null object instances
vyzo Dec 31, 2021
5fcc7b6
add Scope to the CapableConn interface
vyzo Dec 31, 2021
625a159
rename ConnectionScope to ConnScope for consistency
vyzo Dec 31, 2021
f205ca6
fix typo
vyzo Dec 31, 2021
31b6a33
rename ConnectionManagementScope to ConnManagementScope
vyzo Dec 31, 2021
59c3e97
Merge branch 'master' of github.com:libp2p/go-libp2p-core into feat/r…
vyzo Jan 4, 2022
f4f1520
add the ConnManagementScope to Upgrader.Upgrade
marten-seemann Jan 4, 2022
d2ff788
fix argument name
marten-seemann Jan 4, 2022
d1a82f1
godocs for ResourceManager
vyzo Jan 4, 2022
541324d
introduce MemoryStatus indicator in ReserveMemory
vyzo Jan 5, 2022
cafe95a
use uint8 for MemoryStatus
vyzo Jan 5, 2022
84d55d3
rework reservation interface to pass priority instead of returning me…
vyzo Jan 6, 2022
d9136f0
improve comment
vyzo Jan 6, 2022
2192774
fix typo
vyzo Jan 6, 2022
005b84b
export the NullScope
marten-seemann Jan 7, 2022
9d473a8
Stream.SetProtocol can return an error
vyzo Jan 9, 2022
0c1046c
merge the mux package into network
marten-seemann Jan 13, 2022
0391e67
pass the PeerScope to Multiplexer.NetConn
marten-seemann Jan 13, 2022
d3d9ac7
Update network/rcmgr.go
vyzo Jan 13, 2022
140d0ed
Update network/rcmgr.go
vyzo Jan 13, 2022
7a1c47e
Update network/rcmgr.go
vyzo Jan 13, 2022
e2bac5c
Update network/rcmgr.go
vyzo Jan 14, 2022
4bfde8e
remove reference to deprecated mux.MuxedConn
vyzo Jan 14, 2022
68e492f
rename transaction to span
vyzo Jan 14, 2022
953320f
indicate bytes in ReserveMemory
vyzo Jan 14, 2022
06aa6f9
break ResourceManager View methods into Viewer interface.
vyzo Jan 14, 2022
6b8d8bf
add experimental interface warning
vyzo Jan 14, 2022
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
3 changes: 3 additions & 0 deletions network/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Conn interface {

// GetStreams returns all open streams over this conn.
GetStreams() []Stream

// Scope returns the connection resource scope
Scope() ConnectionScope
}

// ConnSecurity is the interface that one can mix into a connection interface to
Expand Down
3 changes: 3 additions & 0 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type Network interface {
// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
// use the known local interfaces.
InterfaceListenAddresses() ([]ma.Multiaddr, error)

// ResourceManager returns the ResourceManager associated with this network
ResourceManager() ResourceManager
}

// Dialer represents a service that can dial out to peers
Expand Down
136 changes: 136 additions & 0 deletions network/rcmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package network

import (
"errors"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
)

var (
ErrResourceLimitExceeded = errors.New("resource limit exceeded")
ErrResourceScopeClosed = errors.New("resource scope closed")
)

// ResourceManager is the interface to the network resource management subsystem
type ResourceManager interface {
// GetSystem retrieves the system wide resource scope
GetSystem() ResourceScope
// GetDMZ retrieves the DMZ resource scope
GetDMZ() ResourceScope
// GetService retrieves a service-specific scope
GetService(srv string) ServiceScope
// GetProtocol retrieves the resource management scope for a specific protocol.
// If there is no configured limits for a particular protocol, then the default scope is
// returned.
GetProtocol(protocol.ID) ProtocolScope
// GetPeer retrieces the resource management scope for a specific peer.
GetPeer(peer.ID) PeerScope

// OpenConnection creates a connection scope not yet associated with any peer; the connection
// is scoped at the DMZ.
OpenConnection(dir Direction, usefd bool) (ConnectionScope, error)

// Close closes the resource manager
Close() error
}

// ResourceScope is the interface for all scopes.
type ResourceScope interface {
// ReserveMemory reserves memory/buffer space in the scope.
ReserveMemory(size int) error
// ReleaseMemory explicitly releases memory previously reserved with ReserveMemory
ReleaseMemory(size int)

// GetBuffer reserves memory and allocates a buffer through the buffer pool.
GetBuffer(size int) ([]byte, error)
// GrowBuffer atomically grows a previous allocated buffer, reserving the appropriate memory space
// and releasing the old buffer. The copy parameter specifies the number of bytes to copy from
// the old buffer to the newly allocated buffer.
GrowBuffer(buf []byte, newsize, copy int) ([]byte, error)
// ReleaseBuffer releases a previous allocated buffer.
ReleaseBuffer(buf []byte)

// Stat retrieves current resource usage for the scope.
Stat() ScopeStat
}

// TransactionalScope is a mixin interface for transactional scopes.
type TransactionalScope interface {
// Done ends the transaction scope and releases associated resources.
Done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any examples of how we think we might want to use these interfaces? We should start with some toy applications before we spend too much time on these APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most basic hooks will be inside the transport and muxer implementations.
We need to cater to both them, at the bowels of the stack, and higher level users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add testable examples to the godocs? https://go.dev/blog/examples

}

// ServiceScope is the interface for service resource scopes
type ServiceScope interface {
ResourceScope

// Name returns the name of this service
Name() string
}

// ProtocolScope is the interface for protocol resource scopes.
type ProtocolScope interface {
ResourceScope

// Protocols returns the list of protocol IDs constrained by this scope.
Protocols() []protocol.ID
}

// PeerScope is the interface for peer resource scopes.
type PeerScope interface {
ResourceScope

// Peer returns the peer ID for this scope
Peer() peer.ID

// OpenConnection creates a new connection scope for this peer.
OpenConnection(dir Direction, useFD bool) (ConnectionScope, error)

// OpenStream creates a new stream scope, with the specified protocols.
// An unnegotiated stream will have an empty protocol list and be initially unattached to any
// protocol scope.
OpenStream(dir Direction, proto ...protocol.ID) (StreamScope, error)
}

// ConnectionScope is the interface for connection resource scopes.
type ConnectionScope interface {
ResourceScope
TransactionalScope

// PeerScope returns the peer scope associated with this connection.
// It reeturns nil if the connection is not yet asociated with any peer.
PeerScope() PeerScope

// SetPeer sets the peer for a previously unassociated connection
SetPeer(peer.ID) error
}

// StreamScope is the interface for stream resource scopes
type StreamScope interface {
ResourceScope
TransactionalScope

// ProtocolScope returns the protocol resource scope associated with this stream.
// It returns nil if the stream is not associated with any scope.
ProtocolScope() ProtocolScope
// SetProtocol sets the protocol for a previously unnegotiated stream
SetProtocol(proto protocol.ID) error

// ServiceScope returns the service owning the stream, if any.
ServiceScope() ServiceScope
// SetService sets the service owning this stream
SetService(srv string) error

// PeerScope returns the peer resource scope associated with this stream.
PeerScope() PeerScope
}

// ScopeStat is a struct containing resource accounting information.
type ScopeStat struct {
NumPeers int
NumConns int
NumStreams int

Memory int64
}
3 changes: 3 additions & 0 deletions network/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ type Stream interface {

// Conn returns the connection this stream is part of.
Conn() Conn

// Scope returns the stream resource scope
Scope() StreamScope
}