Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1098)

Side by Side Diff: go/src/infra/gae/libs/wrapper/mathrand.go

Issue 1151473003: Better attempt at an appengine wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: better typecheck Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package wrapper
6
7 import (
8 "math/rand"
9
10 "golang.org/x/net/context"
11 )
12
13 // MathRandFactory is the function signature for factory methods compatible with
14 // SetMathRandFactory.
15 type MathRandFactory func(context.Context) *rand.Rand
16
17 // GetMathRand gets a *"math/rand".Rand from the context. If one hasn't been
18 // set, this creates a new Rand object with a Source initialized from the
19 // current time accordint to GetTimeNow(c).UnixNano().
20 func GetMathRand(c context.Context) *rand.Rand {
21 if f, ok := c.Value(mathRandKey).(MathRandFactory); ok && f != nil {
22 return f(c)
23 }
24 return rand.New(rand.NewSource(GetTimeNow(c).UnixNano()))
M-A Ruel 2015/05/27 00:27:11 why not return crypto rand?
iannucci 2015/05/27 02:40:57 Doesn't support the same interface (though crypto
25 }
26
27 // SetMathRandFactory sets the function to produce *"math/rand".Rand instances,
28 // as returned by the GetMathRand method.
29 func SetMathRandFactory(c context.Context, mrf MathRandFactory) context.Context {
30 return context.WithValue(c, mathRandKey, mrf)
31 }
32
33 // SetMathRand sets the current *"math/rand".Rand object in the context. Useful
34 // for testing with a quick mock. This is just a shorthand SetMathRandFactory
35 // invocation to set a factory which always returns the same object.
36 func SetMathRand(c context.Context, r *rand.Rand) context.Context {
37 if r == nil {
38 return SetMathRandFactory(c, nil)
39 }
40 return SetMathRandFactory(c, func(context.Context) *rand.Rand { return r })
41 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698