master
go 68 lines 1.66 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package rethinkdb
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/netdata/netdata/go/plugins/pkg/funcapi"
10 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
11 )
12
13 // funcRouter routes method calls to appropriate function handlers.
14 type funcRouter struct {
15 collector *Collector
16
17 handlers map[string]funcapi.MethodHandler
18 }
19
20 func newFuncRouter(c *Collector) *funcRouter {
21 r := &funcRouter{
22 collector: c,
23 handlers: make(map[string]funcapi.MethodHandler),
24 }
25 r.handlers[runningQueriesMethodID] = newFuncRunningQueries(r)
26 return r
27 }
28
29 // Compile-time interface check.
30 var _ funcapi.MethodHandler = (*funcRouter)(nil)
31
32 func (r *funcRouter) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) {
33 if h, ok := r.handlers[method]; ok {
34 return h.MethodParams(ctx, method)
35 }
36 return nil, fmt.Errorf("unknown method: %s", method)
37 }
38
39 func (r *funcRouter) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse {
40 if r.collector.rdb == nil {
41 return funcapi.UnavailableResponse("collector is still initializing, please retry in a few seconds")
42 }
43
44 if h, ok := r.handlers[method]; ok {
45 return h.Handle(ctx, method, params)
46 }
47 return funcapi.NotFoundResponse(method)
48 }
49
50 func (r *funcRouter) Cleanup(ctx context.Context) {
51 for _, h := range r.handlers {
52 h.Cleanup(ctx)
53 }
54 }
55
56 func rethinkdbMethods() []funcapi.MethodConfig {
57 return []funcapi.MethodConfig{
58 runningQueriesMethodConfig(),
59 }
60 }
61
62 func rethinkdbFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler {
63 c, ok := job.Collector().(*Collector)
64 if !ok {
65 return nil
66 }
67 return c.funcRouter
68 }