Esempio n. 1
0
// New returns a new DB object.
func New() *DB {
	db := &DB{l: newLogger()}

	cli := gentleman.New()
	cli.Use(retry.New(retrier.New(retrier.ExponentialBackoff(3, 100*time.Millisecond), nil)))
	cli.UseRequest(func(ctx *context.Context, h context.Handler) {
		u, err := url.Parse(db.url)
		if err != nil {
			h.Error(ctx, err)
			return
		}

		ctx.Request.URL.Scheme = u.Scheme
		ctx.Request.URL.Host = u.Host
		ctx.Request.URL.Path = db.dbPath()
		h.Next(ctx)
	})
	cli.UseRequest(func(ctx *context.Context, h context.Handler) {
		ctx.Request.SetBasicAuth(db.username, db.password)
		h.Next(ctx)
	})

	db.conn = cli

	return db
}
Esempio n. 2
0
package devices

import (
	"fmt"
	"net"
	"sync"
	"time"

	"github.com/docker/libcontainer/netlink"
	"github.com/eapache/go-resiliency/retrier"
)

// netlink is not thread-safe, all calls to netlink should be guarded by this mutex
var netlinkMu *sync.Mutex = new(sync.Mutex)

var retry = retrier.New(retrier.ExponentialBackoff(6, 10*time.Millisecond), nil)

type Bridge struct{}

// Create creates a bridge device and returns the interface.
// If the device already exists, returns the existing interface.
func (Bridge) Create(name string, ip net.IP, subnet *net.IPNet) (intf *net.Interface, err error) {
	netlinkMu.Lock()
	defer netlinkMu.Unlock()

	if intf, err = idempotentlyCreateBridge(name); err != nil {
		return nil, err
	}

	if err = netlink.NetworkLinkAddIp(intf, ip, subnet); err != nil && err.Error() != "file exists" {
		return nil, fmt.Errorf("devices: add IP to bridge: %v", err)