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

@@ -74,9 +74,15 @@ func init() {
func main() { func main() {
defer r.Close() defer r.Close()
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port) instanceID, err := GetInstanceID()
if err := app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port)); err != nil { if err != nil {
log.Fatal(err)
}
log.Printf("Listening on %s:%d\n", conf.Host, conf.Port+instanceID)
if err := app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port+instanceID)); err != nil {
log.Fatalf("failed to start server: %v", err) log.Fatalf("failed to start server: %v", err)
} }
} }

View File

@@ -6,7 +6,9 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@@ -124,6 +126,21 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
return result[0], uint16(port), nil 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. // PointerOf returns a pointer of the argument passed.
func PointerOf[T any](v T) *T { func PointerOf[T any](v T) *T {
return &v return &v