forked from ethersphere/bee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsctx.go
72 lines (60 loc) · 1.57 KB
/
sctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sctx provides convenience methods for context
// value injection and extraction.
package sctx
import (
"context"
"errors"
"math/big"
)
var (
// ErrTargetPrefix is returned when target prefix decoding fails.
ErrTargetPrefix = errors.New("error decoding prefix string")
)
type (
HTTPRequestIDKey struct{}
requestHostKey struct{}
gasPriceKey struct{}
gasLimitKey struct{}
)
// SetHost sets the http request host in the context
func SetHost(ctx context.Context, domain string) context.Context {
return context.WithValue(ctx, requestHostKey{}, domain)
}
// GetHost gets the request host from the context
func GetHost(ctx context.Context) string {
v, ok := ctx.Value(requestHostKey{}).(string)
if ok {
return v
}
return ""
}
func SetGasLimit(ctx context.Context, limit uint64) context.Context {
return context.WithValue(ctx, gasLimitKey{}, limit)
}
func GetGasLimit(ctx context.Context) uint64 {
v, ok := ctx.Value(gasLimitKey{}).(uint64)
if ok {
return v
}
return 0
}
func GetGasLimitWithDefault(ctx context.Context, defaultLimit uint64) uint64 {
limit := GetGasLimit(ctx)
if limit == 0 {
return defaultLimit
}
return limit
}
func SetGasPrice(ctx context.Context, price *big.Int) context.Context {
return context.WithValue(ctx, gasPriceKey{}, price)
}
func GetGasPrice(ctx context.Context) *big.Int {
v, ok := ctx.Value(gasPriceKey{}).(*big.Int)
if ok {
return v
}
return nil
}