Files
threshold/internal/bsr/summary.go
T
Elim Tsiagbey fd3091d8dc feat: Decode Summary File (#3408)
* feat: Decode Summary File

- Add support for decoding BSR session, connection and channel summary file
- Introduce base summary interfaces that other protocols can extend
- Add new SSH register allocation function for getting SSH Channel Summary Type
2023-07-13 10:25:01 -04:00

52 lines
1.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package bsr
import (
"context"
"fmt"
)
// SummaryAllocFunc is a function that returns a summary type
type SummaryAllocFunc func(ctx context.Context) Summary
// summaryAllocFuncRegistry mappings of protocols and container type
// for each SummaryAllocFunc
type summaryAllocFuncRegistry map[Protocol]map[ContainerType]SummaryAllocFunc
func (r summaryAllocFuncRegistry) get(p Protocol, c ContainerType) (SummaryAllocFunc, bool) {
protocol, ok := r[p]
if !ok {
return nil, false
}
af, ok := protocol[c]
return af, ok
}
var summaryAllocFuncs summaryAllocFuncRegistry
// RegisterSummaryAllocFunc registers a SummaryAllocFunc for the given Protocol.
// A given Protocol and Container can only have one SummaryAllocFunc function
// registered.
func RegisterSummaryAllocFunc(p Protocol, c ContainerType, af SummaryAllocFunc) error {
const op = "bsr.RegisterSummaryAllocFunc"
if summaryAllocFuncs == nil {
summaryAllocFuncs = make(map[Protocol]map[ContainerType]SummaryAllocFunc)
}
protocol, ok := summaryAllocFuncs[p]
if !ok {
protocol = make(map[ContainerType]SummaryAllocFunc)
}
_, ok = protocol[c]
if ok {
return fmt.Errorf("%s: %s protocol with %s container: %w", op, p, c, ErrAlreadyRegistered)
}
protocol[c] = af
summaryAllocFuncs[p] = protocol
return nil
}