Add INSTANCE_ID env port offset

This commit is contained in:
Jacob Gunther
2023-05-19 14:29:30 -05:00
parent b364846c1b
commit f26460c256
2 changed files with 25 additions and 2 deletions

View File

@@ -6,7 +6,9 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
@@ -124,6 +126,21 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
return result[0], uint16(port), nil
}
// GetInstanceID returns the INSTANCE_ID environment variable parsed as an unsigned 16-bit integer.
func GetInstanceID() (uint16, error) {
if instanceID := os.Getenv("INSTANCE_ID"); len(instanceID) > 0 {
value, err := strconv.ParseUint(instanceID, 10, 16)
if err != nil {
log.Fatal(err)
}
return uint16(value), nil
}
return 0, nil
}
// PointerOf returns a pointer of the argument passed.
func PointerOf[T any](v T) *T {
return &v