-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. | ||
// This software is licensed under a 3-clause BSD license. Please consult the | ||
// LICENSE.md file distributed with the sources of this project regarding your | ||
// rights to use or distribute this software. | ||
|
||
package client | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/rand" | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
var ( | ||
objectIdCounter = readRandomUint32() | ||
machineId = readMachineId() | ||
processId = os.Getpid() | ||
) | ||
|
||
func readRandomUint32() uint32 { | ||
var b [4]byte | ||
_, err := io.ReadFull(rand.Reader, b[:]) | ||
if err != nil { | ||
panic(fmt.Errorf("cannot read random object id: %v", err)) | ||
} | ||
return uint32((uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)) | ||
} | ||
|
||
func readMachineId() []byte { | ||
var sum [3]byte | ||
id := sum[:] | ||
hostname, err1 := os.Hostname() | ||
if err1 != nil { | ||
_, err2 := io.ReadFull(rand.Reader, id) | ||
if err2 != nil { | ||
panic(fmt.Errorf("cannot get hostname: %v; %v", err1, err2)) | ||
} | ||
return id | ||
} | ||
hw := md5.New() | ||
hw.Write([]byte(hostname)) | ||
copy(id, hw.Sum(nil)) | ||
return id | ||
} | ||
|
||
func newObjectID() string { | ||
var b [12]byte | ||
// Timestamp, 4 bytes, big endian | ||
binary.BigEndian.PutUint32(b[:], uint32(time.Now().Unix())) | ||
// Machine, first 3 bytes of md5(hostname) | ||
b[4] = machineId[0] | ||
b[5] = machineId[1] | ||
b[6] = machineId[2] | ||
// Pid, 2 bytes, specs don't specify endianness, but we use big endian. | ||
b[7] = byte(processId >> 8) | ||
b[8] = byte(processId) | ||
// Increment, 3 bytes, big endian | ||
i := atomic.AddUint32(&objectIdCounter, 1) | ||
b[9] = byte(i >> 16) | ||
b[10] = byte(i >> 8) | ||
b[11] = byte(i) | ||
return string(b[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is= | ||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= | ||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= | ||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= | ||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= | ||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= | ||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/sylabs/json-resp v0.5.0 h1:AWdKu6aS0WrkkltX1M0ex0lENrIcx5TISox902s2L2M= | ||
github.com/sylabs/json-resp v0.5.0/go.mod h1:anCzED2SGHHZQDubMuoVtwMuJZdpqQ+7iso8yDFm/nQ= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= |