// NewAMQPTarget - creates new AMQP target.
972498
func NewAMQPTarget(id string, args AMQPArgs) (*AMQPTarget, error) {
	var conn *amqp.Connection
	var err error
	// Retry 5 times with time interval of 2 seconds.
	for i := 1; i <= 5; i++ {
		conn, err = amqp.Dial(args.URL.String())
		if err == nil {
			break
		}
		if err != nil && i == 5 {
			return nil, err
		}
		time.Sleep(2 * time.Second)
	}

	return &AMQPTarget{
		id:   event.TargetID{ID: id, Name: "amqp"},
		args: args,
		conn: conn,
	}, nil
}
446733
func NewMySQLTarget(id string, args MySQLArgs) (*MySQLTarget, error) {
	if args.DSN == "" {
		config := mysql.Config{
			User:                 args.User,
			Passwd:               args.Password,
			Net:                  "tcp",
			Addr:                 args.Host.String() + ":" + args.Port,
			DBName:               args.Database,
			AllowNativePasswords: true,
		}

		args.DSN = config.FormatDSN()
	}

	db, err := sql.Open("mysql", args.DSN)
	if err != nil {
		return nil, err
	}

	if err = db.Ping(); err != nil {
		return nil, err
	}

	if _, err = db.Exec(fmt.Sprintf(mysqlTableExists, args.Table)); err != nil {
		createStmt := mysqlCreateNamespaceTable
		if args.Format == event.AccessFormat {
			createStmt = mysqlCreateAccessTable
		}

		if _, err = db.Exec(fmt.Sprintf(createStmt, args.Table)); err != nil {
			return nil, err
		}
	}

	var updateStmt, deleteStmt, insertStmt *sql.Stmt
	switch args.Format {
	case event.NamespaceFormat:
		// insert or update statement
		if updateStmt, err = db.Prepare(fmt.Sprintf(mysqlUpdateRow, args.Table)); err != nil {
			return nil, err
		}
		// delete statement
		if deleteStmt, err = db.Prepare(fmt.Sprintf(mysqlDeleteRow, args.Table)); err != nil {
			return nil, err
		}
	case event.AccessFormat:
		// insert statement
		if insertStmt, err = db.Prepare(fmt.Sprintf(mysqlInsertRow, args.Table)); err != nil {
			return nil, err
		}
	}

	return &MySQLTarget{
		id:         event.TargetID{ID: id, Name: "mysql"},
		args:       args,
		updateStmt: updateStmt,
		deleteStmt: deleteStmt,
		insertStmt: insertStmt,
		db:         db,
	}, nil
}
1123391
func NewAdaptorWithAuth(host, clientID, username, password string) *Adaptor {
	return &Adaptor{
		name:          "MQTT",
		Host:          host,
		autoReconnect: false,
		cleanSession:  true,
		useSSL:        false,
		clientID:      clientID,
		username:      username,
		password:      password,
	}
}
22228
func New(dsn string, pex bool) (*Amqp, error) {
	conn, ch, err := createConnAndChan(dsn)
	if err != nil {
		return nil, err
	}

	return &Amqp{
		conn:            conn,
		ch:              ch,
		passiveExchange: pex,
	}, nil
}
918616
func NewSetCustomQuotaArg(UsersAndQuotas []*UserCustomQuotaArg) *SetCustomQuotaArg {
	s := new(SetCustomQuotaArg)
	s.UsersAndQuotas = UsersAndQuotas
	return s
}
917059
func New() *Pget {
	return &Pget{
		Trace:   false,
		Utils:   &Data{},
		Procs:   runtime.NumCPU(), // default
		timeout: 10,
	}
}
1016783
func NewCacheTTL(max int) *CacheTTL {
	if max == 0 {
		return nil
	}

	return &CacheTTL{
		items:       make(map[string]*eurekache.Item),
		maxSize:     max,
		deleteQueue: make([]string, 0, max),
	}
}
791487
func NewMockEmbed(ctrl *gomock.Controller) *MockEmbed {
	mock := &MockEmbed{ctrl: ctrl}
	mock.recorder = &MockEmbedMockRecorder{mock}
	return mock
}
107547
func NewConsoleTarget() *ConsoleTarget {
	return &ConsoleTarget{
		Filter:    &Filter{MaxLevel: LevelDebug},
		ColorMode: true,
		Writer:    os.Stdout,
		close:     make(chan bool, 0),
	}
}
1040667
func NewPairSocket() (*PairSocket, error) {
	s, err := NewSocket(AF_SP, PAIR)
	return &PairSocket{s}, err
}
1213093
func (s *ResourceGroup) SetCreatedAt(v time.Time) *ResourceGroup {
	s.CreatedAt = &v
	return s
}
198396
func (q *RateLimitedTimedQueue) SwapLimiter(newQPS float32) {
	q.limiterLock.Lock()
	defer q.limiterLock.Unlock()
	if q.limiter.QPS() == newQPS {
		return
	}
	var newLimiter flowcontrol.RateLimiter
	if newQPS <= 0 {
		newLimiter = flowcontrol.NewFakeNeverRateLimiter()
	} else {
		newLimiter = flowcontrol.NewTokenBucketRateLimiter(newQPS, EvictionRateLimiterBurst)

		// If we're currently waiting on limiter, we drain the new one - this is a good approach when Burst value is 1
		// TODO: figure out if we need to support higher Burst values and decide on the drain logic, should we keep:
		// - saturation (percentage of used tokens)
		// - number of used tokens
		// - number of available tokens
		// - something else
		if q.limiter.TryAccept() == false {
			newLimiter.TryAccept()
		}
	}
	q.limiter.Stop()
	q.limiter = newLimiter
}
128357
func NewCpuStats() *CpuStats {
	numCpus := runtime.NumCPU()
	cpuStats := &CpuStats{
		totalCpus: numCpus,
	}
	return cpuStats
}
355143
func NewCustomQuotaUsersArg(Users []*UserSelectorArg) *CustomQuotaUsersArg {
	s := new(CustomQuotaUsersArg)
	s.Users = Users
	return s
}
1077861
func NewMockEmbedded(ctrl *gomock.Controller) *MockEmbedded {
	mock := &MockEmbedded{ctrl: ctrl}
	mock.recorder = &MockEmbeddedMockRecorder{mock}
	return mock
}
960487
func New(queueID uint16, maxPacketsInQueue uint32, packetSize uint32) (nfq *Queue, err error) {
	nfq = &Queue{
		ID: queueID,
		// TODO: what should the chan size be?
		packets: make(chan *RawPacket, 1024),
	}
	var ret C.int

	defer func() {
		if err != nil && nfq != nil {
			if nfq.qh != nil {
				C.nfq_destroy_queue(nfq.qh)
			}

			if nfq.h != nil {
				C.nfq_close(nfq.h)
			}
		}
	}()

	if nfq.h, err = C.nfq_open(); err != nil {
		err = errors.Wrap(err, "netfilter: unable to open nfq queue handle.")
		return
	}

	if ret, err = C.nfq_unbind_pf(nfq.h, AF_INET); err != nil || ret < 0 {
		err = errors.Wrap(err, "netfilter: unable to unbind existing nfq handler.")
		return
	}

	if ret, err = C.nfq_bind_pf(nfq.h, AF_INET); err != nil || ret < 0 {
		err = errors.Wrap(err, "netfilter: unable to bind to AF_INET protocol family.")
		return
	}

	if nfq.qh, err = C.CreateQueue(nfq.h, C.u_int16_t(queueID)); err != nil || nfq.qh == nil {
		err = errors.Wrap(err, "netfilter: unable to create nfq queue.")
		return
	}

	if ret, err = C.nfq_set_queue_maxlen(nfq.qh, C.u_int32_t(maxPacketsInQueue)); err != nil || ret < 0 {
		err = errors.Wrap(err, "netfilter: unable to set nfq max queue length.")
		return
	}

	if C.nfq_set_mode(nfq.qh, C.u_int8_t(2), C.uint(packetSize)) < 0 {
		err = errors.Wrap(err, "netfilter: unable to set packet copy mode.")
		return
	}

	if nfq.fd, err = C.nfq_fd(nfq.h); err != nil {
		err = errors.Wrap(err, "netfilter: unable to get nfq queue file-descriptor.")
		return

	}

	// TODO: error handling
	// me: https://i.imgur.com/Lmy5P.gif
	C.nfnl_rcvbufsiz(C.nfq_nfnlh(nfq.h), 1024*16384)

	// register this queue object with the global dispatcher
	register(queueID, nfq.packets)

	return
}
1240894
func NewMailTarget() *MailTarget {
	return &MailTarget{
		Filter:     &Filter{MaxLevel: LevelDebug},
		BufferSize: 1024,
		close:      make(chan bool, 0),
	}
}