github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/sync/sync.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/sync/sync.go
    14  
    15  // Package sync is an interface for distributed synchronization
    16  package sync
    17  
    18  import (
    19  	"errors"
    20  	"time"
    21  )
    22  
    23  var (
    24  	ErrLockTimeout = errors.New("lock timeout")
    25  )
    26  
    27  // Sync is an interface for distributed synchronization
    28  type Sync interface {
    29  	// Initialise options
    30  	Init(...Option) error
    31  	// Return the options
    32  	Options() Options
    33  	// Elect a leader
    34  	Leader(id string, opts ...LeaderOption) (Leader, error)
    35  	// Lock acquires a lock
    36  	Lock(id string, opts ...LockOption) error
    37  	// Unlock releases a lock
    38  	Unlock(id string) error
    39  	// Sync implementation
    40  	String() string
    41  }
    42  
    43  // Leader provides leadership election
    44  type Leader interface {
    45  	// resign leadership
    46  	Resign() error
    47  	// status returns when leadership is lost
    48  	Status() chan bool
    49  }
    50  
    51  type Options struct {
    52  	Nodes  []string
    53  	Prefix string
    54  }
    55  
    56  type Option func(o *Options)
    57  
    58  type LeaderOptions struct{}
    59  
    60  type LeaderOption func(o *LeaderOptions)
    61  
    62  type LockOptions struct {
    63  	TTL  time.Duration
    64  	Wait time.Duration
    65  }
    66  
    67  type LockOption func(o *LockOptions)