...

Source file src/google.golang.org/grpc/internal/grpcrand/grpcrand.go

Documentation: google.golang.org/grpc/internal/grpcrand

     1  //go:build !go1.21
     2  
     3  // TODO: when this file is deleted (after Go 1.20 support is dropped), delete
     4  // all of grpcrand and call the rand package directly.
     5  
     6  /*
     7   *
     8   * Copyright 2018 gRPC authors.
     9   *
    10   * Licensed under the Apache License, Version 2.0 (the "License");
    11   * you may not use this file except in compliance with the License.
    12   * You may obtain a copy of the License at
    13   *
    14   *     http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   * Unless required by applicable law or agreed to in writing, software
    17   * distributed under the License is distributed on an "AS IS" BASIS,
    18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   * See the License for the specific language governing permissions and
    20   * limitations under the License.
    21   *
    22   */
    23  
    24  // Package grpcrand implements math/rand functions in a concurrent-safe way
    25  // with a global random source, independent of math/rand's global source.
    26  package grpcrand
    27  
    28  import (
    29  	"math/rand"
    30  	"sync"
    31  	"time"
    32  )
    33  
    34  var (
    35  	r  = rand.New(rand.NewSource(time.Now().UnixNano()))
    36  	mu sync.Mutex
    37  )
    38  
    39  // Int implements rand.Int on the grpcrand global source.
    40  func Int() int {
    41  	mu.Lock()
    42  	defer mu.Unlock()
    43  	return r.Int()
    44  }
    45  
    46  // Int63n implements rand.Int63n on the grpcrand global source.
    47  func Int63n(n int64) int64 {
    48  	mu.Lock()
    49  	defer mu.Unlock()
    50  	return r.Int63n(n)
    51  }
    52  
    53  // Intn implements rand.Intn on the grpcrand global source.
    54  func Intn(n int) int {
    55  	mu.Lock()
    56  	defer mu.Unlock()
    57  	return r.Intn(n)
    58  }
    59  
    60  // Int31n implements rand.Int31n on the grpcrand global source.
    61  func Int31n(n int32) int32 {
    62  	mu.Lock()
    63  	defer mu.Unlock()
    64  	return r.Int31n(n)
    65  }
    66  
    67  // Float64 implements rand.Float64 on the grpcrand global source.
    68  func Float64() float64 {
    69  	mu.Lock()
    70  	defer mu.Unlock()
    71  	return r.Float64()
    72  }
    73  
    74  // Uint64 implements rand.Uint64 on the grpcrand global source.
    75  func Uint64() uint64 {
    76  	mu.Lock()
    77  	defer mu.Unlock()
    78  	return r.Uint64()
    79  }
    80  
    81  // Uint32 implements rand.Uint32 on the grpcrand global source.
    82  func Uint32() uint32 {
    83  	mu.Lock()
    84  	defer mu.Unlock()
    85  	return r.Uint32()
    86  }
    87  
    88  // ExpFloat64 implements rand.ExpFloat64 on the grpcrand global source.
    89  func ExpFloat64() float64 {
    90  	mu.Lock()
    91  	defer mu.Unlock()
    92  	return r.ExpFloat64()
    93  }
    94  
    95  // Shuffle implements rand.Shuffle on the grpcrand global source.
    96  var Shuffle = func(n int, f func(int, int)) {
    97  	mu.Lock()
    98  	defer mu.Unlock()
    99  	r.Shuffle(n, f)
   100  }
   101  

View as plain text