mg-transport-core/core/healthcheck/iface.go

88 lines
3.8 KiB
Go
Raw Permalink Normal View History

package healthcheck
var (
2024-06-14 18:10:48 +03:00
// compile-time checks to ensure that implementations are compatible with the interface.
_ = Storage(&SyncMapStorage{})
_ = Counter(&AtomicCounter{})
_ = Processor(CounterProcessor{})
_ = NotifyFunc(DefaultNotifyFunc)
_ = CounterConstructor(NewAtomicCounter)
)
2022-12-27 21:05:24 +03:00
// Storage stores different instances of Counter. Implementation should be goroutine-safe.
type Storage interface {
// Get counter by its ID. The counter will be instantiated automatically if necessary.
// Name here is not used to identify the counter in the storage.
Get(id int, name string) Counter
2022-12-27 21:05:24 +03:00
// Remove counter if it exists.
Remove(id int)
// Process will iterate over counters and call Processor on each of them.
// This method is used to collect counters data & send notifications.
Process(processor Processor)
}
2023-01-09 16:09:17 +03:00
// Counter will count successful and failed requests. Its contents can be used
// to judge if specific entity (e.g. Connection / Account) is not working properly
// (invalid credentials, too many failed requests, etc) and take further action based on the result.
2022-12-27 21:05:24 +03:00
// Implementation should be goroutine-safe.
type Counter interface {
// Name can be used as a more friendly identifier for the counter.
Name() string
// SetName of the counter.
SetName(name string)
2022-12-27 21:05:24 +03:00
// HitSuccess registers successful request. It should automatically clear error state because that state should be
// used only if error is totally unrecoverable.
HitSuccess()
// HitFailure registers failed request.
HitFailure()
// TotalSucceeded returns how many requests were successful.
TotalSucceeded() uint32
// TotalFailed returns how many requests have failed.
TotalFailed() uint32
// Failed will put Counter into failed state with specific error message.
Failed(message string)
// IsFailed returns true if Counter is in failed state.
IsFailed() bool
// Message will return error message if Counter is in failed state.
Message() string
// IsFailureProcessed will return true if current error inside counter has been processed already.
IsFailureProcessed() bool
// FailureProcessed will mark current error inside Counter as processed.
FailureProcessed()
// IsCountersProcessed returns true if counters value has been processed by the checker.
// This can be used if you want to process counter values only once.
IsCountersProcessed() bool
// CountersProcessed will mark current counters value as processed.
CountersProcessed()
// ClearCountersProcessed will set IsCountersProcessed to false.
ClearCountersProcessed()
// FlushCounters will reset request counters if deemed necessary (for example, AtomicCounter will clear counters
// only if their contents are older than provided time period).
// This won't clear IsCountersProcessed flag!
FlushCounters()
}
// Processor is used to check if Counter is in error state and act accordingly.
type Processor interface {
// Process counter data. This method is not goroutine-safe!
Process(id int, counter Counter) bool
2022-12-27 21:05:24 +03:00
}
// NotifyMessageLocalizer is the smallest subset of core.Localizer used in the Processor implementation.
2022-12-27 21:05:24 +03:00
type NotifyMessageLocalizer interface {
SetLocale(locale string)
GetLocalizedTemplateMessage(messageID string, templateData map[string]interface{}) string
2022-12-27 21:05:24 +03:00
}
// NotifyFunc will send notification about error to the system with provided credentials.
// It will send the notification to system admins.
type NotifyFunc func(apiURL, apiKey, msg string) error
2022-12-27 21:05:24 +03:00
2023-01-09 16:09:17 +03:00
// CounterConstructor is used to create counters.
// This way you can implement your own counter and still use default CounterStorage.
type CounterConstructor func(name string) Counter
2022-12-27 21:05:24 +03:00
// ConnectionDataProvider should return the connection credentials and language by counter ID.
// It's best to use account ID as a counter ID to be able to retrieve the necessary data as easy as possible.
type ConnectionDataProvider func(id int) (apiURL, apiKey, lang string, exists bool)