github.com/uber/kraken@v0.1.4/core/peer_context.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package core
    15  
    16  import "errors"
    17  
    18  // PeerContext defines the context a peer runs within, namely the fields which
    19  // are used to identify each peer.
    20  type PeerContext struct {
    21  
    22  	// IP and Port specify the address the peer will announce itself as. Note,
    23  	// this is distinct from the address a peer's Scheduler will listen on
    24  	// because the peer may be running within a container and the address it
    25  	// listens on is mapped to a different ip/port outside of the container.
    26  	IP   string `json:"ip"`
    27  	Port int    `json:"port"`
    28  
    29  	// PeerID the peer will identify itself as.
    30  	PeerID PeerID `json:"peer_id"`
    31  
    32  	// Zone is the zone the peer is running within.
    33  	Zone string `json:"zone"`
    34  
    35  	// Cluster is the Kraken cluster the peer is running within.
    36  	Cluster string `json:"cluster"`
    37  
    38  	// Origin indicates whether the peer is an origin server or not.
    39  	Origin bool `json:"origin"`
    40  }
    41  
    42  // NewPeerContext creates a new PeerContext.
    43  func NewPeerContext(
    44  	f PeerIDFactory, zone, cluster, ip string, port int, origin bool) (PeerContext, error) {
    45  
    46  	if ip == "" {
    47  		return PeerContext{}, errors.New("no ip supplied")
    48  	}
    49  	if port == 0 {
    50  		return PeerContext{}, errors.New("no port supplied")
    51  	}
    52  	peerID, err := f.GeneratePeerID(ip, port)
    53  	if err != nil {
    54  		return PeerContext{}, err
    55  	}
    56  	return PeerContext{
    57  		IP:      ip,
    58  		Port:    port,
    59  		PeerID:  peerID,
    60  		Zone:    zone,
    61  		Cluster: cluster,
    62  		Origin:  origin,
    63  	}, nil
    64  }