Select Git revision
hostPool.go
hostPool.go 13.40 KiB
///////////////////////////////////////////////////////////////////////////////
// Copyright © 2020 xx network SEZC //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
// Package gateway Handles functionality related to providing Gateway connect.Host objects
// for message sending to the rest of the client repo
// Used to minimize # of open connections on mobile clients
package gateway
import (
"encoding/binary"
"fmt"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/storage"
"gitlab.com/elixxir/comms/network"
"gitlab.com/elixxir/crypto/fastRNG"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/primitives/id"
"gitlab.com/xx_network/primitives/ndf"
"golang.org/x/net/context"
"io"
"math"
"strings"
"sync"
"time"
)
// List of errors that initiate a Host replacement
var errorsList = []string{context.DeadlineExceeded.Error(), "connection refused", "host disconnected",
"transport is closing", "all SubConns are in TransientFailure", "Last try to connect",
ndf.NO_NDF, "Host is in cool down"}
// HostManager Interface allowing storage and retrieval of Host objects
type HostManager interface {
GetHost(hostId *id.ID) (*connect.Host, bool)
AddHost(hid *id.ID, address string, cert []byte, params connect.HostParams) (host *connect.Host, err error)
RemoveHost(hid *id.ID)
}
// HostPool Handles providing hosts to the Client
type HostPool struct {
hostMap map[id.ID]uint32 // map key to its index in the slice
hostList []*connect.Host // each index in the slice contains the value
hostMux sync.RWMutex // Mutex for the above map/list combination
ndfMap map[id.ID]int // map gateway ID to its index in the ndf
ndf *ndf.NetworkDefinition
ndfMux sync.RWMutex
poolParams PoolParams
rng *fastRNG.StreamGenerator
storage *storage.Session
manager HostManager
addGatewayChan chan network.NodeGateway
}
// PoolParams Allows configuration of HostPool parameters
type PoolParams struct {
MaxPoolSize uint32 // Maximum number of Hosts in the HostPool
PoolSize uint32 // Allows override of HostPool size. Set to zero for dynamic size calculation
// TODO: Move up a layer
ProxyAttempts uint32 // How many proxies will be used in event of send failure
HostParams connect.HostParams // Parameters for the creation of new Host objects
}