Help us shape the future of Go! Take the 2026 Go Developer Survey

etcd

package
v1.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2021 License: Apache-2.0 Imports: 12 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	// EnvVarDomain is the name of the environment variable that defines
	// the lock provider's concurrency domain.
	EnvVarDomain = "X_CSI_SERIAL_VOL_ACCESS_ETCD_DOMAIN"

	// EnvVarEndpoints is the name of the environment variable that defines
	// the lock provider's etcd endoints.
	EnvVarEndpoints = "X_CSI_SERIAL_VOL_ACCESS_ETCD_ENDPOINTS"

	// EnvVarTTL is the name of the environment
	// variable that defines the length of time etcd will wait before
	// releasing ownership of a distributed lock if the lock's session
	// has not been renewed.
	EnvVarTTL = "X_CSI_SERIAL_VOL_ACCESS_ETCD_TTL"

	// EnvVarAutoSyncInterval is the name of the environment
	// variable that defines the interval to update endpoints with its latest
	//  members. 0 disables auto-sync. By default auto-sync is disabled.
	EnvVarAutoSyncInterval = "X_CSI_SERIAL_VOL_ACCESS_ETCD_AUTO_SYNC_INTERVAL"

	// EnvVarDialTimeout is the name of the environment
	// variable that defines the timeout for failing to establish a connection.
	EnvVarDialTimeout = "X_CSI_SERIAL_VOL_ACCESS_ETCD_DIAL_TIMEOUT"

	// EnvVarDialKeepAliveTime is the name of the environment
	// variable that defines the time after which client pings the server to see
	// if transport is alive.
	EnvVarDialKeepAliveTime = "X_CSI_SERIAL_VOL_ACCESS_ETCD_DIAL_KEEP_ALIVE_TIME"

	// EnvVarDialKeepAliveTimeout is the name of the
	// environment variable that defines the time that the client waits for a
	// response for the keep-alive probe. If the response is not received in
	// this time, the connection is closed.
	EnvVarDialKeepAliveTimeout = "X_CSI_SERIAL_VOL_ACCESS_ETCD_DIAL_KEEP_ALIVE_TIMEOUT"

	// EnvVarMaxCallSendMsgSz is the name of the environment
	// variable that defines the client-side request send limit in bytes.
	// If 0, it defaults to 2.0 MiB (2 * 1024 * 1024).
	// Make sure that "MaxCallSendMsgSize" < server-side default send/recv
	// limit. ("--max-request-bytes" flag to etcd or
	// "embed.Config.MaxRequestBytes").
	EnvVarMaxCallSendMsgSz = "X_CSI_SERIAL_VOL_ACCESS_ETCD_MAX_CALL_SEND_MSG_SZ"

	// EnvVarMaxCallRecvMsgSz is the name of the environment
	// variable that defines the client-side response receive limit.
	// If 0, it defaults to "math.MaxInt32", because range response can
	// easily exceed request send limits.
	// Make sure that "MaxCallRecvMsgSize" >= server-side default send/recv
	// limit. ("--max-request-bytes" flag to etcd or
	// "embed.Config.MaxRequestBytes").
	EnvVarMaxCallRecvMsgSz = "X_CSI_SERIAL_VOL_ACCESS_ETCD_MAX_CALL_RECV_MSG_SZ"

	// EnvVarUsername is the name of the environment
	// variable that defines the user name used for authentication.
	EnvVarUsername = "X_CSI_SERIAL_VOL_ACCESS_ETCD_USERNAME"

	// EnvVarPassword is the name of the environment
	// variable that defines the password used for authentication.
	EnvVarPassword = "X_CSI_SERIAL_VOL_ACCESS_ETCD_PASSWORD"

	// EnvVarRejectOldCluster is the name of the environment
	// variable that defines when set will refuse to create a client against
	// an outdated cluster.
	EnvVarRejectOldCluster = "X_CSI_SERIAL_VOL_ACCESS_ETCD_REJECT_OLD_CLUSTER"

	// EnvVarTLS is the name of the environment
	// variable that defines whether or not the client should attempt
	// to use TLS when connecting to the server.
	EnvVarTLS = "X_CSI_SERIAL_VOL_ACCESS_ETCD_TLS"

	// EnvVarTLSInsecure is the name of the environment
	// variable that defines whether or not the TLS connection should
	// verify certificates.
	EnvVarTLSInsecure = "X_CSI_SERIAL_VOL_ACCESS_ETCD_TLS_INSECURE"
)

Variables

This section is empty.

Functions

func New

func New(
	ctx context.Context,
	domain string,
	ttl time.Duration,
	config *etcd.Config) (mwtypes.VolumeLockerProvider, error)

New returns a new etcd volume lock provider.

Types

type TryMutex

type TryMutex struct {

	// LockCtx, when non-nil, is the context used with Lock.
	LockCtx context.Context

	// UnlockCtx, when non-nil, is the context used with Unlock.
	UnlockCtx context.Context

	// TryLockCtx, when non-nil, is the context used with TryLock.
	TryLockCtx context.Context
	// contains filtered or unexported fields
}

TryMutex is a mutual exclusion lock backed by etcd that implements the TryLocker interface. The zero value for a TryMutex is an unlocked mutex.

A TryMutex may be copied after first use.

func (*TryMutex) Close

func (m *TryMutex) Close() error

Close closes and cleans up the underlying concurrency session.

func (*TryMutex) Lock

func (m *TryMutex) Lock()

Lock locks m. If the lock is already in use, the calling goroutine blocks until the mutex is available.

func (*TryMutex) TryLock

func (m *TryMutex) TryLock(timeout time.Duration) bool

TryLock attempts to lock m. If no lock can be obtained in the specified duration then a false value is returned.

Example
package main

import (
	"context"
	"fmt"
	"io"
	"time"

	log "github.com/sirupsen/logrus"

	mwtypes "github.com/rexray/gocsi/middleware/serialvolume/types"
)

var p mwtypes.VolumeLockerProvider

func main() {

	const lockName = "ExampleTryMutex_TryLock"

	// The context used when creating new locks and their concurrency sessions.
	ctx := context.Background()

	// Assign a TryMutex to m1 and then lock m1.
	m1, err := p.GetLockWithName(ctx, lockName)
	if err != nil {
		log.Error(err)
		return
	}
	defer m1.(io.Closer).Close()
	m1.Lock()

	// Start a goroutine that sleeps for one second and then
	// unlocks m1. This makes it possible for the TryLock
	// call below to lock m2.
	go func() {
		time.Sleep(time.Duration(1) * time.Second)
		m1.Unlock()
	}()

	// Try for three seconds to lock m2.
	m2, err := p.GetLockWithName(ctx, lockName)
	if err != nil {
		log.Error(err)
		return
	}
	defer m2.(io.Closer).Close()
	if m2.TryLock(time.Duration(3) * time.Second) {
		fmt.Println("lock obtained")
	}
	m2.Unlock()

}
Output:
lock obtained
Example (Timeout)
package main

import (
	"context"
	"fmt"
	"io"
	"time"

	log "github.com/sirupsen/logrus"

	mwtypes "github.com/rexray/gocsi/middleware/serialvolume/types"
)

var p mwtypes.VolumeLockerProvider

func main() {

	const lockName = "ExampleTryMutex_TryLock_timeout"

	// The context used when creating new locks and their concurrency sessions.
	ctx := context.Background()

	// Assign a TryMutex to m1 and then lock m1.
	m1, err := p.GetLockWithName(ctx, lockName)
	if err != nil {
		log.Error(err)
		return
	}
	defer m1.(io.Closer).Close()
	defer m1.Unlock()
	m1.Lock()

	// Try for three seconds to lock m2.
	m2, err := p.GetLockWithName(ctx, lockName)
	if err != nil {
		log.Error(err)
		return
	}
	defer m2.(io.Closer).Close()
	if !m2.TryLock(time.Duration(3) * time.Second) {
		fmt.Println("lock not obtained")
	}

}
Output:
lock not obtained

func (*TryMutex) Unlock

func (m *TryMutex) Unlock()

Unlock unlocks m. It is a run-time error if m is not locked on entry to Unlock.

A locked TryMutex is not associated with a particular goroutine. It is allowed for one goroutine to lock a Mutex and then arrange for another goroutine to unlock it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL