create result manager

This commit is contained in:
GLYASAI 2021-08-05 16:38:27 +08:00
parent 8ef96b59e8
commit 6103b522fa
3 changed files with 122 additions and 1 deletions

View File

@ -19,7 +19,11 @@ func (s *staticEndpoint) GetComponent() *v1alpha1.ThirdComponent {
}
func (s *staticEndpoint) Discover(ctx context.Context, update chan *v1alpha1.ThirdComponent) ([]*v1alpha1.ThirdComponentEndpointStatus, error) {
return nil, nil
endpointStatues, err := s.DiscoverOne(ctx)
if err != nil {
return nil, err
}
return endpointStatues, nil
}
func (s *staticEndpoint) DiscoverOne(ctx context.Context) ([]*v1alpha1.ThirdComponentEndpointStatus, error) {

View File

@ -0,0 +1,91 @@
package results
import (
"sync"
)
// Manager provides a probe results cache and channel of updates.
type Manager interface {
// Get returns the cached result for the endpoint.
Get(endpointID string) (Result, bool)
// Set sets the cached result for the endpoint.
Set(endpointID string, result Result)
// Remove clears the cached result for the endpoint.
Remove(endpointID string)
}
// Result is the type for probe results.
type Result int
const (
// Unknown is encoded as -1 (type Result)
Unknown Result = iota - 1
// Success is encoded as 0 (type Result)
Success
// Failure is encoded as 1 (type Result)
Failure
)
func (r Result) String() string {
switch r {
case Success:
return "Success"
case Failure:
return "Failure"
default:
return "UNKNOWN"
}
}
// ToPrometheusType translates a Result to a form which is better understood by prometheus.
func (r Result) ToPrometheusType() float64 {
switch r {
case Success:
return 0
case Failure:
return 1
default:
return -1
}
}
// Manager implementation.
type manager struct {
// guards the cache
sync.RWMutex
// map of endpoint ID -> probe Result
cache map[string]Result
}
var _ Manager = &manager{}
// NewManager creates and returns an empty results manager.
func NewManager() Manager {
return &manager{
cache: make(map[string]Result),
}
}
func (m *manager) Get(id string) (Result, bool) {
m.RLock()
defer m.RUnlock()
result, found := m.cache[id]
return result, found
}
func (m *manager) Set(id string, result Result) {
m.Lock()
defer m.Unlock()
prev, exists := m.cache[id]
if !exists || prev != result {
m.cache[id] = result
}
}
func (m *manager) Remove(id string) {
m.Lock()
defer m.Unlock()
delete(m.cache, id)
}

View File

@ -0,0 +1,26 @@
package results
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCacheOperations(t *testing.T) {
m := NewManager()
unsetID := "unset"
setID := "set"
_, found := m.Get(unsetID)
assert.False(t, found, "unset result found")
m.Set(setID, Success)
result, found := m.Get(setID)
assert.True(t, result == Success, "set result")
assert.True(t, found, "set result found")
m.Remove(setID)
_, found = m.Get(setID)
assert.False(t, found, "removed result found")
}