@@ -33,14 +33,6 @@ const (
33
33
AllMethodsHook = ""
34
34
)
35
35
36
- type contextKey string
37
-
38
- var (
39
- contextKeyQuery = contextKey ("query" )
40
- contextKeyResponse = contextKey ("response" )
41
- contextKeyLogEntry = contextKey ("log-entry" )
42
- )
43
-
44
36
type HTTPRequester interface {
45
37
Do (req * http.Request ) (res * http.Response , err error )
46
38
}
@@ -50,38 +42,13 @@ type HTTPRequester interface {
50
42
// Hooks can modify both query and response, as well as perform additional queries via supplied Caller.
51
43
// If nil is returned instead of *jsonrpc.RPCResponse, original response is returned.
52
44
type Hook func (* Caller , context.Context ) (* jsonrpc.RPCResponse , error )
45
+
53
46
type hookEntry struct {
54
47
method string
55
48
function Hook
56
49
name string
57
50
}
58
51
59
- func AttachToContext (ctx context.Context , query * Query ) context.Context {
60
- return context .WithValue (ctx , contextKeyQuery , query )
61
- }
62
-
63
- func GetFromContext (ctx context.Context ) * Query {
64
- return ctx .Value (contextKeyQuery ).(* Query )
65
- }
66
-
67
- func WithResponse (ctx context.Context , response * jsonrpc.RPCResponse ) context.Context {
68
- return context .WithValue (ctx , contextKeyResponse , response )
69
- }
70
-
71
- func GetResponse (ctx context.Context ) * jsonrpc.RPCResponse {
72
- return ctx .Value (contextKeyResponse ).(* jsonrpc.RPCResponse )
73
- }
74
-
75
- func WithLogEntry (ctx context.Context , entry * logrus.Entry ) context.Context {
76
- return context .WithValue (ctx , contextKeyLogEntry , entry )
77
- }
78
-
79
- // WithLogField injects additional data into default post-query log entry
80
- func WithLogField (ctx context.Context , key string , value interface {}) {
81
- e := ctx .Value (contextKeyLogEntry ).(* logrus.Entry )
82
- e .Data [key ] = value
83
- }
84
-
85
52
// Caller patches through JSON-RPC requests from clients, doing pre/post-processing,
86
53
// account processing and validation.
87
54
type Caller struct {
@@ -159,8 +126,10 @@ func (c *Caller) addDefaultHooks() {
159
126
c .AddPreflightHook (MethodGet , preflightHookGet , builtinHookName )
160
127
c .AddPreflightHook (MethodClaimSearch , preflightHookClaimSearch , builtinHookName )
161
128
162
- c .AddPostflightHook (MethodClaimSearch , postClaimSearchArfleetThumbs , builtinHookName )
163
- c .AddPostflightHook (MethodResolve , postResolveArfleetThumbs , builtinHookName )
129
+ if config .GetArfleetEnabled () {
130
+ c .AddPostflightHook (MethodClaimSearch , postClaimSearchArfleetThumbs , builtinHookName )
131
+ c .AddPostflightHook (MethodResolve , postResolveArfleetThumbs , builtinHookName )
132
+ }
164
133
}
165
134
166
135
func (c * Caller ) CloneWithoutHook (endpoint , method , name string ) * Caller {
@@ -187,6 +156,20 @@ func (c *Caller) Endpoint() string {
187
156
// Call method forwards a JSON-RPC request to the lbrynet server.
188
157
// It returns a response that is ready to be sent back to the JSON-RPC client as is.
189
158
func (c * Caller ) Call (ctx context.Context , req * jsonrpc.RPCRequest ) (* jsonrpc.RPCResponse , error ) {
159
+ origin := OriginFromContext (ctx )
160
+ metrics .ProxyCallCounter .WithLabelValues (req .Method , c .Endpoint (), origin ).Inc ()
161
+ res , err := c .call (ctx , req )
162
+ metrics .ProxyCallDurations .WithLabelValues (req .Method , c .Endpoint (), origin ).Observe (c .Duration )
163
+ if err != nil {
164
+ metrics .ProxyCallFailedDurations .WithLabelValues (req .Method , c .Endpoint (), origin , metrics .FailureKindNet ).Observe (c .Duration )
165
+ metrics .ProxyCallFailedCounter .WithLabelValues (req .Method , c .Endpoint (), origin , metrics .FailureKindNet ).Inc ()
166
+ }
167
+ return res , err
168
+ }
169
+
170
+ // Call method forwards a JSON-RPC request to the lbrynet server.
171
+ // It returns a response that is ready to be sent back to the JSON-RPC client as is.
172
+ func (c * Caller ) call (ctx context.Context , req * jsonrpc.RPCRequest ) (* jsonrpc.RPCResponse , error ) {
190
173
if c .endpoint == "" {
191
174
return nil , errors .Err ("cannot call blank endpoint" )
192
175
}
@@ -203,7 +186,7 @@ func (c *Caller) Call(ctx context.Context, req *jsonrpc.RPCRequest) (*jsonrpc.RP
203
186
204
187
// Applying preflight hooks
205
188
var res * jsonrpc.RPCResponse
206
- ctx = AttachToContext (ctx , q )
189
+ ctx = AttachQuery (ctx , q )
207
190
for _ , hook := range c .preflightHooks {
208
191
if isMatchingHook (q .Method (), hook ) {
209
192
res , err = hook .function (c , ctx )
@@ -251,7 +234,7 @@ func (c *Caller) SendQuery(ctx context.Context, q *Query) (*jsonrpc.RPCResponse,
251
234
start := time .Now ()
252
235
client := c .getRPCClient (q .Method ())
253
236
r , err = client .CallRaw (q .Request )
254
- c .Duration = time .Since (start ).Seconds ()
237
+ c .Duration + = time .Since (start ).Seconds ()
255
238
logger .Log ().Debugf ("sent request: %s %+v (%.2fs)" , q .Method (), q .Params (), c .Duration )
256
239
257
240
// Generally a HTTP transport failure (connect error etc)
@@ -303,8 +286,8 @@ func (c *Caller) SendQuery(ctx context.Context, q *Query) (*jsonrpc.RPCResponse,
303
286
304
287
// Applying postflight hooks
305
288
var hookResp * jsonrpc.RPCResponse
306
- ctx = WithLogEntry (ctx , logEntry )
307
- ctx = WithResponse (ctx , r )
289
+ ctx = AttachLogEntry (ctx , logEntry )
290
+ ctx = AttachResponse (ctx , r )
308
291
for _ , hook := range c .postflightHooks {
309
292
if isMatchingHook (q .Method (), hook ) {
310
293
hookResp , err = hook .function (c , ctx )
0 commit comments