Пример #1
0
// NewServer construct rest server listening specific host and port and routes requests by passed routes
func NewServer(props properties.Properties, routes PathHandlers) error {
	bind := props.Get("rest.address") + ":" + props.Get("rest.port")

	log.Println("start rest server on", bind)
	listener, err := net.Listen("tcp", bind)
	if err != nil {
		return err
	}

	s := &server{
		listener: listener,
		m:        &sync.Mutex{},
		alive:    true,
		wg:       &sync.WaitGroup{},
	}

	kit.SafeGo(func() error {
		router := mux.NewRouter()
		for path := range routes {
			mh := handlers.MethodHandler{}
			for method := range routes[path] {
				log.Printf("setup rest handler %v %v\n", method, path)
				mh[method] = s.wrapHandler(routes[path][method])
			}
			router.Path(path).Handler(mh)
		}

		handler := http.Handler(router)

		compression, err := strconv.ParseBool(props.Get("rest.compression"))
		if err == nil && compression {
			log.Println("enable compression for rest server")
			handler = handlers.CompressHandler(handler)
		} else {
			log.Println("no compression for rest server")
		}

		log.Println("rest server serves")
		err = http.Serve(listener, handler)
		if err != nil {
			return err
		}

		return nil
	})

	kit.SafeGo(func() error {
		<-registry.DoneChannel()
		s.Stop()
		return nil
	})

	return nil
}
Пример #2
0
func NewPostgresRepository(props properties.Properties) (Repository, error) {
	cs := kit.PostgresConnectionString(props)
	log.Printf("postgres connection string `%s`\n", cs)

	db, err := sql.Open("postgres", cs)
	if err != nil {
		return nil, err
	}

	repo := &postgresRepository{
		connectionString: cs,
		db:               db,

		listener:       nil,
		listenChannels: map[string]map[chan string]struct{}{},

		m:     &sync.RWMutex{},
		alive: true,
	}

	kit.SafeGo(func() error {
		<-registry.DoneChannel()
		repo.Close()
		return nil
	})

	return repo, nil
}
Пример #3
0
func (d *direct) Forever() {
	doneCh := registry.DoneChannel()

	var err error
	processed := true

	kit.SafeGo(func() error {
		for {
			if processed {
				select {
				case <-doneCh:
					return nil
				default:
				}

				processed, err = d.iter()
				if err != nil {
					return err
				}
			} else {
				select {
				case <-doneCh:
					return nil

				case <-d.resumeCh:
					processed, err = d.iter()
					if err != nil {
						return err
					}
				}
			}
		}

	})
}
Пример #4
0
func (repo *postgresRepository) ensureListener() {
	repo.m.Lock()
	defer repo.m.Unlock()

	if repo.listener == nil {
		repo.listener = pq.NewListener(repo.connectionString, time.Second, time.Minute, nil)

		kit.SafeGo(repo.listenDaemon)
	}
}
Пример #5
0
func NewFixedDelayScheduler(initialDelay time.Duration, delay time.Duration) Scheduler {
	scheduler := &fixedDelayScheduler{
		initialDelay: initialDelay,
		delay:        delay,

		alive:  true,
		doneCh: make(chan struct{}),
		m:      &sync.Mutex{},
		wg:     &sync.WaitGroup{},
	}

	kit.SafeGo(func() error {
		<-registry.DoneChannel()
		scheduler.Stop()
		return nil
	})

	return scheduler
}
Пример #6
0
func NewDirectEventStore(repo repository.Repository, stream string, consumer string) (EventStore, error) {
	es := &direct{
		repo: repo,

		stream:   stream,
		consumer: consumer,

		locker: &sync.RWMutex{},
		types:  map[string]reflect.Type{},

		aggregators:    map[string]*list.List{},
		rawAggregators: map[string][]AggregatorHandler{},

		observers:    map[string]*list.List{},
		rawObservers: map[string][]ObserverHandler{},

		newEventChannelName: "es_" + stream,
		resumeCh:            make(chan struct{}, 1),
	}

	kit.SafeGo(es.listen)

	err := es.setupDB()
	if err != nil {
		return nil, err
	}

	lastID, err := fetchLastID(repo.DB(), stream, consumer)
	if err != nil {
		return nil, err
	}

	es.lastID = lastID

	return es, nil
}