コード例 #1
0
ファイル: init.go プロジェクト: gowebabc/nedomi
func (a *Application) initFromConfigLocationsForVHost(cfgLocations []*config.Location, accessLog io.Writer) ([]*types.Location, error) {
	var err error
	var locations = make([]*types.Location, len(cfgLocations))
	for index, locCfg := range cfgLocations {
		locations[index] = &types.Location{
			Name:                  locCfg.Name,
			CacheKey:              locCfg.CacheKey,
			CacheKeyIncludesQuery: locCfg.CacheKeyIncludesQuery,
			CacheDefaultDuration:  locCfg.CacheDefaultDuration,
		}
		if locations[index].Upstream, err = a.getUpstream(locCfg.Upstream); err != nil {
			return nil, err
		}

		if locations[index].Logger, err = logger.New(&locCfg.Logger); err != nil {
			return nil, err
		}

		if locCfg.CacheZone != nil {
			cz, ok := a.cacheZones[locCfg.CacheZone.ID]
			if !ok {
				return nil, fmt.Errorf("Could not get the cache zone for locations[index] %s", locCfg.Name)
			}
			locations[index].Cache = cz
		}

		if locations[index].Handler, err = chainHandlers(locations[index], locCfg, accessLog); err != nil {
			return nil, err
		}

	}

	return locations, nil
}
コード例 #2
0
ファイル: init.go プロジェクト: gowebabc/nedomi
func (a *Application) initVirtualHost(cfgVhost *config.VirtualHost, logs accessLogs) (err error) {
	var accessLog io.Writer
	if cfgVhost.AccessLog != "" {
		if accessLog, err = logs.openAccessLog(cfgVhost.AccessLog); err != nil {
			return fmt.Errorf("error opening access log for virtual host %s - %s",
				cfgVhost.Name, err)
		}
	}

	vhost := VirtualHost{
		Location: types.Location{
			Name:                  cfgVhost.Name,
			CacheKey:              cfgVhost.CacheKey,
			CacheKeyIncludesQuery: cfgVhost.CacheKeyIncludesQuery,
			CacheDefaultDuration:  cfgVhost.CacheDefaultDuration,
		},
	}
	if vhost.Upstream, err = a.getUpstream(cfgVhost.Upstream); err != nil {
		return err
	}

	if _, ok := a.virtualHosts[cfgVhost.Name]; ok {
		return fmt.Errorf("Virtual host or alias %s already exists", cfgVhost.Name)
	}
	a.virtualHosts[cfgVhost.Name] = &vhost

	for _, alias := range cfgVhost.Aliases {
		if _, ok := a.virtualHosts[alias]; ok {
			return fmt.Errorf("Virtual host or alias %s already exists, duplicated by alias for %s",
				alias, cfgVhost.Name)
		}
		a.virtualHosts[alias] = &vhost
	}

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

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

	if vhost.Handler, err = chainHandlers(&vhost.Location, &cfgVhost.Location, accessLog); err != nil {
		return err
	}
	var locations []*types.Location
	if locations, err = a.initFromConfigLocationsForVHost(cfgVhost.Locations, accessLog); err != nil {
		return err
	}

	if vhost.Muxer, err = NewLocationMuxer(locations); err != nil {
		return fmt.Errorf("Could not create location muxer for vhost %s - %s", cfgVhost.Name, err)
	}

	return nil
}
コード例 #3
0
ファイル: impl_test.go プロジェクト: gitter-badger/nedomi
func newStdLogger() types.Logger {
	l, _ := logger.New(config.LoggerSection{
		Type:     "std",
		Settings: []byte(`{"level":"debug"}`),
	})
	return l
}
コード例 #4
0
ファイル: utils_test.go プロジェクト: na--/nedomi
func newStdLogger() types.Logger {
	l, err := logger.New(config.NewLogger("std", []byte(`{"level":"error"}`)))
	if err != nil {
		panic(err)
	}
	return l
}
コード例 #5
0
func aFullCache(b *testing.B, size uint64) *TieredLRUCache {
	cz := getCacheZone()
	cz.StorageObjects = size
	l, _ := logger.New(config.NewLogger("nillogger", nil))
	lru := New(cz, mockRemove, l)
	fillCache(b, lru)
	return lru

}
コード例 #6
0
ファイル: init.go プロジェクト: gowebabc/nedomi
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
}
コード例 #7
0
ファイル: init.go プロジェクト: gitter-badger/nedomi
// 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
}