@@ -101,17 +101,17 @@ func (rb *redisBackend) GetTicket(ctx context.Context, id string) (*pb.Ticket, e
101101
102102 // if a ticket is assigned, its given an TTL and automatically de-indexed
103103 if ticket .Assignment == nil {
104- ticketTTL := getTicketReleaseTimeout (rb .cfg )
105- expiry := time .Now ().Add (ticketTTL ).UnixNano ()
106-
107- expiredKey , err := rb .checkIfExpired (ctx , id )
104+ expiredKey , err := rb .checkIfExpired (ctx , redisConn , id )
108105 if err != nil {
109106 err = errors .Wrapf (err , "failed to check if ticket is expired, id: %s" , id )
110107 return nil , err
111108 }
112109
110+ ticketTTL := getTicketReleaseTimeout (rb .cfg )
111+ expiresAt := time .Now ().Add (ticketTTL ).UnixNano ()
112+
113113 if ! expiredKey {
114- _ , err = redisConn .Do ("ZADD" , allTicketsWithTTL , "XX" , "CH" , expiry , id )
114+ _ , err = redisConn .Do ("ZADD" , allTicketsWithTTL , "XX" , "CH" , expiresAt , id )
115115 if err != nil {
116116 err = errors .Wrapf (err , "failed to update score for ticket id: %s" , id )
117117 return nil , status .Errorf (codes .Internal , "%v" , err )
@@ -122,54 +122,9 @@ func (rb *redisBackend) GetTicket(ctx context.Context, id string) (*pb.Ticket, e
122122 return ticket , nil
123123}
124124
125- func (rb * redisBackend ) UpdateIndexedTicketTTL (ctx context.Context , ticketId string ) error {
126- m := rb .NewMutex (ticketId )
125+ func (rb * redisBackend ) checkIfExpired (ctx context.Context , redisConn redis.Conn , id string ) (bool , error ) {
127126 // TODO: should we add a timeout to this acquisition
128- err := m .Lock (ctx )
129- if err != nil {
130- return err
131- }
132-
133- defer func () {
134- if _ , err = m .Unlock (context .Background ()); err != nil {
135- logger .WithError (err ).Error ("error on mutex unlock" )
136- }
137- }()
138-
139- redisConn , err := rb .redisPool .GetContext (ctx )
140- if err != nil {
141- return status .Errorf (codes .Unavailable , "checkIfExpired, id: %s, failed to connect to redis: %v" , ticketId , err )
142- }
143- defer handleConnectionClose (& redisConn )
144-
145- score , err := redis .Float64 (redisConn .Do ("ZSCORE" , allTicketsWithTTL , ticketId ))
146- if errors .Is (err , redis .ErrNil ) {
147- logger .WithError (err ).Errorf ("Ticket id: %s not found" , ticketId )
148- return nil
149- }
150- if err != nil {
151- return err
152- }
153-
154- if score <= float64 (time .Now ().UnixNano ()) {
155- return errors .New ("ticket is already expired" )
156- }
157-
158- ticketTTL := getTicketReleaseTimeout (rb .cfg )
159- expiry := time .Now ().Add (ticketTTL ).UnixNano ()
160-
161- _ , err = redisConn .Do ("ZADD" , allTicketsWithTTL , "XX" , "CH" , expiry , ticketId )
162- if err != nil {
163- err = errors .Wrapf (err , "failed to update score for ticket id: %s" , ticketId )
164- return status .Errorf (codes .Internal , "%v" , err )
165- }
166-
167- return nil
168- }
169-
170- func (rb * redisBackend ) checkIfExpired (ctx context.Context , id string ) (bool , error ) {
171127 m := rb .NewMutex (id )
172- // TODO: should we add a timeout to this acquisition
173128 err := m .Lock (ctx )
174129 if err != nil {
175130 return false , err
@@ -181,12 +136,6 @@ func (rb *redisBackend) checkIfExpired(ctx context.Context, id string) (bool, er
181136 }
182137 }()
183138
184- redisConn , err := rb .redisPool .GetContext (ctx )
185- if err != nil {
186- return false , status .Errorf (codes .Unavailable , "checkIfExpired, id: %s, failed to connect to redis: %v" , id , err )
187- }
188- defer handleConnectionClose (& redisConn )
189-
190139 score , err := redis .Float64 (redisConn .Do ("ZSCORE" , allTicketsWithTTL , id ))
191140 if errors .Is (err , redis .ErrNil ) {
192141 logger .WithError (err ).Errorf ("Ticket id: %s not found" , id )
@@ -459,25 +408,27 @@ func (rb *redisBackend) GetTickets(ctx context.Context, ids []string) ([]*pb.Tic
459408 }
460409 r = append (r , t )
461410
462- if t .Assignment == nil {
463- // if a ticket is assigned, its given an TTL and automatically de-indexed
464- expiredKey , err := rb .checkIfExpired (ctx , t .Id )
465- if err != nil {
466- err = errors .Wrapf (err , "failed to check ticket expiry, %v" , err )
467- return nil , status .Errorf (codes .Internal , "%v" , err )
468- }
469-
470- if expiredKey {
471- continue
472- }
473-
474- expiry := time .Now ().Add (ticketTTL ).UnixNano ()
475- // update the indexed ticket's ttl. careful this might become slow.
476- err = redisConn .Send ("ZADD" , allTicketsWithTTL , "XX" , "CH" , expiry , t .Id )
477- if err != nil {
478- err = errors .Wrapf (err , "failed to update ttl for ticket id: %s" , t .Id )
479- return nil , status .Errorf (codes .Internal , "%v" , err )
480- }
411+ if t .Assignment != nil {
412+ continue
413+ }
414+
415+ // if a ticket is assigned, its given an TTL and automatically de-indexed
416+ expiredKey , err := rb .checkIfExpired (ctx , redisConn , t .Id )
417+ if err != nil {
418+ err = errors .Wrapf (err , "failed to check ticket expiry, %v" , err )
419+ return nil , status .Errorf (codes .Internal , "%v" , err )
420+ }
421+
422+ if expiredKey {
423+ continue
424+ }
425+
426+ expiry := time .Now ().Add (ticketTTL ).UnixNano ()
427+ // update the indexed ticket's ttl. careful this might become slow.
428+ err = redisConn .Send ("ZADD" , allTicketsWithTTL , "XX" , "CH" , expiry , t .Id )
429+ if err != nil {
430+ err = errors .Wrapf (err , "failed to update ttl for ticket id: %s" , t .Id )
431+ return nil , status .Errorf (codes .Internal , "%v" , err )
481432 }
482433 }
483434 }
0 commit comments