The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/util/xorm/sequence_inmem.go

39 lines
832 B

package xorm
import (
"context"
"fmt"
"math/rand/v2"
"sync"
)
type inMemSequenceGenerator struct {
sequencesMu sync.Mutex
nextValues map[string]int
}
func newInMemSequenceGenerator() *inMemSequenceGenerator {
return &inMemSequenceGenerator{
nextValues: make(map[string]int),
}
}
func (g *inMemSequenceGenerator) Next(_ context.Context, table, column string) (int64, error) {
if table == "migration_log" {
// Don't use sequential IDs for migration log entries, as we don't clean up migration_log table between tests,
// so restarting the sequence can lead to conflicting IDs.
return rand.Int64(), nil
}
key := fmt.Sprintf("%s:%s", table, column)
g.sequencesMu.Lock()
defer g.sequencesMu.Unlock()
seq, ok := g.nextValues[key]
if !ok {
seq = 1
}
g.nextValues[key] = seq + 1
return int64(seq), nil
}