etcd: Index | Files

package embed

import ""

Package embed provides bindings for embedding an etcd server in a program.

Launch an embedded etcd server using the configuration defaults:

import (


func main() {
	cfg := embed.NewConfig()
	cfg.Dir = "default.etcd"
	e, err := embed.StartEtcd(cfg)
	if err != nil {
	defer e.Close()
	select {
	case <-e.Server.ReadyNotify():
		log.Printf("Server is ready!")
	case <-time.After(60 * time.Second):
		e.Server.Stop() // trigger a shutdown
		log.Printf("Server took too long to start!")


Package Files

config.go doc.go etcd.go serve.go util.go


const (
    ClusterStateFlagNew      = "new"
    ClusterStateFlagExisting = "existing"

    DefaultName                  = "default"
    DefaultMaxSnapshots          = 5
    DefaultMaxWALs               = 5
    DefaultMaxTxnOps             = uint(128)
    DefaultMaxRequestBytes       = 1.5 * 1024 * 1024
    DefaultGRPCKeepAliveMinTime  = 5 * time.Second
    DefaultGRPCKeepAliveInterval = 2 * time.Hour
    DefaultGRPCKeepAliveTimeout  = 20 * time.Second

    DefaultListenPeerURLs   = "http://localhost:2380"
    DefaultListenClientURLs = "http://localhost:2379"

    DefaultLogOutput = "default"

    // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag.
    // It's enabled by default.
    DefaultStrictReconfigCheck = true
    // DefaultEnableV2 is the default value for "--enable-v2" flag.
    // v2 is enabled by default.
    // TODO: disable v2 when deprecated.
    DefaultEnableV2 = true


var (
    ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " +
        "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"")
    ErrUnsetAdvertiseClientURLsFlag = fmt.Errorf("--advertise-client-urls is required when --listen-client-urls is set explicitly")

    DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
    DefaultAdvertiseClientURLs      = "http://localhost:2379"
var (
    // CompactorModePeriodic is periodic compaction mode
    // for "Config.AutoCompactionMode" field.
    // If "AutoCompactionMode" is CompactorModePeriodic and
    // "AutoCompactionRetention" is "1h", it automatically compacts
    // compacts storage every hour.
    CompactorModePeriodic = compactor.ModePeriodic

    // CompactorModeRevision is revision-based compaction mode
    // for "Config.AutoCompactionMode" field.
    // If "AutoCompactionMode" is CompactorModeRevision and
    // "AutoCompactionRetention" is "1000", it compacts log on
    // revision 5000 when the current revision is 6000.
    // This runs every 5-minute if enough of logs have proceeded.
    CompactorModeRevision = compactor.ModeRevision

type Config Uses

type Config struct {
    CorsInfo                *cors.CORSInfo
    LPUrls, LCUrls          []url.URL
    Dir                     string `json:"data-dir"`
    WalDir                  string `json:"wal-dir"`
    MaxSnapFiles            uint   `json:"max-snapshots"`
    MaxWalFiles             uint   `json:"max-wals"`
    Name                    string `json:"name"`
    SnapCount               uint64 `json:"snapshot-count"`
    AutoCompactionRetention string `json:"auto-compaction-retention"`
    AutoCompactionMode      string `json:"auto-compaction-mode"`

    // TickMs is the number of milliseconds between heartbeat ticks.
    // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1).
    // make ticks a cluster wide configuration.
    TickMs            uint  `json:"heartbeat-interval"`
    ElectionMs        uint  `json:"election-timeout"`
    QuotaBackendBytes int64 `json:"quota-backend-bytes"`
    MaxTxnOps         uint  `json:"max-txn-ops"`
    MaxRequestBytes   uint  `json:"max-request-bytes"`

    // GRPCKeepAliveMinTime is the minimum interval that a client should
    // wait before pinging server. When client pings "too fast", server
    // sends goaway and closes the connection (errors: too_many_pings,
    // http2.ErrCodeEnhanceYourCalm). When too slow, nothing happens.
    // Server expects client pings only when there is any active streams
    // (PermitWithoutStream is set false).
    GRPCKeepAliveMinTime time.Duration `json:"grpc-keepalive-min-time"`
    // GRPCKeepAliveInterval is the frequency of server-to-client ping
    // to check if a connection is alive. Close a non-responsive connection
    // after an additional duration of Timeout. 0 to disable.
    GRPCKeepAliveInterval time.Duration `json:"grpc-keepalive-interval"`
    // GRPCKeepAliveTimeout is the additional duration of wait
    // before closing a non-responsive connection. 0 to disable.
    GRPCKeepAliveTimeout time.Duration `json:"grpc-keepalive-timeout"`

    APUrls, ACUrls        []url.URL
    ClusterState          string `json:"initial-cluster-state"`
    DNSCluster            string `json:"discovery-srv"`
    DNSClusterServiceName string `json:"discovery-srv-name"`
    Dproxy                string `json:"discovery-proxy"`
    Durl                  string `json:"discovery"`
    InitialCluster        string `json:"initial-cluster"`
    InitialClusterToken   string `json:"initial-cluster-token"`
    StrictReconfigCheck   bool   `json:"strict-reconfig-check"`
    EnableV2              bool   `json:"enable-v2"`

    ClientTLSInfo transport.TLSInfo
    ClientAutoTLS bool
    PeerTLSInfo   transport.TLSInfo
    PeerAutoTLS   bool

    Debug                 bool   `json:"debug"`
    LogPkgLevels          string `json:"log-package-levels"`
    LogOutput             string `json:"log-output"`
    EnablePprof           bool   `json:"enable-pprof"`
    Metrics               string `json:"metrics"`
    ListenMetricsUrls     []url.URL
    ListenMetricsUrlsJSON string `json:"listen-metrics-urls"`

    // ForceNewCluster starts a new cluster even if previously started; unsafe.
    ForceNewCluster bool `json:"force-new-cluster"`

    // UserHandlers is for registering users handlers and only used for
    // embedding etcd into other applications.
    // The map key is the route path for the handler, and
    // you must ensure it can't be conflicted with etcd's.
    UserHandlers map[string]http.Handler `json:"-"`
    // ServiceRegister is for registering users' gRPC services. A simple usage example:
    //	cfg := embed.NewConfig()
    //	cfg.ServerRegister = func(s *grpc.Server) {
    //		pb.RegisterFooServer(s, &fooServer{})
    //		pb.RegisterBarServer(s, &barServer{})
    //	}
    //	embed.StartEtcd(cfg)
    ServiceRegister func(*grpc.Server) `json:"-"`

    AuthToken string `json:"auth-token"`

    ExperimentalInitialCorruptCheck bool          `json:"experimental-initial-corrupt-check"`
    ExperimentalCorruptCheckTime    time.Duration `json:"experimental-corrupt-check-time"`
    ExperimentalEnableV2V3          string        `json:"experimental-enable-v2v3"`

Config holds the arguments for configuring an etcd server.

func ConfigFromFile Uses

func ConfigFromFile(path string) (*Config, error)

func NewConfig Uses

func NewConfig() *Config

NewConfig creates a new Config populated with default values.

func (*Config) ClientSelfCert Uses

func (cfg *Config) ClientSelfCert() (err error)

func (Config) ElectionTicks Uses

func (cfg Config) ElectionTicks() int

func (*Config) GetDNSClusterNames Uses

func (cfg *Config) GetDNSClusterNames() ([]string, error)

GetDNSClusterNames uses DNS SRV records to get a list of initial nodes for cluster bootstrapping.

func (Config) InitialClusterFromName Uses

func (cfg Config) InitialClusterFromName(name string) (ret string)

func (Config) IsNewCluster Uses

func (cfg Config) IsNewCluster() bool

func (*Config) PeerSelfCert Uses

func (cfg *Config) PeerSelfCert() (err error)

func (*Config) PeerURLsMapAndToken Uses

func (cfg *Config) PeerURLsMapAndToken(which string) (urlsmap types.URLsMap, token string, err error)

PeerURLsMapAndToken sets up an initial peer URLsMap and cluster token for bootstrap or discovery.

func (*Config) SetupLogging Uses

func (cfg *Config) SetupLogging()

SetupLogging initializes etcd logging. Must be called after flag parsing.

func (*Config) UpdateDefaultClusterFromName Uses

func (cfg *Config) UpdateDefaultClusterFromName(defaultInitialCluster string) (string, error)

UpdateDefaultClusterFromName updates cluster advertise URLs with, if available, default host, if advertise URLs are default values(localhost:2379,2380) AND if listen URL is e.g. advertise peer URL localhost:2380 or listen peer URL then the advertise peer host would be updated with machine's default host, while keeping the listen URL's port. User can work around this by explicitly setting URL with It returns the default hostname, if used, and the error, if any, from getting the machine's default host. TODO: check whether fields are set instead of whether fields have default value

func (*Config) Validate Uses

func (cfg *Config) Validate() error

type Etcd Uses

type Etcd struct {
    Peers   []*peerListener
    Clients []net.Listener

    Server *etcdserver.EtcdServer
    // contains filtered or unexported fields

Etcd contains a running etcd server and its listeners.

func StartEtcd Uses

func StartEtcd(inCfg *Config) (e *Etcd, err error)

StartEtcd launches the etcd server and HTTP handlers for client/server communication. The returned Etcd.Server is not guaranteed to have joined the cluster. Wait on the Etcd.Server.ReadyNotify() channel to know when it completes and is ready for use.

func (*Etcd) Close Uses

func (e *Etcd) Close()

Close gracefully shuts down all servers/listeners. Client requests will be terminated with request timeout. After timeout, enforce remaning requests be closed immediately.

func (*Etcd) Config Uses

func (e *Etcd) Config() Config

Config returns the current configuration.

func (*Etcd) Err Uses

func (e *Etcd) Err() <-chan error

Package embed imports 48 packages (graph) and is imported by 57 packages. Updated 2018-02-14. Refresh now. Tools for package owners.