diff options
Diffstat (limited to 'src/gobang.go')
-rw-r--r-- | src/gobang.go | 124 |
1 files changed, 5 insertions, 119 deletions
diff --git a/src/gobang.go b/src/gobang.go index c8ab0f3..73540d2 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "crypto/sha256" "encoding/binary" - "encoding/hex" "errors" "fmt" "hash" @@ -21,7 +20,8 @@ import ( "strings" "sync" "syscall" - "time" + + "guuid" ) @@ -36,15 +36,6 @@ const ( ) -const ( - uuidDashCount = 4 - uuidByteCount = 16 - uuidEncodedLength = (uuidByteCount * 2) + uuidDashCount -) -type UUID struct { - bytes [uuidByteCount]byte -} - type Gauge struct { Inc func(...any) Dec func(...any) @@ -413,111 +404,6 @@ func Hash(password []byte, salt []byte) []byte{ return hash } -// getV7Time returns the time in milliseconds and nanoseconds / 256. -// The returned (milli << (12 + seq)) is guaranteed to be greater than -// (milli << (12 + seq)) returned by any previous call to getV7Time. -// `seq` Sequence number is between 0 and 3906 (nanoPerMilli >> 8) -func getV7Time(nano int64) (int64, int64) { - const nanoPerMilli = 1000 * 1000 - - milli := nano / nanoPerMilli - seq := (nano - (milli * nanoPerMilli)) >> 8 - now := milli << (12 + seq) - - timeMutex.Lock() - defer timeMutex.Unlock() - if now <= lastV7Time { - now = lastV7Time + 1 - milli = now >> 12 - seq = now & 0xfff - } - lastV7Time = now - return milli, seq -} - -func newUUIDFrom(randomBuffer [uuidByteCount]byte, now int64) UUID { - randomBuffer[6] = (randomBuffer[6] & 0x0f) | 0x40 // Version 4 - randomBuffer[8] = (randomBuffer[8] & 0x3f) | 0x80 // Variant is 10 - - t, s := getV7Time(now) - - randomBuffer[0] = byte(t >> 40) - randomBuffer[1] = byte(t >> 32) - randomBuffer[2] = byte(t >> 24) - randomBuffer[3] = byte(t >> 16) - randomBuffer[4] = byte(t >> 8) - randomBuffer[5] = byte(t >> 0) - - randomBuffer[6] = 0x70 | (0x0f & byte(s >> 8)) - randomBuffer[7] = byte(s) - return UUID { bytes: randomBuffer } -} - -func NewUUID() UUID { - var buffer [uuidByteCount]byte - _, err := io.ReadFull(rand.Reader, buffer[7:]) - FatalIf(err) - - now := time.Now().UnixNano() - return newUUIDFrom(buffer, now) -} - -func (uuid UUID) String() string { - dst := [uuidEncodedLength]byte { - 0, 0, 0, 0, - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - '-', - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0, 0, - } - - hex.Encode(dst[ 0:8], uuid.bytes[0:4]) - hex.Encode(dst[ 9:13], uuid.bytes[4:6]) - hex.Encode(dst[14:18], uuid.bytes[6:8]) - hex.Encode(dst[19:23], uuid.bytes[8:10]) - hex.Encode(dst[24:36], uuid.bytes[10:]) - - return string(dst[:]) -} - -var ( - dashIndexes = []int { 8, 13, 18, 23 } - emptyUUID = UUID {} - badUUIDLengthError = errors.New("str isn't of the correct length") - badUUIDDashCountError = errors.New("Bad count of dashes in string") - badUUIDDashPositionError = errors.New("Bad char in string") -) -func ParseUUID(str string) (UUID, error) { - if len(str) != uuidEncodedLength { - return emptyUUID, badUUIDLengthError - } - - if strings.Count(str, "-") != uuidDashCount { - return emptyUUID, badUUIDDashCountError - } - - for _, idx := range dashIndexes { - if str[idx] != '-' { - return emptyUUID, badUUIDDashPositionError - } - } - - hexstr := strings.Join(strings.Split(str, "-"), "") - data, err := hex.DecodeString(hexstr) - if err != nil { - return emptyUUID, err - } - - return UUID { bytes: [uuidByteCount]byte(data) }, nil -} - func sourceInfo(skip int) slog.Attr { pc := make([]uintptr, 10) n := runtime.Callers(skip, pc) @@ -543,7 +429,7 @@ func sourceInfo(skip int) slog.Attr { func logArgs(type_ string) []string { return []string { - "id", NewUUID().String(), + "id", guuid.New().String(), "kind", "log", "type", type_, } @@ -626,7 +512,7 @@ func metric(type_ string, label string, args ...any) { "_", slices.Concat( []any { - "id", NewUUID().String(), + "id", guuid.New().String(), "kind", "metric", "type", type_, "label", label, @@ -763,7 +649,7 @@ func SetLoggerOutput(w io.Writer, args ...any) { "info", "pid", os.Getpid(), "ppid", os.Getppid(), - "puuid", NewUUID().String(), + "puuid", guuid.New().String(), ), ).With(args...)) } |