Skip to content

Commit

Permalink
fix: improve healthcheck uuid generation (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoorn authored Nov 20, 2019
1 parent 6aa28d5 commit f8019a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/nightlyone/lockfile v0.0.0-20180618180623-0ad87eef1443
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/rdoorn/gorule v0.0.0-20191111095408-9e3c1ea80fdf
github.com/rdoorn/hashstructure v0.0.0-20180705160145-6d677f823801
github.com/rdoorn/tinyresolver v0.0.0-20191008124230-0320b16ed71e
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/rdoorn/gorule v0.0.0-20191028142911-03d8545710ac h1:zTaw7QuheXZblxw+R
github.com/rdoorn/gorule v0.0.0-20191028142911-03d8545710ac/go.mod h1:owXaf99rseXLsWD1z35/+BuB5lgviqWbHBvqq2iu8Zw=
github.com/rdoorn/gorule v0.0.0-20191111095408-9e3c1ea80fdf h1:FSLOC7rdaq0yB29bZihFhaWy8/sTjfJBSjEbePhIDnY=
github.com/rdoorn/gorule v0.0.0-20191111095408-9e3c1ea80fdf/go.mod h1:owXaf99rseXLsWD1z35/+BuB5lgviqWbHBvqq2iu8Zw=
github.com/rdoorn/hashstructure v0.0.0-20180705160145-6d677f823801 h1:jYdx94sLwqrSs8p5QE3lY9BimtDdt8C4f4DT2DMnkJQ=
github.com/rdoorn/hashstructure v0.0.0-20180705160145-6d677f823801/go.mod h1:Y7tOAbJVyxLVjqfIqMeFXaKzktjnOfQmQpDQovKCx/A=
github.com/rdoorn/tinyresolver v0.0.0-20191008124230-0320b16ed71e h1:SVUnFxi1fF4xDlxIKZfWkySl3c7msnZ12Ikt9F1Mues=
github.com/rdoorn/tinyresolver v0.0.0-20191008124230-0320b16ed71e/go.mod h1:ZhcbeBaKiUfDkR2pj6uQBC2h71D+Hc5PEQKTWTU1sZQ=
github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down
15 changes: 10 additions & 5 deletions pkg/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package healthcheck

import (
"crypto/sha256"
"encoding/json"
"fmt"
"sort"
"sync"

"github.com/rdoorn/hashstructure"
"github.com/schubergphilis/mercury/pkg/logging"
"github.com/schubergphilis/mercury/pkg/tlsconfig"
)
Expand Down Expand Up @@ -60,7 +59,7 @@ type HealthCheck struct {
Port int `json:"port" toml:"port"` // specific port
OnlineState StatusType `json:"online_state" toml:"online_state"` // alternative online_state - default: online / optional: offline / maintenance
OfflineState StatusType `json:"offline_state" toml:"offline_state"` // alternative offline_state - default: offline
uuidStr string
uuidStr string `hash:"ignore"`
}

// UUID returns a uuid of a healthcheck
Expand All @@ -69,11 +68,17 @@ func (h HealthCheck) UUID() string {
return h.uuidStr
}

sort.Strings(h.HTTPHeaders)
hash, err := hashstructure.Hash(h, nil)
if err != nil {
panic(fmt.Sprintf("unable to hash structure in health check: %v", h))
}
h.uuidStr = fmt.Sprintf("%x", hash)

/*sort.Strings(h.HTTPHeaders)
s := fmt.Sprintf("%s%s%s%s%s%v%d%s%d%d%s%t%s%s", h.Type, h.TCPRequest, h.TCPReply, h.HTTPRequest, h.HTTPPostData, h.HTTPHeaders, h.HTTPStatus, h.HTTPReply, h.Interval, h.Timeout, h.ActivePassiveID, h.DisableAutoCheck, h.OfflineState, h.OnlineState)
t := sha256.New()
t.Write([]byte(s))
h.uuidStr = fmt.Sprintf("%x", t.Sum(nil))
h.uuidStr = fmt.Sprintf("%x", t.Sum(nil))*/
return h.uuidStr
}

Expand Down

0 comments on commit f8019a4

Please sign in to comment.