func (w *WriteClient) write(data []byte) error { msgCount := atomic.AddUint64(&w.msgCount, 1) md := model.NewMetaData(w.id, msgCount, model.NewUUID()) //TODO transactionid should be supplied entry := model.NewLogEntry(md, data) request := model.NewWriteRequest(entry) conn := w.connectionPool.Connection() if _, err := conn.Write(encoder.EncodePayload(request)); err != nil { return err } return nil }
//Subscribe creates a subsciption on the log, which in realtime outputs all written log entries to the return channel from the time of subscription func (r *ReadClient) Subscribe() <-chan model.LogEntry { subscribeChan := make(chan model.LogEntry) go func(chan<- model.LogEntry) { for _, conn := range r.connectionPool.AllConnections() { req := model.NewSubscribeRequest() if _, err := conn.Write(encoder.EncodePayload(req)); err != nil { log.Fatal(err) } go writeLogEntryToChan(subscribeChan, conn) } }(subscribeChan) return subscribeChan }