Пример #1
0
func newRaft(home, addr string, peerStore *peerStoreWrapper, fsm raft.FSM, trans *raft.NetworkTransport, cfg *raft.Config) (*Raft, error) {
	if err := os.MkdirAll(home, 0700); err != nil {
		return nil, err
	}
	db, err := raftboltdb.NewBoltStore(filepath.Join(home, "raft.db"))
	if err != nil {
		return nil, fmt.Errorf("error initializing raft db: %v", err)
	}

	snapStore, err := raft.NewFileSnapshotStore(filepath.Join(home, "snapshots"), 5, nil)
	if err != nil {
		return nil, fmt.Errorf("error intializing raft snap store: %v", err)
	}

	r, err := raft.NewRaft(cfg, fsm, db, db, snapStore, peerStore, trans)
	if err != nil {
		return nil, err
	}

	raft := &Raft{
		r:          r,
		peers:      peerStore,
		trans:      trans,
		db:         db,
		shutdownCh: make(chan struct{}),
		pub:        pubsub.NewPublisher(defaultTimeout, 1),
	}
	go raft.waitLeader()
	return raft, nil
}
Пример #2
0
// New creates new JSONFileLogger which writes to filename
func New(ctx logger.Context) (logger.Logger, error) {
	log, err := os.OpenFile(ctx.LogPath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
	if err != nil {
		return nil, err
	}
	var capval int64 = -1
	if capacity, ok := ctx.Config["max-size"]; ok {
		var err error
		capval, err = units.FromHumanSize(capacity)
		if err != nil {
			return nil, err
		}
	}
	var maxFiles int = 1
	if maxFileString, ok := ctx.Config["max-file"]; ok {
		maxFiles, err = strconv.Atoi(maxFileString)
		if err != nil {
			return nil, err
		}
		if maxFiles < 1 {
			return nil, fmt.Errorf("max-files cannot be less than 1.")
		}
	}
	return &JSONFileLogger{
		f:            log,
		buf:          bytes.NewBuffer(nil),
		ctx:          ctx,
		capacity:     capval,
		n:            maxFiles,
		readers:      make(map[*logger.LogWatcher]struct{}),
		notifyRotate: pubsub.NewPublisher(0, 1),
	}, nil
}
Пример #3
0
// collect registers the container with the collector and adds it to
// the event loop for collection on the specified interval returning
// a channel for the subscriber to receive on.
func (s *statsCollector) collect(c *container.Container) chan interface{} {
	s.m.Lock()
	defer s.m.Unlock()
	publisher, exists := s.publishers[c]
	if !exists {
		publisher = pubsub.NewPublisher(100*time.Millisecond, 1024)
		s.publishers[c] = publisher
	}
	return publisher.Subscribe()
}
Пример #4
0
// collect registers the container with the collector and adds it to
// the event loop for collection on the specified interval returning
// a channel for the subscriber to receive on.
func (s *statsCollector) collect(c runtime.Container) chan interface{} {
	s.m.Lock()
	defer s.m.Unlock()
	publisher, exists := s.publishers[c.ID()]
	if !exists {
		pub := pubsub.NewPublisher(100*time.Millisecond, 1024)
		publisher = &statsPair{ct: c, pub: pub}
		s.publishers[c.ID()] = publisher
	}
	return publisher.pub.Subscribe()
}
Пример #5
0
//NewRotateFileWriter creates new RotateFileWriter
func NewRotateFileWriter(logPath string, capacity int64, maxFiles int) (*RotateFileWriter, error) {
	log, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
	if err != nil {
		return &RotateFileWriter{}, err
	}

	return &RotateFileWriter{
		f:            log,
		capacity:     capacity,
		maxFiles:     maxFiles,
		notifyRotate: pubsub.NewPublisher(0, 1),
	}, nil
}
Пример #6
0
// New creates new JSONFileLogger which writes to filename passed in
// on given context.
func New(ctx logger.Context) (logger.Logger, error) {
	var capval int64 = -1
	if capacity, ok := ctx.Config["max-size"]; ok {
		var err error
		capval, err = units.FromHumanSize(capacity)
		if err != nil {
			return nil, err
		}
	}
	var maxFiles = 1
	if maxFileString, ok := ctx.Config["max-file"]; ok {
		var err error
		maxFiles, err = strconv.Atoi(maxFileString)
		if err != nil {
			return nil, err
		}
		if maxFiles < 1 {
			return nil, fmt.Errorf("max-file cannot be less than 1")
		}
	}

	writer, err := loggerutils.NewRotateFileWriter(ctx.LogPath, capval, maxFiles)
	if err != nil {
		return nil, err
	}

	var extra []byte
	if attrs := ctx.ExtraAttributes(nil); len(attrs) > 0 {
		var err error
		extra, err = json.Marshal(attrs)
		if err != nil {
			return nil, err
		}
	}

	return &JSONFileLogger{
		buf:           bytes.NewBuffer(nil),
		writer:        writer,
		readers:       make(map[*logger.LogWatcher]struct{}),
		extra:         extra,
		writeNotifier: pubsub.NewPublisher(0, 10),
	}, nil
}
Пример #7
0
func main() {
	var id string
	flag.StringVar(&id, "id", "", "ID to subscribe from")
	flag.Parse()
	props := property.Init()

	evStore, err := evstore.Dial(props["mongodb.url"], props["mongodb.db"], props["mongodb.stream"])
	if err != nil {
		log.Fatalln("Error connecting to event store. ", err)
	}
	wsServer := wsock.NewServer(props["current.uri"])
	if wsServer == nil {
		log.Fatalln("Error creating new websocket server")
	}
	sState = ScalarState{}
	sState.state = make(map[int]map[int]*bson.M)
	sState.mx = &sync.Mutex{}
	isCurrent = false
	stateUpdate := pubsub.NewPublisher(time.Millisecond*100, 1024)
	err = evStore.Listenner2().Subscribe2("scalar", scalarHandler)
	if err != nil {
		log.Fatalln("Error subscribing for changes", err)
	}
	ctx1, cancel := context.WithCancel(context.Background())
	ctx := context.WithValue(ctx1, "stateUpdate", stateUpdate)
	defer cancel()
	sState.lastID = evStore.Listenner2().GetLastID()
	log.Println("Before Listen call")
	go evStore.Listenner2().Listen(ctx, id)

	go processClientConnection(ctx, wsServer)
	go wsServer.Listen()

	//http.Handle(props["static.url"], http.FileServer(http.Dir("webroot")))
	err = http.ListenAndServe(props["current.url"], nil)
	evStore.Close()
}
Пример #8
0
// New returns new *Events instance
func New() *Events {
	return &Events{
		events: make([]eventtypes.Message, 0, eventsLimit),
		pub:    pubsub.NewPublisher(100*time.Millisecond, bufferSize),
	}
}
Пример #9
0
// New returns new *Events instance
func New() *Events {
	return &Events{
		events: make([]*jsonmessage.JSONMessage, 0, eventsLimit),
		pub:    pubsub.NewPublisher(100*time.Millisecond, 1024),
	}
}
Пример #10
0
func TestScalarHandler(t *testing.T) {
	Convey("When pass empty message slice to scalarHandler", t, func() {
		var msgs []interface{}
		Convey("It should not panic", func() {
			So(func() { scalarHandler(context.Background(), msgs) }, ShouldNotPanic)
		})
	})
	Convey("When pass message with int boxID, varID", t, func() {
		var msgs []interface{}
		sState = ScalarState{}
		sState.state = make(map[int]map[int]*bson.M)
		sState.mx = &sync.Mutex{}
		stateUpdateChannel := pubsub.NewPublisher(time.Millisecond*100, 1024)
		ctx := context.WithValue(context.Background(), "stateUpdate", stateUpdateChannel)
		id := bson.NewObjectId()
		msg := bson.M{"_id": id, "tag": "scalar", "event": bson.M{"box_id": 1, "var_id": 1, "value": 1.5}}
		msgs = append(msgs, msg)
		Convey("It should not panic with type assertion", func() {
			So(func() { scalarHandler(ctx, msgs) }, ShouldNotPanic)
		})
	})
	Convey("When pass message with float64 boxID, varID", t, func() {
		var msgs []interface{}
		sState = ScalarState{}
		sState.state = make(map[int]map[int]*bson.M)
		sState.mx = &sync.Mutex{}
		stateUpdateChannel := pubsub.NewPublisher(time.Millisecond*100, 1024)
		ctx := context.WithValue(context.Background(), "stateUpdate", stateUpdateChannel)
		id := bson.NewObjectId()
		msg := bson.M{"_id": id, "tag": "scalar", "event": bson.M{"box_id": 1.0, "var_id": 1.0, "value": 1.5}}
		msgs = append(msgs, msg)
		Convey("It should not panic with type assertion", func() {
			So(func() { scalarHandler(ctx, msgs) }, ShouldNotPanic)
		})
	})
	Convey("When pass message with string boxID, varID", t, func() {
		var msgs []interface{}
		sState = ScalarState{}
		sState.state = make(map[int]map[int]*bson.M)
		sState.mx = &sync.Mutex{}
		stateUpdateChannel := make(chan *bson.M, 256)
		ctx := context.WithValue(context.Background(), "stateUpdateChannel", stateUpdateChannel)
		id := bson.NewObjectId()
		msg := bson.M{"_id": id, "tag": "scalar", "event": bson.M{"box_id": "1.0", "var_id": "1.0", "value": 1.5}}
		msgs = append(msgs, msg)
		Convey("It should not panic with type assertion", func() {
			So(func() { scalarHandler(ctx, msgs) }, ShouldNotPanic)
		})
	})
	Convey("When pass message with int boxID and string varID", t, func() {
		var msgs []interface{}
		sState = ScalarState{}
		sState.state = make(map[int]map[int]*bson.M)
		sState.mx = &sync.Mutex{}
		stateUpdateChannel := make(chan *bson.M, 256)
		ctx := context.WithValue(context.Background(), "stateUpdateChannel", stateUpdateChannel)
		id := bson.NewObjectId()
		msg := bson.M{"_id": id, "tag": "scalar", "event": bson.M{"box_id": 1, "var_id": "1.0", "value": 1.5}}
		msgs = append(msgs, msg)
		Convey("It should not panic with type assertion", func() {
			So(func() { scalarHandler(ctx, msgs) }, ShouldNotPanic)
		})
	})
}