package main // Queue for pending authentication requests import ( "log" "math/rand" "time" "github.com/oklog/ulid" ) type AuthenticationQueue struct { requestMap map[string]*AuthRequest requestChan chan *AuthRequest tokenChan chan *AuthToken } func (q *AuthenticationQueue) QueueRequest(r *AuthRequest) { q.requestChan <- r } // Should be executed by the goroutine that watches // AuthenticationQueue.requestChan to be thread safe func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error { t := time.Unix(1000000, 0) entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) ulidID := ulid.MustNew(ulid.Timestamp(t), entropy) ID := ulidID.String() err := r.SetId(ID) if err != nil { return err } q.requestMap[ID] = r log.Println("Queued request by id: ", ID) return nil } func (q *AuthenticationQueue) startRequestChanListener() { go func() { for { var req *AuthRequest = <-q.requestChan q.queueRequest(req) } }() }