...

Source file src/go.etcd.io/etcd/client/v3/experimental/recipes/queue.go

Documentation: go.etcd.io/etcd/client/v3/experimental/recipes

     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  
    15  package recipe
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.etcd.io/etcd/api/v3/mvccpb"
    21  	v3 "go.etcd.io/etcd/client/v3"
    22  )
    23  
    24  // Queue implements a multi-reader, multi-writer distributed queue.
    25  type Queue struct {
    26  	client *v3.Client
    27  	ctx    context.Context
    28  
    29  	keyPrefix string
    30  }
    31  
    32  func NewQueue(client *v3.Client, keyPrefix string) *Queue {
    33  	return &Queue{client, context.TODO(), keyPrefix}
    34  }
    35  
    36  func (q *Queue) Enqueue(val string) error {
    37  	_, err := newUniqueKV(q.client, q.keyPrefix, val)
    38  	return err
    39  }
    40  
    41  // Dequeue returns Enqueue()'d elements in FIFO order. If the
    42  // queue is empty, Dequeue blocks until elements are available.
    43  func (q *Queue) Dequeue() (string, error) {
    44  	// TODO: fewer round trips by fetching more than one key
    45  	resp, err := q.client.Get(q.ctx, q.keyPrefix, v3.WithFirstRev()...)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  
    50  	kv, err := claimFirstKey(q.client, resp.Kvs)
    51  	if err != nil {
    52  		return "", err
    53  	} else if kv != nil {
    54  		return string(kv.Value), nil
    55  	} else if resp.More {
    56  		// missed some items, retry to read in more
    57  		return q.Dequeue()
    58  	}
    59  
    60  	// nothing yet; wait on elements
    61  	ev, err := WaitPrefixEvents(
    62  		q.client,
    63  		q.keyPrefix,
    64  		resp.Header.Revision,
    65  		[]mvccpb.Event_EventType{mvccpb.PUT})
    66  	if err != nil {
    67  		return "", err
    68  	}
    69  
    70  	ok, err := deleteRevKey(q.client, string(ev.Kv.Key), ev.Kv.ModRevision)
    71  	if err != nil {
    72  		return "", err
    73  	} else if !ok {
    74  		return q.Dequeue()
    75  	}
    76  	return string(ev.Kv.Value), err
    77  }
    78  

View as plain text