1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: attempt to quickfix hash collisions

This commit is contained in:
Ferdinand Mütsch 2021-01-31 18:06:20 +01:00
parent dc1a0c7983
commit 0c3f3b37b0
2 changed files with 22 additions and 5 deletions

View File

@ -67,6 +67,24 @@ func (h *Heartbeat) GetKey(t uint8) (key string) {
return key return key
} }
func (h *Heartbeat) String() string {
return fmt.Sprintf(
"Heartbeat {user=%s, entity=%s, type=%s, category=%s, project=%s, branch=%s, language=%s, iswrite=%v, editor=%s, os=%s, machine=%s, time=%d}",
h.UserID,
h.Entity,
h.Type,
h.Category,
h.Project,
h.Branch,
h.Language,
h.IsWrite,
h.Editor,
h.OperatingSystem,
h.Machine,
(time.Time(h.Time)).UnixNano(),
)
}
// Hash is used to prevent duplicate heartbeats // Hash is used to prevent duplicate heartbeats
// Using a UNIQUE INDEX over all relevant columns would be more straightforward, // Using a UNIQUE INDEX over all relevant columns would be more straightforward,
// whereas manually computing this kind of hash is quite cumbersome. However, // whereas manually computing this kind of hash is quite cumbersome. However,
@ -74,7 +92,7 @@ func (h *Heartbeat) GetKey(t uint8) (key string) {
// essentially double the space required for heartbeats, so we decided to go this way. // essentially double the space required for heartbeats, so we decided to go this way.
func (h *Heartbeat) Hashed() *Heartbeat { func (h *Heartbeat) Hashed() *Heartbeat {
hash, err := hashstructure.Hash(h, hashstructure.FormatV2, nil) hash, err := hashstructure.Hash(h.String(), hashstructure.FormatV2, nil)
if err != nil { if err != nil {
logbuch.Error("CRITICAL ERROR: failed to hash struct %v", err) logbuch.Error("CRITICAL ERROR: failed to hash struct %v", err)
} }

View File

@ -2,13 +2,12 @@ package routes
import ( import (
"encoding/json" "encoding/json"
"github.com/emvi/logbuch"
"github.com/gorilla/mux" "github.com/gorilla/mux"
conf "github.com/muety/wakapi/config" conf "github.com/muety/wakapi/config"
"net/http"
"os"
"github.com/muety/wakapi/services" "github.com/muety/wakapi/services"
"github.com/muety/wakapi/utils" "github.com/muety/wakapi/utils"
"net/http"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
) )
@ -68,7 +67,7 @@ func (h *HeartbeatHandler) ApiPost(w http.ResponseWriter, r *http.Request) {
if err := h.heartbeatSrvc.InsertBatch(heartbeats); err != nil { if err := h.heartbeatSrvc.InsertBatch(heartbeats); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
os.Stderr.WriteString(err.Error()) logbuch.Error(err.Error())
return return
} }