...

Source file src/go.etcd.io/etcd/client/v3/experimental/recipes/barrier.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  // Barrier creates a key in etcd to block processes, then deletes the key to
    25  // release all blocked processes.
    26  type Barrier struct {
    27  	client *v3.Client
    28  	ctx    context.Context
    29  
    30  	key string
    31  }
    32  
    33  func NewBarrier(client *v3.Client, key string) *Barrier {
    34  	return &Barrier{client, context.TODO(), key}
    35  }
    36  
    37  // Hold creates the barrier key causing processes to block on Wait.
    38  func (b *Barrier) Hold() error {
    39  	_, err := newKey(b.client, b.key, v3.NoLease)
    40  	return err
    41  }
    42  
    43  // Release deletes the barrier key to unblock all waiting processes.
    44  func (b *Barrier) Release() error {
    45  	_, err := b.client.Delete(b.ctx, b.key)
    46  	return err
    47  }
    48  
    49  // Wait blocks on the barrier key until it is deleted. If there is no key, Wait
    50  // assumes Release has already been called and returns immediately.
    51  func (b *Barrier) Wait() error {
    52  	resp, err := b.client.Get(b.ctx, b.key)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if len(resp.Kvs) == 0 {
    57  		// key already removed
    58  		return nil
    59  	}
    60  	_, err = WaitEvents(
    61  		b.client,
    62  		b.key,
    63  		resp.Header.Revision,
    64  		[]mvccpb.Event_EventType{mvccpb.PUT, mvccpb.DELETE})
    65  	return err
    66  }
    67  

View as plain text