Debian Codegraph @

localservices/hook/service.go

raw symbols SCIP
1 package hook
2
3 import (
4 "runtime"
5 "strings"
6 )
7
8 // TestService is the root object for the test service
9 type TestService struct {
10 ServiceControl
11 // Hooks to run when specified control points are reached in the service business logic.
12 ControlHooks map[string]ControlProcessor
13 }
14
15 // ControlProcessor defines a function that is run when a specified control point is reached in the service
16 // business logic. The function receives the service instance so internal state can be inspected, plus for any
17 // arguments passed to the currently executing service function.
18 type ControlProcessor func(sc ServiceControl, args ...interface{}) error
19
20 // ControlHookCleanup defines a function used to remove a control hook.
21 type ControlHookCleanup func()
22
23 // ServiceControl instances allow hooks to be registered for execution at the specified point of execution.
24 // The control point name can be a function name or a logical execution point meaningful to the service.
25 // If name is "", the hook for the currently executing function is executed.
26 // Returns a function which can be used to remove the hook.
27 type ServiceControl interface {
28 RegisterControlPoint(name string, controller ControlProcessor) ControlHookCleanup
29 }
30
31 // currentServiceMethodName returns the method executing on the service when ProcessControlHook was invoked.
32 func (s *TestService) currentServiceMethodName() string {
33 pc, _, _, ok := runtime.Caller(2)
34 if !ok {
35 panic("current method name cannot be found")
36 }
37 return unqualifiedMethodName(pc)
38 }
39
40 func unqualifiedMethodName(pc uintptr) string {
41 f := runtime.FuncForPC(pc)
42 fullName := f.Name()
43 nameParts := strings.Split(fullName, ".")
44 return nameParts[len(nameParts)-1]
45 }
46
47 // ProcessControlHook retrieves the ControlProcessor for the specified hook name and runs it, returning any error.
48 // Use it like this to invoke a hook registered for some arbitrary control point:
49 // if err := n.ProcessControlHook("foobar", <serviceinstance>, <somearg1>, <somearg2>); err != nil {
50 // return err
51 // }
52 func (s *TestService) ProcessControlHook(hookName string, sc ServiceControl, args ...interface{}) error {
53 if s.ControlHooks == nil {
54 return nil
55 }
56 if hook, ok := s.ControlHooks[hookName]; ok {
57 return hook(sc, args...)
58 }
59 return nil
60 }
61
62 // ProcessFunctionHook runs the ControlProcessor for the current function, returning any error.
63 // Use it like this:
64 // if err := n.ProcessFunctionHook(<serviceinstance>, <somearg1>, <somearg2>); err != nil {
65 // return err
66 // }
67 func (s *TestService) ProcessFunctionHook(sc ServiceControl, args ...interface{}) error {
68 hookName := s.currentServiceMethodName()
69 return s.ProcessControlHook(hookName, sc, args...)
70 }
71
72 // RegisterControlPoint assigns the specified controller to the named hook. If nil, any existing controller for the
73 // hook is removed.
74 // hookName is the name of a function on the service or some arbitrarily named control point.
75 func (s *TestService) RegisterControlPoint(hookName string, controller ControlProcessor) ControlHookCleanup {
76 if s.ControlHooks == nil {
77 s.ControlHooks = make(map[string]ControlProcessor)
78 }
79 if controller == nil {
80 delete(s.ControlHooks, hookName)
81 } else {
82 s.ControlHooks[hookName] = controller
83 }
84 return func() {
85 s.RegisterControlPoint(hookName, nil)
86 }
87 }