mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-01 09:52:23 -06:00
42 lines
928 B
Go
42 lines
928 B
Go
package generators
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
)
|
|
|
|
// NType is an enum type for the different types of NATS connections
|
|
type NType int
|
|
|
|
// Enum values for NType
|
|
const (
|
|
NTypeBus NType = iota
|
|
NTypeKeyValue
|
|
NTypeRegistry
|
|
)
|
|
|
|
// String returns the string representation of a NType
|
|
func (n NType) String() string {
|
|
return []string{"bus", "kv", "reg"}[n]
|
|
}
|
|
|
|
// GenerateConnectionName generates a connection name for a NATS connection
|
|
// The connection name will be formatted as follows: "hostname:pid:service:type"
|
|
func GenerateConnectionName(service string, ntype NType) string {
|
|
host, err := os.Hostname()
|
|
if err != nil {
|
|
host = ""
|
|
}
|
|
|
|
return firstNRunes(host, 5) + ":" + strconv.Itoa(os.Getpid()) + ":" + service + ":" + ntype.String()
|
|
}
|
|
|
|
// firstNRunes returns the first n runes of a string
|
|
func firstNRunes(s string, n int) string {
|
|
runes := []rune(s)
|
|
if n > len(runes) {
|
|
n = len(runes)
|
|
}
|
|
return string(runes[:n])
|
|
}
|