connstate

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2019 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Overview

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright (c) 2016-2019 Uber Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTorrentAtCapacity       = errors.New("torrent is at capacity")
	ErrConnAlreadyPending      = errors.New("conn is already pending")
	ErrConnAlreadyActive       = errors.New("conn is already active")
	ErrConnClosed              = errors.New("conn is closed")
	ErrInvalidActiveTransition = errors.New("conn must be pending to transition to active")
	ErrTooManyMutualConns      = errors.New("conn has too many mutual connections")
)

State errors.

Functions

This section is empty.

Types

type BlacklistedConn

type BlacklistedConn struct {
	PeerID    core.PeerID   `json:"peer_id"`
	InfoHash  core.InfoHash `json:"info_hash"`
	Remaining time.Duration `json:"remaining"`
}

BlacklistedConn represents a connection which has been blacklisted.

type Config

type Config struct {

	// MaxOpenConnectionsPerTorrent is the maximum number of connections which a
	// Scheduler will maintain at once for each torrent.
	MaxOpenConnectionsPerTorrent int `yaml:"max_open_conn"`

	// MaxMutualConnections is the maximum number of mutual connections a peer
	// can have and still connect with us.
	MaxMutualConnections int `yaml:"max_mutual_conn"`

	// DisableBlacklist disables the blacklisting of peers. Should only be used
	// for testing purposes.
	DisableBlacklist bool `yaml:"disable_blacklist"`

	// BlacklistDuration is the duration a connection will remain blacklisted.
	BlacklistDuration time.Duration `yaml:"blacklist_duration"`
}

Config defines State configuration.

type State

type State struct {
	// contains filtered or unexported fields
}

State provides connection lifecycle management and enforces connection limits. A connection to a peer is identified by peer id and torrent info hash. Each connection may exist in the following states: pending, active, or blacklisted. Pending connections are unestablished connections which "reserve" connection capacity until they are done handshaking. Active connections are established connections. Blacklisted connections are failed connections which should be skipped in each peer handout.

Note, State is NOT thread-safe. Synchronization must be provided by the client.

func New

func New(
	config Config,
	clk clock.Clock,
	localPeerID core.PeerID,
	netevents networkevent.Producer,
	logger *zap.SugaredLogger) *State

New creates a new State.

func (*State) ActiveConns

func (s *State) ActiveConns() []*conn.Conn

ActiveConns returns a list of all active connections.

func (*State) AddPending

func (s *State) AddPending(peerID core.PeerID, h core.InfoHash, neighbors []core.PeerID) error

AddPending sets the connection for peerID/h as pending and reserves capacity for it.

func (*State) Blacklist

func (s *State) Blacklist(peerID core.PeerID, h core.InfoHash) error

Blacklist blacklists peerID/h for the configured BlacklistDuration. Returns error if the connection is already blacklisted.

func (*State) BlacklistSnapshot

func (s *State) BlacklistSnapshot() []BlacklistedConn

BlacklistSnapshot returns a snapshot of all valid blacklist entries.

func (*State) Blacklisted

func (s *State) Blacklisted(peerID core.PeerID, h core.InfoHash) bool

Blacklisted returns true if peerID/h is blacklisted.

func (*State) ClearBlacklist

func (s *State) ClearBlacklist(h core.InfoHash)

ClearBlacklist un-blacklists all connections for h.

func (*State) DeleteActive

func (s *State) DeleteActive(c *conn.Conn)

DeleteActive deletes c. No-ops if c is not an active conn.

func (*State) DeletePending

func (s *State) DeletePending(peerID core.PeerID, h core.InfoHash)

DeletePending deletes the pending connection for peerID/h and frees capacity.

func (*State) MaxConnsPerTorrent

func (s *State) MaxConnsPerTorrent() int

MaxConnsPerTorrent returns the max number of connections a torrent is permitted to have.

func (*State) MovePendingToActive

func (s *State) MovePendingToActive(c *conn.Conn) error

MovePendingToActive sets a previously pending connection as active.

func (*State) NumActiveConns

func (s *State) NumActiveConns() int

NumActiveConns returns the total number of active connections.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL