Ejemplo n.º 1
0
func (a *Application) reinitFromConfig() (err error) {
	app := *a // copy
	app.virtualHosts = make(map[string]*VirtualHost)
	app.upstreams = make(map[string]types.Upstream)
	app.cacheZones = make(map[string]*types.CacheZone)
	logs := accessLogs{"": nil}
	// Initialize the global logger
	if app.logger, err = logger.New(&app.cfg.Logger); err != nil {
		return err
	}

	var toBeResized = make([]string, 0, len(a.cacheZones))
	// Initialize all cache zones
	for _, cfgCz := range app.cfg.CacheZones {
		if zone, ok := a.cacheZones[cfgCz.ID]; ok {
			app.cacheZones[cfgCz.ID] = zone
			toBeResized = append(toBeResized, cfgCz.ID)
			continue
		}
		if err = app.initCacheZone(cfgCz); err != nil {
			return err
		}
	}

	// Initialize all advanced upstreams
	for _, cfgUp := range app.cfg.HTTP.Upstreams {
		if app.upstreams[cfgUp.ID], err = upstream.New(cfgUp, app.logger); err != nil {
			return err
		}
	}

	app.notConfiguredHandler = newNotConfiguredHandler()
	var accessLog io.Writer
	if accessLog, err = logs.openAccessLog(app.cfg.HTTP.AccessLog); err != nil {
		return err
	}
	app.notConfiguredHandler, _ = loggingHandler(app.notConfiguredHandler, accessLog, notConfiguredLocation)
	// Initialize all vhosts
	for _, cfgVhost := range app.cfg.HTTP.Servers {
		if err = app.initVirtualHost(cfgVhost, logs); err != nil {
			return err
		}
	}

	a.Lock()
	defer a.Unlock()
	a.logger = app.logger
	a.virtualHosts = app.virtualHosts
	a.upstreams = app.upstreams
	a.notConfiguredHandler = app.notConfiguredHandler
	for id := range a.cacheZones { // clean the cacheZones
		delete(a.cacheZones, id)
	}
	for _, id := range toBeResized { // resize the to be resized
		var cfgCz = app.cfg.CacheZones[id]
		app.cacheZones[id].Algorithm.ChangeConfig(cfgCz.BulkRemoveTimeout, cfgCz.BulkRemoveCount, cfgCz.StorageObjects, a.logger)
		app.cacheZones[id].Storage.ChangeConfig(a.logger)
	}
	for id, zone := range app.cacheZones { // copy everything
		a.cacheZones[id] = zone
	}

	return nil
}
Ejemplo n.º 2
0
// initFromConfig should be called when starting or reloading the app. It makes
// all the connections between cache zones, virtual hosts, storage objects
// and upstreams.
func (a *Application) initFromConfig() error {
	// Make the vhost and storage maps
	a.virtualHosts = make(map[string]*types.VirtualHost)
	a.storages = make(map[string]types.Storage)

	// Create a global application context
	a.ctx, a.ctxCancel = context.WithCancel(context.Background())

	// Initialize the global logger
	defaultLogger, err := logger.New(a.cfg.Logger)
	if err != nil {
		return err
	}
	a.logger = defaultLogger

	// Initialize all cache storages
	for _, cfgStorage := range a.cfg.CacheZones {
		//!TODO: the cache zone should be responsible for it's own algorithm
		ca, err := cache.New(cfgStorage)
		if err != nil {
			return err
		}

		removeChan := make(chan types.ObjectIndex, 1000)
		ca.ReplaceRemoveChannel(removeChan)
		stor, err := storage.New(*cfgStorage, ca, a.logger)
		if err != nil {
			return fmt.Errorf("Could not initialize storage '%s' impl: %s", cfgStorage.Type, err)
		}

		a.storages[cfgStorage.ID] = stor
		go a.cacheToStorageCommunicator(stor, removeChan)
		a.removeChannels = append(a.removeChannels, removeChan)
	}

	// Initialize all vhosts
	for _, cfgVhost := range a.cfg.HTTP.Servers {
		vhost := types.VirtualHost{
			Name:            cfgVhost.Name,
			CacheKey:        cfgVhost.CacheKey,
			UpstreamAddress: cfgVhost.UpstreamAddress,
			Logger:          a.logger,
		}
		a.virtualHosts[cfgVhost.Name] = &vhost

		if cfgVhost.Logger != nil {
			if vhost.Logger, err = logger.New(*cfgVhost.Logger); err != nil {
				return err
			}
		}

		if vhost.Handler, err = handler.New(cfgVhost.HandlerType); err != nil {
			return err
		}

		//!TODO: the rest of the initialization should probably be handled by
		// the handler constructor itself, like how each log type handles its
		// own specific settings, not with string comparisons of the type here

		// If this is not a proxy hanlder, there is no need to initialize the rest
		if cfgVhost.HandlerType != "proxy" {
			continue
		}

		if vhost.Upstream, err = upstream.New(cfgVhost.UpstreamType, cfgVhost.UpstreamAddress); err != nil {
			return err
		}

		if cfgVhost.CacheZone == nil {
			return fmt.Errorf("Cache zone for %s was nil", cfgVhost.Name)
		}

		stor, ok := a.storages[cfgVhost.CacheZone.ID]
		if !ok {
			return fmt.Errorf("Could not get the cache zone for vhost %s", cfgVhost.Name)
		}
		vhost.Storage = stor
	}

	a.ctx = contexts.NewStoragesContext(a.ctx, a.storages)

	return nil
}