本文整理匯總了Golang中github.com/nu7hatch/gouuid.UUID類的典型用法代碼示例。如果您正苦於以下問題:Golang UUID類的具體用法?Golang UUID怎麽用?Golang UUID使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了UUID類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SaveSession
// Save to DB
func (this *UdpSessionList) SaveSession(sid *uuid.UUID, s *UdpSession) error {
v, ok := gUdpSessions.udpmap[s.Addr.String()]
if ok {
v.Reset(40 * time.Second)
}
return SetDeviceSession(sid.String(), gUdpTimeout, s.String(), s.DeviceId, s.Addr)
}
示例2: NewJob
/*
NewJob creates a new job from the config as well as a job spec. After creating
the job, calling job.Process() will actually perform the work.
*/
func NewJob(cfg *Config, spec *Spec, req *http.Request) *Job {
var idUUID *gouuid.UUID
var err error
idUUID, err = gouuid.NewV4()
if TestMode {
idUUID, err = gouuid.NewV5(gouuid.NamespaceURL, []byte("0"))
}
if err != nil {
cfg.Logger.WithField("error", err).Error("error creating uuid")
}
id := idUUID.String()
bobfile := spec.Bobfile
if bobfile == "" {
bobfile = defaultBobfile
}
ret := &Job{
Bobfile: bobfile,
ID: id,
Account: spec.RepoOwner,
GitHubAPIToken: spec.GitHubAPIToken,
Ref: spec.GitRef,
Repo: spec.RepoName,
Workdir: cfg.Workdir,
InfoRoute: "/jobs/" + id,
LogRoute: "/jobs/" + id + "/tail?n=" + defaultTail,
logDir: cfg.Workdir + "/" + id,
Status: "created",
Created: time.Now(),
}
ret.addHostToRoutes(req)
out, file, err := newMultiWriter(ret.logDir)
if err != nil {
cfg.Logger.WithField("error", err).Error("error creating log dir")
id = ""
}
ret.logFile = file
ret.Logger = &logrus.Logger{
Formatter: cfg.Logger.Formatter,
Level: cfg.Logger.Level,
Out: out,
}
if ret.GitHubAPIToken == "" {
ret.GitHubAPIToken = cfg.GitHubAPIToken
}
if id != "" {
jobs[id] = ret
}
return ret
}
示例3: GetSession
// Get existed session from DB
func (this *UdpSessionList) GetSession(sid *uuid.UUID) (*UdpSession, error) {
data, err := GetDeviceSession(sid.String())
if err != nil {
return nil, err
}
s := &UdpSession{
Addr: &net.UDPAddr{},
}
err = s.FromString(data)
if err != nil {
return nil, err
}
s.Sid = sid.String()
return s, nil
}
示例4: InsertSession
func (sr *SessionDbRow) InsertSession() (csi *ClearSessionId, err error) {
var u4 *uuid.UUID
u4, err = uuid.NewV4()
if err != nil {
return nil, err
}
csi = new(ClearSessionId)
saltBytes := makeSalt()
csi.Salt = hex.EncodeToString(saltBytes)
csi.SessionToken = u4.String()
sessionIdCrypt, err := csi.EncryptToken()
if err != nil {
err = fmt.Errorf("Failed to encrypt SessionToken: %s", err)
return nil, err
}
insertSql := fmt.Sprintf(`
INSERT INTO session.session (
SessionId,
SysUserId,
start_dt,
expires_dt,
IpAddr,
UserAgent)
VALUES ($1, $2, now(), now() + interval '%d second', $3, $4)`, Conf.SessionTimeout)
_, err = Conf.DatabaseHandle.Exec(insertSql, sessionIdCrypt, sr.SysUserId,
sr.IpAddr, sr.UserAgent)
if err != nil {
glog.Errorf("Insert of session row failed: %s", err)
return nil, err
}
return csi, nil
}
示例5:
response := httptest.NewRecorder()
h.ServeHTTP(response, req)
Expect(response.Header().Get("X-CF-RequestID")).To(Equal(id.String()))
})
It("should use an empty request ID if generating a new one fails", func() {
instrumented_handler.GenerateUuid = func() (u *uuid.UUID, err error) {
return nil, errors.New("test error")
}
h.ServeHTTP(httptest.NewRecorder(), req)
Expect(req.Header.Get("X-CF-RequestID")).To(Equal("00000000-0000-0000-0000-000000000000"))
})
})
Describe("event emission", func() {
var requestId *uuid.UUID
BeforeEach(func() {
requestId, _ = uuid.NewV4()
req.Header.Set("X-CF-RequestID", requestId.String())
})
Context("without an application ID or instanceIndex", func() {
BeforeEach(func() {
h.ServeHTTP(httptest.NewRecorder(), req)
})
It("should emit a start event with the right origin", func() {
Expect(fakeEmitter.GetMessages()[0].Event).To(BeAssignableToTypeOf(new(events.HttpStart)))
Expect(fakeEmitter.GetMessages()[0].Origin).To(Equal("testHandler/41"))
})
示例6:
package factories_test
import (
uuid "github.com/nu7hatch/gouuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"code.google.com/p/gogoprotobuf/proto"
"github.com/cloudfoundry-incubator/dropsonde/events"
"github.com/cloudfoundry-incubator/dropsonde/factories"
"net/http"
)
var _ = Describe("HTTP event creation", func() {
var applicationId *uuid.UUID
var requestId *uuid.UUID
var req *http.Request
BeforeEach(func() {
applicationId, _ = uuid.NewV4()
requestId, _ = uuid.NewV4()
req, _ = http.NewRequest("GET", "http://foo.example.com/", nil)
req.RemoteAddr = "127.0.0.1"
req.Header.Set("User-Agent", "our-testing-client")
})
Describe("NewHttpStart", func() {
Context("without an application ID or instanceIndex", func() {
示例7:
It("defaults to an empty request ID", func() {
rt.RoundTrip(req)
Expect(req.Header.Get("X-CF-RequestID")).To(Equal("00000000-0000-0000-0000-000000000000"))
})
})
})
Context("event emission", func() {
It("should emit a start event", func() {
rt.RoundTrip(req)
Expect(fakeEmitter.Messages[0].Event).To(BeAssignableToTypeOf(new(events.HttpStart)))
Expect(fakeEmitter.Messages[0].Origin).To(Equal("testRoundtripper/42"))
})
Context("if request ID already exists", func() {
var existingRequestId *uuid.UUID
BeforeEach(func() {
existingRequestId, _ = uuid.NewV4()
req.Header.Set("X-CF-RequestID", existingRequestId.String())
})
It("should emit the existing request ID as the parent request ID", func() {
rt.RoundTrip(req)
startEvent := fakeEmitter.Messages[0].Event.(*events.HttpStart)
Expect(startEvent.GetParentRequestId()).To(Equal(factories.NewUUID(existingRequestId)))
})
})
Context("if round tripper returns an error", func() {
It("should emit a stop event with blank response fields", func() {
示例8: handle
func (h *Handler) handle(t *UdpMsg) error {
// TODO need decryption later
mlen := len(t.Msg)
if mlen < FrameHeaderLen {
return fmt.Errorf("[protocol] invalid message length for device proxy,reason: mlen < FrameHeaderLen | %v < %v.", mlen, FrameHeaderLen)
}
// check opcode
op := (0x7 & t.Msg[0])
if op != 0x2 && op != 0x3 {
return fmt.Errorf("[protocol] reason: wrong opcode, op!=2&&op!=3, op=", op)
}
if op == 0x2 {
if mlen < FrameHeaderLen+DataHeaderLen {
return fmt.Errorf("[protocol] invalid message length for protocol, mlen < FrameHeaderLen+DataHeaderLen ,%v < %v", mlen, FrameHeaderLen+DataHeaderLen)
}
packNum := binary.LittleEndian.Uint16(t.Msg[2:4])
bodyLen := int(binary.LittleEndian.Uint16(t.Msg[FrameHeaderLen+6:]))
// discard msg if found checking error
if t.Msg[FrameHeaderLen+8] != msgs.ChecksumHeader(t.Msg, FrameHeaderLen+8) {
return fmt.Errorf("checksum header error %v!=%v", t.Msg[FrameHeaderLen+8], msgs.ChecksumHeader(t.Msg, FrameHeaderLen+8))
}
// check body
if t.Msg[FrameHeaderLen+9] != msgs.ChecksumHeader(t.Msg[FrameHeaderLen+10:], 2+bodyLen) {
return fmt.Errorf("checksum body error %v!=%v", t.Msg[FrameHeaderLen+9], msgs.ChecksumHeader(t.Msg[FrameHeaderLen+10:], 2+bodyLen))
}
var (
sess *UdpSession
sid *uuid.UUID
err error
locker Locker
body []byte
bodyIndex int
)
bodyIndex = FrameHeaderLen + DataHeaderLen
if bodyLen != len(t.Msg[bodyIndex:]) {
return fmt.Errorf("wrong body length in data header: %d != %d", bodyLen, len(t.Msg[bodyIndex:]))
}
body = t.Msg[bodyIndex : bodyIndex+bodyLen]
// parse data(udp)
// 28 = FrameHeaderLen + 4
c := binary.LittleEndian.Uint16(t.Msg[28:30])
if c == CmdSyncState {
glog.Infoln("執行設備狀態同步")
GMsgBusManager.Push2Bus(0, nil, t.Msg)
return nil
} else if c == 0x31 {
glog.Infoln("執行報警信息同步")
GMsgBusManager.Push2Bus(0, nil, t.Msg)
return nil
//TODO alarm msg synchronization
} else if c != CmdGetToken {
sid, err = uuid.Parse(t.Msg[bodyIndex : bodyIndex+16])
if err != nil {
return fmt.Errorf("parse session id error: %v", err)
}
locker = NewDeviceSessionLocker(sid.String())
err = locker.Lock()
if err != nil {
return fmt.Errorf("lock session id [%s] failed: %v", sid, err)
}
sess, err = gUdpSessions.GetSession(sid)
if err != nil {
locker.Unlock()
return fmt.Errorf("cmd: %X, sid: [%v], error: %v", c, sid, err)
}
err = sess.VerifySession(packNum)
if err != nil {
locker.Unlock()
return fmt.Errorf("cmd: %X, verify session error: %v", c, err)
}
}
output := make([]byte, bodyIndex)
// copy same packNum into this ACK response
copy(output[:bodyIndex], t.Msg[:bodyIndex])
var res []byte
switch c {
case CmdGetToken:
t.CmdType = c
res, err = h.onGetToken(t, body)
case CmdRegister:
t.CmdType = c
t.Url = h.kApiUrls[c]
res, err = h.onRegister(t, sess, body)
case CmdLogin:
t.CmdType = c
t.Url = h.kApiUrls[c]
res, err = h.onLogin(t, sess, body)
case CmdRename:
//.........這裏部分代碼省略.........