core

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

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.

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.

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.

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.

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

View Source
const (
	// DigestEmptyTar is the sha256 digest of an empty tar file.
	DigestEmptyTar = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)
View Source
const (
	// SHA256 is the only algorithm supported.
	SHA256 = "sha256"
)

Variables

View Source
var ErrInvalidPeerIDLength = errors.New("peer id has invalid length")

ErrInvalidPeerIDLength returns when a string peer id does not decode into 20 bytes.

Functions

func NamespaceFixture

func NamespaceFixture() string

NamespaceFixture creates a random namespace.

func PieceHash

func PieceHash() hash.Hash32

PieceHash returns the hash used to sum pieces.

func TagFixture

func TagFixture() string

TagFixture creates a random tag for service repo-bar.

func ValidateSHA256

func ValidateSHA256(s string) error

ValidateSHA256 returns error if s is not a valid SHA256 hex digest.

Types

type BlobFixture

type BlobFixture struct {
	Content  []byte
	Digest   Digest
	MetaInfo *MetaInfo
}

BlobFixture joins all information associated with a blob for testing convenience.

func CustomBlobFixture

func CustomBlobFixture(content []byte, digest Digest, mi *MetaInfo) *BlobFixture

CustomBlobFixture creates a BlobFixture with custom fields.

func NewBlobFixture

func NewBlobFixture() *BlobFixture

NewBlobFixture creates a randomly generated BlobFixture.

func SizedBlobFixture

func SizedBlobFixture(size uint64, pieceLength uint64) *BlobFixture

SizedBlobFixture creates a randomly generated BlobFixture of given size with given piece lengths.

func (*BlobFixture) Info

func (f *BlobFixture) Info() *BlobInfo

Info returns a BlobInfo for f.

func (*BlobFixture) Length

func (f *BlobFixture) Length() int64

Length returns the length of the blob.

type BlobInfo

type BlobInfo struct {
	Size int64
}

BlobInfo contains metadata about a blob.

func NewBlobInfo

func NewBlobInfo(size int64) *BlobInfo

NewBlobInfo creates a new BlobInfo.

type Digest

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

Digest can be represented in a string like "<algorithm>:<hex_digest_string>" Example:

sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

func DigestFixture

func DigestFixture() Digest

DigestFixture returns a random Digest.

func DigestListFixture

func DigestListFixture(n int) []Digest

DigestListFixture returns a list of random Digests.

func NewSHA256DigestFromHex

func NewSHA256DigestFromHex(hex string) (Digest, error)

NewSHA256DigestFromHex constructs a Digest from a sha256 in hexadecimal format. Returns error if hex is not a valid sha256.

func ParseSHA256Digest

func ParseSHA256Digest(raw string) (Digest, error)

ParseSHA256Digest parses a raw "<algo>:<hex>" sha256 digest. Returns error if the algo is not sha256 or the hex is not a valid sha256.

func (Digest) Algo

func (d Digest) Algo() string

Algo returns the algo part of the digest. Example:

sha256

func (Digest) Hex

func (d Digest) Hex() string

Hex returns the hex part of the digest. Example:

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

func (Digest) MarshalJSON

func (d Digest) MarshalJSON() ([]byte, error)

MarshalJSON unmarshals hexBytes to Digest.

func (*Digest) Scan

func (d *Digest) Scan(src interface{}) error

Scan unmarshals []byte to a Digest.

func (Digest) ShardID

func (d Digest) ShardID() string

ShardID returns the shard id of the digest.

func (Digest) String

func (d Digest) String() string

String returns digest in string format like "<algorithm>:<hex_digest_string>".

func (*Digest) UnmarshalJSON

func (d *Digest) UnmarshalJSON(str []byte) error

UnmarshalJSON unmarshals "<algorithm>:<hex_digest_string>" to Digest.

func (Digest) Value

func (d Digest) Value() (driver.Value, error)

Value marshals a digest and returns []byte as driver.Value.

type DigestList

type DigestList []Digest

DigestList is a list of digests.

func (*DigestList) Scan

func (l *DigestList) Scan(src interface{}) error

Scan unmarshals []byte to a list of Digest.

func (DigestList) Value

func (l DigestList) Value() (driver.Value, error)

Value marshals a list of digests and returns []byte as driver.Value.

type Digester

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

Digester calculates the digest of data stream.

func NewDigester

func NewDigester() *Digester

NewDigester instantiates and returns a new Digester object.

func (*Digester) Digest

func (d *Digester) Digest() Digest

Digest returns the digest of existing data.

func (Digester) FromBytes

func (d Digester) FromBytes(p []byte) (Digest, error)

FromBytes digests the input and returns a Digest.

func (Digester) FromReader

func (d Digester) FromReader(rd io.Reader) (Digest, error)

FromReader returns the digest of data from reader.

func (Digester) Tee

func (d Digester) Tee(r io.Reader) io.Reader

Tee allows d to calculate a digest of r while the caller reads from the returned reader.

type InfoHash

type InfoHash [20]byte

InfoHash is 20-byte SHA1 hash of the Info struct. It is the authoritative identifier for a torrent.

func InfoHashFixture

func InfoHashFixture() InfoHash

InfoHashFixture returns a randomly generated InfoHash.

func NewInfoHashFromBytes

func NewInfoHashFromBytes(b []byte) InfoHash

NewInfoHashFromBytes converts raw bytes to an InfoHash.

func NewInfoHashFromHex

func NewInfoHashFromHex(s string) (InfoHash, error)

NewInfoHashFromHex converts a hexidemical string into an InfoHash

func (InfoHash) Bytes

func (h InfoHash) Bytes() []byte

Bytes converts h to raw bytes.

func (InfoHash) Hex

func (h InfoHash) Hex() string

Hex converts h into a hexidemical string.

func (InfoHash) String

func (h InfoHash) String() string

type MetaInfo

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

MetaInfo contains torrent metadata.

func DeserializeMetaInfo

func DeserializeMetaInfo(data []byte) (*MetaInfo, error)

DeserializeMetaInfo reconstructs a MetaInfo from a json blob.

func MetaInfoFixture

func MetaInfoFixture() *MetaInfo

MetaInfoFixture returns a randomly generated MetaInfo.

func NewMetaInfo

func NewMetaInfo(d Digest, blob io.Reader, pieceLength int64) (*MetaInfo, error)

NewMetaInfo creates a new MetaInfo. Assumes that d is the valid digest for blob (re-computing it is expensive).

func (*MetaInfo) Digest

func (mi *MetaInfo) Digest() Digest

Digest returns the digest of the original blob.

func (*MetaInfo) GetPieceLength

func (mi *MetaInfo) GetPieceLength(i int) int64

GetPieceLength returns the length of piece i.

func (*MetaInfo) GetPieceSum

func (mi *MetaInfo) GetPieceSum(i int) uint32

GetPieceSum returns the checksum of piece i. Does not check bounds.

func (*MetaInfo) InfoHash

func (mi *MetaInfo) InfoHash() InfoHash

InfoHash returns the torrent InfoHash.

func (*MetaInfo) Length

func (mi *MetaInfo) Length() int64

Length returns the length of the original blob.

func (*MetaInfo) NumPieces

func (mi *MetaInfo) NumPieces() int

NumPieces returns the number of pieces in the torrent.

func (*MetaInfo) PieceLength

func (mi *MetaInfo) PieceLength() int64

PieceLength returns the piece length used to break up the original blob. Note, the final piece may be shorter than this. Use GetPieceLength for the true lengths of each piece.

func (*MetaInfo) Serialize

func (mi *MetaInfo) Serialize() ([]byte, error)

Serialize converts mi to a json blob.

type PeerContext

type PeerContext struct {

	// IP and Port specify the address the peer will announce itself as. Note,
	// this is distinct from the address a peer's Scheduler will listen on
	// because the peer may be running within a container and the address it
	// listens on is mapped to a different ip/port outside of the container.
	IP   string `json:"ip"`
	Port int    `json:"port"`

	// PeerID the peer will identify itself as.
	PeerID PeerID `json:"peer_id"`

	// Zone is the zone the peer is running within.
	Zone string `json:"zone"`

	// Cluster is the Kraken cluster the peer is running within.
	Cluster string `json:"cluster"`

	// Origin indicates whether the peer is an origin server or not.
	Origin bool `json:"origin"`
}

PeerContext defines the context a peer runs within, namely the fields which are used to identify each peer.

func NewPeerContext

func NewPeerContext(
	f PeerIDFactory, zone, cluster, ip string, port int, origin bool) (PeerContext, error)

NewPeerContext creates a new PeerContext.

func OriginContextFixture

func OriginContextFixture() PeerContext

OriginContextFixture returns a randomly generated origin PeerContext.

func PeerContextFixture

func PeerContextFixture() PeerContext

PeerContextFixture returns a randomly generated PeerContext.

type PeerID

type PeerID [20]byte

PeerID represents a fixed size peer id.

func HashedPeerID

func HashedPeerID(s string) (PeerID, error)

HashedPeerID returns a PeerID derived from the hash of s.

func NewPeerID

func NewPeerID(s string) (PeerID, error)

NewPeerID parses a PeerID from the given string. Must be in hexadecimal notation, encoding exactly 20 bytes.

func PeerIDFixture

func PeerIDFixture() PeerID

PeerIDFixture returns a randomly generated PeerID.

func RandomPeerID

func RandomPeerID() (PeerID, error)

RandomPeerID returns a randomly generated PeerID.

func (PeerID) LessThan

func (p PeerID) LessThan(o PeerID) bool

LessThan returns whether p is less than o.

func (PeerID) String

func (p PeerID) String() string

String encodes the PeerID in hexadecimal notation.

type PeerIDFactory

type PeerIDFactory string

PeerIDFactory defines the method used to generate a peer id.

const AddrHashPeerIDFactory PeerIDFactory = "addr_hash"

AddrHashPeerIDFactory creates peers ids based on a full "ip:port" address.

const RandomPeerIDFactory PeerIDFactory = "random"

RandomPeerIDFactory creates random peer ids.

func (PeerIDFactory) GeneratePeerID

func (f PeerIDFactory) GeneratePeerID(ip string, port int) (PeerID, error)

GeneratePeerID creates a new peer id per the factory policy.

type PeerInfo

type PeerInfo struct {
	PeerID   PeerID `json:"peer_id"`
	IP       string `json:"ip"`
	Port     int    `json:"port"`
	Origin   bool   `json:"origin"`
	Complete bool   `json:"complete"`
}

PeerInfo defines peer metadata scoped to a torrent.

func NewPeerInfo

func NewPeerInfo(
	peerID PeerID,
	ip string,
	port int,
	origin bool,
	complete bool) *PeerInfo

NewPeerInfo creates a new PeerInfo.

func OriginPeerInfoFixture

func OriginPeerInfoFixture() *PeerInfo

OriginPeerInfoFixture returns a randomly generated PeerInfo for an origin.

func PeerInfoFixture

func PeerInfoFixture() *PeerInfo

PeerInfoFixture returns a randomly generated PeerInfo.

func PeerInfoFromContext

func PeerInfoFromContext(pctx PeerContext, complete bool) *PeerInfo

PeerInfoFromContext derives PeerInfo from a PeerContext.

func SortedByPeerID

func SortedByPeerID(peers []*PeerInfo) []*PeerInfo

SortedByPeerID returns a copy of peers which has been sorted by peer id.

type PeerInfos

type PeerInfos []*PeerInfo

PeerInfos groups PeerInfo structs for sorting.

func (PeerInfos) Len

func (s PeerInfos) Len() int

Len for sorting.

func (PeerInfos) Swap

func (s PeerInfos) Swap(i, j int)

Swap for sorting

type PeersByPeerID

type PeersByPeerID struct{ PeerInfos }

PeersByPeerID sorts PeerInfos by peer id.

func (PeersByPeerID) Less

func (s PeersByPeerID) Less(i, j int) bool

Less for sorting.

Jump to

Keyboard shortcuts

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