Skip to content

Commit

Permalink
Merge pull request #108 from unistack-org/improve
Browse files Browse the repository at this point in the history
small improve
  • Loading branch information
vtolstov authored Mar 26, 2022
2 parents 3805d0f + 03410c4 commit c10f29e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
34 changes: 34 additions & 0 deletions router/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package router

import (
"context"
)

type routerKey struct{}

// FromContext get router from context
func FromContext(ctx context.Context) (Router, bool) {
if ctx == nil {
return nil, false
}
c, ok := ctx.Value(routerKey{}).(Router)
return c, ok
}

// NewContext put router in context
func NewContext(ctx context.Context, c Router) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, routerKey{}, c)
}

// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}
5 changes: 5 additions & 0 deletions util/jitter/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ func Random(d time.Duration) time.Duration {
v := rng.Float64() * float64(d.Nanoseconds())
return time.Duration(v)
}

func RandomInterval(min, max time.Duration) time.Duration {
var rng rand.Rand
return time.Duration(rng.Int63n(max.Nanoseconds()-min.Nanoseconds())+min.Nanoseconds()) * time.Nanosecond
}

0 comments on commit c10f29e

Please sign in to comment.