...

Source file src/github.com/golang/groupcache/peers.go

Documentation: github.com/golang/groupcache

     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  	"context"
    23  
    24  	pb "github.com/golang/groupcache/groupcachepb"
    25  )
    26  
    27  // Context is an alias to context.Context for backwards compatibility purposes.
    28  type Context = context.Context
    29  
    30  // ProtoGetter is the interface that must be implemented by a peer.
    31  type ProtoGetter interface {
    32  	Get(ctx 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(groupName string) PeerPicker
    51  )
    52  
    53  // RegisterPeerPicker registers the peer initialization function.
    54  // It is called once, when the first group is created.
    55  // Either RegisterPeerPicker or RegisterPerGroupPeerPicker should be
    56  // called exactly once, but not both.
    57  func RegisterPeerPicker(fn func() PeerPicker) {
    58  	if portPicker != nil {
    59  		panic("RegisterPeerPicker called more than once")
    60  	}
    61  	portPicker = func(_ string) PeerPicker { return fn() }
    62  }
    63  
    64  // RegisterPerGroupPeerPicker registers the peer initialization function,
    65  // which takes the groupName, to be used in choosing a PeerPicker.
    66  // It is called once, when the first group is created.
    67  // Either RegisterPeerPicker or RegisterPerGroupPeerPicker should be
    68  // called exactly once, but not both.
    69  func RegisterPerGroupPeerPicker(fn func(groupName string) PeerPicker) {
    70  	if portPicker != nil {
    71  		panic("RegisterPeerPicker called more than once")
    72  	}
    73  	portPicker = fn
    74  }
    75  
    76  func getPeers(groupName string) PeerPicker {
    77  	if portPicker == nil {
    78  		return NoPeers{}
    79  	}
    80  	pk := portPicker(groupName)
    81  	if pk == nil {
    82  		pk = NoPeers{}
    83  	}
    84  	return pk
    85  }
    86  

View as plain text