storj/pkg/transport/slowtransport.go

123 lines
3.3 KiB
Go
Raw Normal View History

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package transport
import (
"context"
"net"
"time"
"google.golang.org/grpc"
"storj.io/storj/internal/memory"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/pb"
)
// SimulatedNetwork allows creating connections that try to simulated realistic network conditions.
type SimulatedNetwork struct {
DialLatency time.Duration
BytesPerSecond memory.Size
}
// NewClient wraps an exiting client with the simulated network params.
func (network *SimulatedNetwork) NewClient(client Client) Client {
return &slowTransport{
client: client,
network: network,
}
}
// slowTransport is a slow version of transport
type slowTransport struct {
client Client
network *SimulatedNetwork
}
// DialNode dials a node with latency
func (client *slowTransport) DialNode(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return client.client.DialNode(ctx, node, append(client.network.DialOptions(), opts...)...)
}
// DialAddress dials an address with latency
func (client *slowTransport) DialAddress(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
return client.client.DialAddress(ctx, address, append(client.network.DialOptions(), opts...)...)
}
// Identity for slowTransport
func (client *slowTransport) Identity() *identity.FullIdentity {
return client.client.Identity()
}
// WithObservers calls WithObservers for slowTransport
Add Versioning Server (#1576) * Initial Webserver Draft for Version Controlling * Rename type to avoid confusion * Move Function Calls into Version Package * Fix Linting and Language Typos * Fix Linting and Spelling Mistakes * Include Copyright * Include Copyright * Adjust Version-Control Server to return list of Versions * Linting * Improve Request Handling and Readability * Add Configuration File Option Add Systemd Service file * Add Logging to File * Smaller Changes * Add Semantic Versioning and refuses outdated Software from Startup (#1612) * implements internal Semantic Version library * adds version logging + reporting to process * Advance SemVer struct for easier handling * Add Accepted Version Store * Fix Function * Restructure * Type Conversion * Handle Version String properly * Add Note about array index * Set temporary Default Version * Add Copyright * Adding Version to Dashboard * Adding Version Info Log * Renaming and adding CheckerProcess * Iteration Sync * Iteration V2 * linting * made LogAndReportVersion a go routine * Refactor to Go Routine * Add Context to Go Routine and allow Operation if Lookup to Control Server fails * Handle Unmarshal properly * Linting * Relocate Version Checks * Relocating Version Check and specified default Version for now * Linting Error Prevention * Refuse Startup on outdated Version * Add Startup Check Function * Straighten Logging * Dont force Shutdown if --dev flag is set * Create full Service/Peer Structure for ControlServer * Linting * Straighting Naming * Finish VersionControl Service Layout * Improve Error Handling * Change Listening Address * Move Checker Function * Remove VersionControl Peer * Linting * Linting * Create VersionClient Service * Renaming * Add Version Client to Peer Definitions * Linting and Renaming * Linting * Remove Transport Checks for now * Move to Client Side Flag * Remove check * Linting * Transport Client Version Intro * Adding Version Client to Transport Client * Add missing parameter * Adding Version Check, to set Allowed = true * Set Default to true, testing * Restructuring Code * Uplink Changes * Add more proper Defaults * Renaming of Version struct * Dont pass Service use Pointer * Set Defaults for Versioning Checks * Put HTTP Server in go routine * Add Versioncontrol to Storj-Sim * Testplanet Fixes * Linting * Add Error Handling and new Server Struct * Move Lock slightly * Reduce Race Potentials * Remove unnecessary files * Linting * Add Proper Transport Handling * small fixes * add fence for allowed check * Add Startup Version Check and Service Naming * make errormessage private * Add Comments about VersionedClient * Linting * Remove Checks that refuse outgoing connections * Remove release cmd * Add Release Script * Linting * Update to use correct Values * Move vars private and set minimum default versions for testing builds * Remove VersionedClient * Better Error Handling and naked return removal * Straighten the Regex and string conversion * Change Check to allows testplanet and storj-sim to run without the need to pass an LDFlag * Cosmetic Change to Dashboard * Cleanup Returns and remove commented code * Remove Version Check if no build options are passed in * Pass in Config Values instead of Pointers * Handle missed Error * Update Endpoint URL * Change Type of Release Flag * Add additional Logging * Remove Versions Logging of other Services * minor fixes Change-Id: I5cc04a410ea6b2008d14dffd63eb5f36dd348a8b
2019-04-03 20:13:39 +01:00
func (client *slowTransport) WithObservers(obs ...Observer) Client {
return &slowTransport{client.client.WithObservers(obs...), client.network}
}
// DialOptions returns options such that it will use simulated network parameters
func (network *SimulatedNetwork) DialOptions() []grpc.DialOption {
return []grpc.DialOption{grpc.WithContextDialer(network.GRPCDialContext)}
}
// GRPCDialContext implements DialContext that is suitable for `grpc.WithContextDialer`
func (network *SimulatedNetwork) GRPCDialContext(ctx context.Context, address string) (net.Conn, error) {
timer := time.NewTimer(network.DialLatency)
defer timer.Stop()
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-timer.C:
}
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", address)
if err != nil {
return conn, err
}
if network.BytesPerSecond == 0 {
return conn, err
}
return &simulatedConn{network, conn}, nil
}
// simulatedConn implements slow reading and writing to the connection
//
// This does not handle read deadline and write deadline properly.
type simulatedConn struct {
network *SimulatedNetwork
net.Conn
}
// delay sleeps specified amount of time
func (conn *simulatedConn) delay(actualWait time.Duration, bytes int) {
expectedWait := time.Duration(bytes * int(time.Second) / conn.network.BytesPerSecond.Int())
if actualWait < expectedWait {
time.Sleep(expectedWait - actualWait)
}
}
// Read reads data from the connection.
func (conn *simulatedConn) Read(b []byte) (n int, err error) {
start := time.Now()
n, err = conn.Conn.Read(b)
if err == context.Canceled {
return n, err
}
conn.delay(time.Since(start), n)
return n, err
}
// Write writes data to the connection.
func (conn *simulatedConn) Write(b []byte) (n int, err error) {
start := time.Now()
n, err = conn.Conn.Write(b)
if err == context.Canceled {
return n, err
}
conn.delay(time.Since(start), n)
return n, err
}