// NewPU creates a new PU context
196306
func NewPU(contextID string, puInfo *policy.PUInfo, timeout time.Duration) (*PUContext, error) {
	ctx := context.Background()
	ctx, cancelFunc := context.WithCancel(ctx)

	pu := &PUContext{
		id:              contextID,
		username:        puInfo.Runtime.Options().UserID,
		autoport:        puInfo.Runtime.Options().AutoPort,
		managementID:    puInfo.Policy.ManagementID(),
		puType:          puInfo.Runtime.PUType(),
		identity:        puInfo.Policy.Identity(),
		annotations:     puInfo.Policy.Annotations(),
		externalIPCache: cache.NewCacheWithExpiration("External IP Cache", timeout),
		ApplicationACLs: acls.NewACLCache(),
		networkACLs:     acls.NewACLCache(),
		mark:            puInfo.Runtime.Options().CgroupMark,
		scopes:          puInfo.Policy.Scopes(),
		CancelFunc:      cancelFunc,
	}

	pu.CreateRcvRules(puInfo.Policy.ReceiverRules())

	pu.CreateTxtRules(puInfo.Policy.TransmitterRules())

	tcpPorts, udpPorts := common.ConvertServicesToProtocolPortList(puInfo.Runtime.Options().Services)
	pu.tcpPorts = strings.Split(tcpPorts, ",")
	pu.udpPorts = strings.Split(udpPorts, ",")

	if err := pu.UpdateApplicationACLs(puInfo.Policy.ApplicationACLs()); err != nil {
		return nil, err
	}

	if err := pu.UpdateNetworkACLs(puInfo.Policy.NetworkACLs()); err != nil {
		return nil, err
	}

	dnsACL := puInfo.Policy.DNSNameACLs()
	pu.startDNS(ctx, &dnsACL)
	return pu, nil
}
487315
func (c *Canceller) NewContext() (context.Context, context.CancelFunc) {
	return context.WithCancel(c.ctx)
}
678976
func NewUniterAPI(context facade.Context) (*UniterAPI, error) {
	authorizer := context.Auth()
	if !authorizer.AuthUnitAgent() && !authorizer.AuthApplicationAgent() {
		return nil, common.ErrPerm
	}
	st := context.State()
	resources := context.Resources()
	accessUnit := unitAccessor(authorizer, st)
	leadershipChecker, err := context.LeadershipChecker()
	if err != nil {
		return nil, errors.Trace(err)
	}
	accessApplication := func() (common.AuthFunc, error) {
		switch tag := authorizer.GetAuthTag().(type) {
		case names.ApplicationTag:
			return func(applicationTag names.Tag) bool {
				return tag == applicationTag
			}, nil
		case names.UnitTag:
			entity, err := st.Unit(tag.Id())
			if err != nil {
				return nil, errors.Trace(err)
			}
			applicationName := entity.ApplicationName()
			applicationTag := names.NewApplicationTag(applicationName)
			return func(tag names.Tag) bool {
				return tag == applicationTag
			}, nil
		default:
			return nil, errors.Errorf("expected names.UnitTag or names.ApplicationTag, got %T", tag)
		}
	}
	accessMachine := func() (common.AuthFunc, error) {
		switch tag := authorizer.GetAuthTag().(type) {
		// Application agents can't access machines.
		case names.ApplicationTag:
			return func(tag names.Tag) bool {
				return false
			}, nil
		case names.UnitTag:
			entity, err := st.Unit(tag.Id())
			if err != nil {
				return nil, errors.Trace(err)
			}
			machineId, err := entity.AssignedMachineId()
			if err != nil {
				return nil, errors.Trace(err)
			}
			machineTag := names.NewMachineTag(machineId)
			return func(tag names.Tag) bool {
				return tag == machineTag
			}, nil
		default:
			return nil, errors.Errorf("expected names.UnitTag or names.ApplicationTag, got %T", tag)
		}
	}
	accessCloudSpec := func() (func() bool, error) {
		var appName string
		var err error

		switch tag := authorizer.GetAuthTag().(type) {
		case names.ApplicationTag:
			appName = tag.Id()
		case names.UnitTag:
			entity, err := st.Unit(tag.Id())
			if err != nil {
				return nil, errors.Trace(err)
			}
			appName = entity.ApplicationName()
		default:
			return nil, errors.Errorf("expected names.UnitTag or names.ApplicationTag, got %T", tag)
		}

		app, err := st.Application(appName)
		if err != nil {
			return nil, errors.Trace(err)
		}
		config, err := app.ApplicationConfig()
		if err != nil {
			return nil, errors.Trace(err)
		}
		return func() bool {
			return config.GetBool(application.TrustConfigOptionName, false)
		}, nil
	}

	m, err := st.Model()
	if err != nil {
		return nil, errors.Trace(err)
	}

	storageAccessor, err := getStorageState(st)
	storageAPI, err := newStorageAPI(
		stateShim{st}, storageAccessor, resources, accessUnit)
	if err != nil {
		return nil, err
	}
	msAPI, err := meterstatus.NewMeterStatusAPI(st, resources, authorizer)
	if err != nil {
		return nil, errors.Annotate(err, "could not create meter status API handler")
	}
	accessUnitOrApplication := common.AuthAny(accessUnit, accessApplication)

	cloudSpec := cloudspec.NewCloudSpec(
		resources,
		cloudspec.MakeCloudSpecGetterForModel(st),
		cloudspec.MakeCloudSpecWatcherForModel(st),
		common.AuthFuncForTag(m.ModelTag()),
	)

	return &UniterAPI{
		LifeGetter:                 common.NewLifeGetter(st, accessUnitOrApplication),
		DeadEnsurer:                common.NewDeadEnsurer(st, accessUnit),
		AgentEntityWatcher:         common.NewAgentEntityWatcher(st, resources, accessUnitOrApplication),
		APIAddresser:               common.NewAPIAddresser(st, resources),
		ModelWatcher:               common.NewModelWatcher(m, resources, authorizer),
		RebootRequester:            common.NewRebootRequester(st, accessMachine),
		UpgradeSeriesAPI:           common.NewExternalUpgradeSeriesAPI(st, resources, authorizer, accessMachine, accessUnit, logger),
		LeadershipSettingsAccessor: leadershipSettingsAccessorFactory(st, leadershipChecker, resources, authorizer),
		MeterStatus:                msAPI,
		// TODO(fwereade): so *every* unit should be allowed to get/set its
		// own status *and* its application's? This is not a pleasing arrangement.
		StatusAPI: NewStatusAPI(st, accessUnitOrApplication, leadershipChecker),

		st:                st,
		m:                 m,
		auth:              authorizer,
		resources:         resources,
		leadershipChecker: leadershipChecker,
		accessUnit:        accessUnit,
		accessApplication: accessApplication,
		accessMachine:     accessMachine,
		accessCloudSpec:   accessCloudSpec,
		cloudSpec:         cloudSpec,
		StorageAPI:        storageAPI,
	}, nil
}
62412
func NewContext(req *http.Request) context.Context {
	return WithContext(context.Background(), req)
}
547151
func NewPUPolicyWithDefaults() *PUPolicy {
	return NewPUPolicy("", AllowAll, nil, nil, nil, nil, nil, nil, nil, nil, 0, nil, nil, []string{})
}
381818
func NewContext(req *http.Request) *Context {
	if ae != nil {
		ctxt := ae.NewContext(req)
		return &Context{ae: ctxt}
	}
	return newContext(req)
}
1031551
func NewContext(projectroot string, options ...func(*gb.Context) error) (*gb.Context, error) {
	if projectroot == "" {
		return nil, errors.New("project root is blank")
	}

	root, err := FindProjectroot(projectroot)
	if err != nil {
		return nil, errors.Wrap(err, "could not locate project root")
	}
	proj := gb.NewProject(root)

	ctx, err := gb.NewContext(proj, options...)
	if err != nil {
		return nil, err
	}
	ctx.Debug("project root %q", proj.Projectdir())
	return ctx, err
}
1221004
func NewPubsub(pool *redis.Pool) *Pubsub {
	ps := &Pubsub{
		pool:   pool,
		errs:   make(chan error),
		closer: make(chan struct{}),
		send:   make(chan command),
		subs: []*RecordList{
			PlainEvent:   NewRecordList(),
			PatternEvent: NewRecordList(),
		},
	}

	go ps.work()

	return ps
}
670269
func NewUniterAPIV8(context facade.Context) (*UniterAPIV8, error) {
	uniterAPI, err := NewUniterAPIV9(context)
	if err != nil {
		return nil, err
	}
	return &UniterAPIV8{
		UniterAPIV9: *uniterAPI,
	}, nil
}
1178388
func New(cfg *UtxoSweeperConfig) *UtxoSweeper {

	return &UtxoSweeper{
		cfg:           cfg,
		newInputs:     make(chan *sweepInputMessage),
		spendChan:     make(chan *chainntnfs.SpendDetail),
		quit:          make(chan struct{}),
		pendingInputs: make(map[wire.OutPoint]*pendingInput),
	}
}
216815
func NewContext(ctx context.Context, s Service) context.Context {
	return context.WithValue(ctx, serviceKey{}, s)
}
534455
func NewPUBCOMP(opts *PUBCOMPOptions) (Packet, error) {
	// Initialize the options.
	if opts == nil {
		opts = &PUBCOMPOptions{}
	}

	// Validate the options.
	if err := opts.validate(); err != nil {
		return nil, err
	}

	// Create a PUBCOMP Packet.
	p := &PUBCOMP{
		PacketID: opts.PacketID,
	}

	// Set the variable header to the Packet.
	p.setVariableHeader()

	// Set the Fixed header to the Packet.
	p.setFixedHeader()

	// Return the Packet.
	return p, nil
}
1350484
func NewPUBREC(opts *PUBRECOptions) (Packet, error) {
	// Initialize the options.
	if opts == nil {
		opts = &PUBRECOptions{}
	}

	// Validate the options.
	if err := opts.validate(); err != nil {
		return nil, err
	}

	// Create a PUBREC Packet.
	p := &PUBREC{
		PacketID: opts.PacketID,
	}

	// Set the variable header to the Packet.
	p.setVariableHeader()

	// Set the Fixed header to the Packet.
	p.setFixedHeader()

	// Return the Packet.
	return p, nil
}
481276
func NewUV(key string, options ...Option) (*UV, error) {
	k, err := setKey(key)
	if err != nil {
		return nil, err
	}
	u := &UV{
		Key:      k,
		Settings: NewSettings(),
	}

	if err := setOptions(u.Settings, options); err != nil {
		return nil, err
	}
	return u, nil
}
259509
func NewContext(ctx context.Context) context.Context {
	u, _ := uuid.NewV4()
	uStr := u.String()
	ctx = context.WithValue(ctx, requestUuidKey, uStr)
	ctx = context.WithValue(ctx, processPidKey, strconv.Itoa(os.Getpid()))
	ctx = context.WithValue(ctx, additionnalLogElementsKey, make(map[interface{}]interface{}))
	return ctx
}
630939
func NewContext(ctx context.Context, ci CallInfo) context.Context {
	return context.WithValue(ctx, callInfoKey, ci)
}
492087
func NewContext() (retVal *Context) {
	var internal C.cudnnHandle_t
	if err := result(C.cudnnCreate(&internal)); err != nil {
		panic(err)
	}
	retVal = &Context{internal}
	return retVal
}