queue.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package recipe
  15. import (
  16. "context"
  17. v3 "go.etcd.io/etcd/clientv3"
  18. "go.etcd.io/etcd/mvcc/mvccpb"
  19. )
  20. // Queue implements a multi-reader, multi-writer distributed queue.
  21. type Queue struct {
  22. client *v3.Client
  23. ctx context.Context
  24. keyPrefix string
  25. }
  26. func NewQueue(client *v3.Client, keyPrefix string) *Queue {
  27. return &Queue{client, context.TODO(), keyPrefix}
  28. }
  29. func (q *Queue) Enqueue(val string) error {
  30. _, err := newUniqueKV(q.client, q.keyPrefix, val)
  31. return err
  32. }
  33. // Dequeue returns Enqueue()'d elements in FIFO order. If the
  34. // queue is empty, Dequeue blocks until elements are available.
  35. func (q *Queue) Dequeue() (string, error) {
  36. // TODO: fewer round trips by fetching more than one key
  37. resp, err := q.client.Get(q.ctx, q.keyPrefix, v3.WithFirstRev()...)
  38. if err != nil {
  39. return "", err
  40. }
  41. kv, err := claimFirstKey(q.client, resp.Kvs)
  42. if err != nil {
  43. return "", err
  44. } else if kv != nil {
  45. return string(kv.Value), nil
  46. } else if resp.More {
  47. // missed some items, retry to read in more
  48. return q.Dequeue()
  49. }
  50. // nothing yet; wait on elements
  51. ev, err := WaitPrefixEvents(
  52. q.client,
  53. q.keyPrefix,
  54. resp.Header.Revision,
  55. []mvccpb.Event_EventType{mvccpb.PUT})
  56. if err != nil {
  57. return "", err
  58. }
  59. ok, err := deleteRevKey(q.client, string(ev.Kv.Key), ev.Kv.ModRevision)
  60. if err != nil {
  61. return "", err
  62. } else if !ok {
  63. return q.Dequeue()
  64. }
  65. return string(ev.Kv.Value), err
  66. }