github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/groupcache/peers.go (about)

     1  /*
     2  Copyright 2012 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // peers.go defines how processes find and communicate with their peers.
    18  
    19  package groupcache
    20  
    21  import (
    22  	pb "github.com/insionng/yougam/libraries/golang/groupcache/groupcachepb"
    23  )
    24  
    25  // Context is an opaque value passed through calls to the
    26  // ProtoGetter. It may be nil if your ProtoGetter implementation does
    27  // not require a context.
    28  type Context interface{}
    29  
    30  // ProtoGetter is the interface that must be implemented by a peer.
    31  type ProtoGetter interface {
    32  	Get(context Context, in *pb.GetRequest, out *pb.GetResponse) error
    33  }
    34  
    35  // PeerPicker is the interface that must be implemented to locate
    36  // the peer that owns a specific key.
    37  type PeerPicker interface {
    38  	// PickPeer returns the peer that owns the specific key
    39  	// and true to indicate that a remote peer was nominated.
    40  	// It returns nil, false if the key owner is the current peer.
    41  	PickPeer(key string) (peer ProtoGetter, ok bool)
    42  }
    43  
    44  // NoPeers is an implementation of PeerPicker that never finds a peer.
    45  type NoPeers struct{}
    46  
    47  func (NoPeers) PickPeer(key string) (peer ProtoGetter, ok bool) { return }
    48  
    49  var (
    50  	portPicker func() PeerPicker
    51  )
    52  
    53  // RegisterPeerPicker registers the peer initialization function.
    54  // It is called once, when the first group is created.
    55  func RegisterPeerPicker(fn func() PeerPicker) {
    56  	if portPicker != nil {
    57  		panic("RegisterPeerPicker called more than once")
    58  	}
    59  	portPicker = fn
    60  }
    61  
    62  func getPeers() PeerPicker {
    63  	if portPicker == nil {
    64  		return NoPeers{}
    65  	}
    66  	pk := portPicker()
    67  	if pk == nil {
    68  		pk = NoPeers{}
    69  	}
    70  	return pk
    71  }