Add logs for receiving a message, descriptive messages for api

This commit is contained in:
mcrakhman 2022-08-02 20:50:35 +02:00 committed by Mikhail Iudin
parent 0e9f5423d3
commit 85cdb1c189
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 35 additions and 5 deletions

View File

@ -94,7 +94,7 @@ func (s *service) createDocument(w http.ResponseWriter, req *http.Request) {
query = req.URL.Query()
text = query.Get("text")
)
treeId, err := s.documentService.CreateDocument(context.Background(), text)
treeId, err := s.documentService.CreateDocument(context.Background(), fmt.Sprintf("created document with id: %s", text))
if err != nil {
sendText(w, http.StatusInternalServerError, err.Error())
return
@ -113,7 +113,7 @@ func (s *service) appendDocument(w http.ResponseWriter, req *http.Request) {
sendText(w, http.StatusInternalServerError, err.Error())
return
}
sendText(w, http.StatusOK, "")
sendText(w, http.StatusOK, fmt.Sprintf("updated document with id: %s with text: %s", treeId, text))
}
func sendText(r http.ResponseWriter, code int, body string) {

View File

@ -94,6 +94,11 @@ func (s *service) UpdateDocument(ctx context.Context, id, text string) (err erro
if err != nil {
return err
}
log.With(
zap.String("id", id),
zap.Strings("heads", heads),
zap.String("header", header.String())).
Debug("document updated in the database")
return s.messageService.SendMessage("", syncpb.WrapHeadUpdate(&syncpb.SyncHeadUpdate{
Heads: heads,
@ -139,7 +144,11 @@ func (s *service) CreateDocument(ctx context.Context, text string) (id string, e
if err != nil {
return err
}
log.With(
zap.String("id", id),
zap.Strings("heads", heads),
zap.String("header", header.String())).
Debug("document created in the database")
return nil
})
if err != nil {

View File

@ -53,8 +53,8 @@ func (s *service) Name() (name string) {
}
func (s *service) Run(ctx context.Context) (err error) {
go s.runSender(ctx)
go s.runReceiver(ctx)
//go s.runSender(ctx)
//go s.runReceiver(ctx)
return nil
}
@ -84,6 +84,10 @@ func (s *service) UnregisterMessageSender(peerId string) {
}
func (s *service) HandleMessage(peerId string, msg *syncpb.SyncContent) error {
log.With(
zap.String("peerId", peerId),
zap.String("message", msgType(msg))).
Debug("handling message from peer")
return s.receiveBatcher.Add(&message{
peerId: peerId,
content: msg,
@ -91,6 +95,10 @@ func (s *service) HandleMessage(peerId string, msg *syncpb.SyncContent) error {
}
func (s *service) SendMessage(peerId string, msg *syncpb.SyncContent) error {
log.With(
zap.String("peerId", peerId),
zap.String("message", msgType(msg))).
Debug("sending message to peer")
return s.sendBatcher.Add(&message{
peerId: peerId,
content: msg,
@ -150,3 +158,16 @@ func (s *service) sendMessage(typedMsg *message) {
}
ch <- typedMsg.content
}
func msgType(content *syncpb.SyncContent) string {
msg := content.GetMessage()
switch {
case msg.GetFullSyncRequest() != nil:
return "FullSyncRequest"
case msg.GetFullSyncResponse() != nil:
return "FullSyncResponse"
case msg.GetHeadUpdate() != nil:
return "HeadUpdate"
}
return "UnknownMessage"
}