-
Notifications
You must be signed in to change notification settings - Fork 78
/
ticketfactory.go
41 lines (31 loc) · 936 Bytes
/
ticketfactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package client
import (
"sync/atomic"
ticketpb2 "github.com/deephaven/deephaven-core/go/internal/proto/ticket"
)
//todo doc eveything
type ticketFactory struct {
id int32
}
func newTicketFactory() *ticketFactory {
return &ticketFactory{}
}
// newTicketNum returns a new ticket number that has not been used before.
func (tf *ticketFactory) nextId() int32 {
nextTicket := atomic.AddInt32(&tf.id, 1)
if nextTicket <= 0 {
// If you ever see this panic... what are you doing?
panic("out of tickets")
}
return nextTicket
}
// newTicket returns a new ticket that has not used before.
func (tf *ticketFactory) newTicket() ticketpb2.Ticket {
id := tf.nextId()
return tf.makeTicket(id)
}
// makeTicket turns a ticket ID into a ticket.
func (tf *ticketFactory) makeTicket(id int32) ticketpb2.Ticket {
bytes := []byte{'e', byte(id), byte(id >> 8), byte(id >> 16), byte(id >> 24)}
return ticketpb2.Ticket{Ticket: bytes}
}