-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
161 lines (135 loc) · 6.71 KB
/
interfaces.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package servicemesh
import (
"io"
"log/slog"
"sync"
ee "github.com/gravestench/eventemitter"
)
// Mesh is the abstract idea of the service mesh, an interface.
//
// The Mesh interface defines the operations that can be performed with
// services, such as adding, removing, and retrieving services. It acts as a
// container for services and uses other interfaces like HasDependencies to
// work with them and do things automatically on their behalf.
type Mesh interface {
// Add a single service to the Mesh.
Add(Service) *sync.WaitGroup
// Remove a specific service from the Mesh.
Remove(Service) *sync.WaitGroup
// Services returns a pointer to a slice of Services currently managed by
// the service Mesh **which are ready to be used**.
Services() []Service
Events() *ee.EventEmitter
Run()
Shutdown() *sync.WaitGroup
slogLoggerMethods
}
type slogLoggerMethods interface {
SetLogHandler(handler slog.Handler)
SetLogLevel(level slog.Level)
SetLogDestination(dst io.Writer)
}
// Service represents a generic service within a service mesh.
//
// The Service interface defines the contract that all services in the
// service mesh must adhere to. It provides methods for initializing the service and
// retrieving its name.
type Service interface {
// Init initializes the service and establishes a connection to the
// service Mesh.
Init(mesh Mesh)
// Name returns the name of the service.
Name() string
}
// HasDependencies represents a service that can resolve its dependencies.
//
// The HasDependencies interface extends the Service interface and adds
// methods for managing dependencies. It allows services to declare whether
// their dependencies are resolved, as well as a method that attempts to resolve
// those dependencies with the given service mesh.
//
// The mesh will use this interface automatically when a service is added.
// You do not need to implement this interface, it is optional. You would want
// to do this when you have services that depend upon each other to operate
type HasDependencies interface {
Service
// DependenciesResolved returns true if all dependencies are resolved. This
// is up to the service.
DependenciesResolved() bool
// ResolveDependencies attempts to resolve the dependencies of the
// service using the provided Mesh.
ResolveDependencies(services []Service)
}
// HasLogger is an interface for services that require a logger instance.
//
// The HasLogger interface represents components that depend on a logger for
// logging purposes. It defines a method to set the logger instance.
type HasLogger interface {
Service
// UseLogger sets the logger instance for the component.
SetLogger(l *slog.Logger)
// Logger yields the logger instance for the component.
Logger() *slog.Logger
}
// HasGracefulShutdown is an interface for services that require graceful shutdown handling.
//
// The HasGracefulShutdown interface extends the Service interface and adds
// a method for performing custom actions during graceful shutdown.
type HasGracefulShutdown interface {
Service
// OnShutdown is called during the graceful shutdown process to perform
// custom actions before the service is stopped.
OnShutdown()
}
// EventHandlerServiceAdded is an optional interface. If implemented, it will automatically bind to the
// "Service Added" service mesh event, allowing the handler to respond when a new service is added.
type EventHandlerServiceAdded interface {
OnServiceAdded(service Service)
}
// EventHandlerServiceRemoved is an optional interface. If implemented, it will automatically bind to the
// "Service Removed" service mesh event, enabling the implementor to respond when a service is removed.
type EventHandlerServiceRemoved interface {
OnServiceRemoved(service Service)
}
// EventHandlerServiceInitialized is an optional interface. If implemented, it will automatically bind to the
// "Service Initialized" service mesh event, enabling the implementor to respond when a service is initialized.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerServiceInitialized interface {
OnServiceInitialized(service Service)
}
// EventHandlerServiceEventsBound is an optional interface. If implemented, it will automatically bind to the
// "Service Events Bound" service mesh event, enabling the implementor to respond when events are bound to a service.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerServiceEventsBound interface {
OnServiceEventsBound(service Service)
}
// EventHandlerServiceLoggerBound is an optional interface. If implemented, it will automatically bind to the
// "Service Logger Bound" service mesh event, enabling the implementor to respond when a logger is bound to a service.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerServiceLoggerBound interface {
OnServiceLoggerBound(service Service)
}
// EventHandlerServiceMeshRunLoopInitiated is an optional interface. If implemented, it will automatically bind to the
// "mesh Run Loop Initiated" service mesh event, enabling the implementor to respond when the service mesh run loop is initiated.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerServiceMeshRunLoopInitiated interface {
OnServiceMeshRunLoopInitiated()
}
// EventHandlerServiceMeshShutdownInitiated is an optional interface. If implemented, it will automatically bind to the
// "mesh Shutdown Initiated" service mesh event, enabling the implementor to respond when the service mesh is preparing to shut down.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerServiceMeshShutdownInitiated interface {
OnServiceMeshShutdownInitiated()
}
// EventHandlerDependencyResolutionStarted is an optional interface. If implemented, it will automatically bind to the
// "Dependency Resolution Started" service mesh event, enabling the implementor to respond when dependency resolution starts.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerDependencyResolutionStarted interface {
OnDependencyResolutionStarted(service Service)
}
// EventHandlerDependencyResolutionEnded is an optional interface. If implemented, it will automatically bind to the
// "Dependency Resolution Ended" service mesh event, enabling the implementor to respond when dependency resolution ends.
// When the event is emitted, the declared method will be called and passed the arguments from the emitter.
type EventHandlerDependencyResolutionEnded interface {
OnDependencyResolutionEnded(service Service)
}