go.uber.org/yarpc@v1.72.1/peer/randpeer/random.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package randpeer
    22  
    23  import (
    24  	"math/rand"
    25  	"sync"
    26  	"time"
    27  
    28  	"go.uber.org/yarpc/api/peer"
    29  	"go.uber.org/yarpc/api/transport"
    30  	"go.uber.org/yarpc/peer/abstractlist"
    31  )
    32  
    33  type randomList struct {
    34  	subscribers []*subscriber
    35  	random      *rand.Rand
    36  
    37  	m sync.RWMutex
    38  }
    39  
    40  // Option configures the peer list implementation constructor.
    41  type Option interface {
    42  	apply(*options)
    43  }
    44  
    45  type options struct{}
    46  
    47  // NewImplementation creates a new random abstractlist.Implementation.
    48  //
    49  // Use this constructor instead of NewList, when wanting to do custom peer
    50  // connection management.
    51  func NewImplementation(opts ...Option) abstractlist.Implementation {
    52  	return newRandomList(10, rand.NewSource(time.Now().UnixNano()))
    53  }
    54  
    55  func newRandomList(cap int, source rand.Source) *randomList {
    56  	return &randomList{
    57  		subscribers: make([]*subscriber, 0, cap),
    58  		random:      rand.New(source),
    59  	}
    60  }
    61  
    62  func (r *randomList) Add(peer peer.StatusPeer, _ peer.Identifier) abstractlist.Subscriber {
    63  	r.m.Lock()
    64  	defer r.m.Unlock()
    65  
    66  	index := len(r.subscribers)
    67  	r.subscribers = append(r.subscribers, &subscriber{
    68  		index: index,
    69  		peer:  peer,
    70  	})
    71  	return r.subscribers[index]
    72  }
    73  
    74  func (r *randomList) Remove(peer peer.StatusPeer, _ peer.Identifier, ps abstractlist.Subscriber) {
    75  	r.m.Lock()
    76  	defer r.m.Unlock()
    77  
    78  	sub, ok := ps.(*subscriber)
    79  	if !ok || len(r.subscribers) == 0 {
    80  		return
    81  	}
    82  	index := sub.index
    83  	last := len(r.subscribers) - 1
    84  	r.subscribers[index] = r.subscribers[last]
    85  	r.subscribers[index].index = index
    86  	r.subscribers = r.subscribers[0:last]
    87  }
    88  
    89  func (r *randomList) Choose(_ *transport.Request) peer.StatusPeer {
    90  	// Usage of a wite lock because r.random.Intn is not thread safe
    91  	// see: https://golang.org/pkg/math/rand/
    92  	r.m.Lock()
    93  	defer r.m.Unlock()
    94  
    95  	if len(r.subscribers) == 0 {
    96  		return nil
    97  	}
    98  	index := r.random.Intn(len(r.subscribers))
    99  	return r.subscribers[index].peer
   100  }
   101  
   102  type subscriber struct {
   103  	index int
   104  	peer  peer.StatusPeer
   105  }
   106  
   107  var _ abstractlist.Subscriber = (*subscriber)(nil)
   108  
   109  func (*subscriber) UpdatePendingRequestCount(int) {}