From f6ee7d4732c897bef170565e38ee5de582c81801 Mon Sep 17 00:00:00 2001 From: Raman Khatri Date: Tue, 11 Apr 2023 06:08:35 -0400 Subject: [PATCH] fix: handle the case when the gateway suffix (in HA mode) ends up being a number (#16) --- config/config_test.go | 6 ++++++ config/misc.go | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 9d859e12..4cbcb1bd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -328,4 +328,10 @@ func Test_Misc(t *testing.T) { t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs") require.Equal(t, "", GetInstanceID()) + + t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs-gw-10") + require.Equal(t, "10", GetInstanceID()) + + t.Setenv("INSTANCE_ID", "prousmtusmt-v0-rs-gw-ha-12-234234-10") + require.Equal(t, "12", GetInstanceID()) } diff --git a/config/misc.go b/config/misc.go index 354a52fc..e1935d84 100644 --- a/config/misc.go +++ b/config/misc.go @@ -2,10 +2,15 @@ package config import ( "os" - "strconv" + "regexp" "strings" ) +var ( + regexGwHa = regexp.MustCompile(`^.*-gw-ha-\d+-\w+-\w+$`) + regexGwNonHaOrProcessor = regexp.MustCompile(`^.*-\d+$`) +) + // GetWorkspaceToken returns the workspace token provided in the environment variables // Env variable CONFIG_BACKEND_TOKEN is deprecating soon // WORKSPACE_TOKEN is newly introduced. This will override CONFIG_BACKEND_TOKEN @@ -38,16 +43,10 @@ func GetInstanceID() string { // This handles 2 kinds of server instances // a) Processor OR Gateway running in non HA mod where the instance name ends with the index // b) Gateway running in HA mode, where the instance name is of the form *-gw-ha--- - potentialServerIndexIndices := []int{length - 1, length - 3} - for _, i := range potentialServerIndexIndices { - if i < 0 { - continue - } - serverIndex := instanceArr[i] - _, err := strconv.Atoi(serverIndex) - if err == nil { - return serverIndex - } + if (regexGwHa.MatchString(instance)) && (length > 3) { + return instanceArr[length-3] + } else if (regexGwNonHaOrProcessor.MatchString(instance)) && (length > 1) { + return instanceArr[length-1] } return "" }