From 3f2eab449870fb6ed667fc55a205a68bf97a0935 Mon Sep 17 00:00:00 2001 From: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> Date: Tue, 31 May 2022 12:27:05 -0400 Subject: [PATCH] Convert NextMessage.nextMessage to a pointer (#89) ProtoBuf messages are non-copy (see https://golang.org/issues/8005), so instead of storing by value we store a pointer. Resolves https://github.com/open-telemetry/opamp-go/issues/86 --- client/internal/nextmessage.go | 14 ++++++++++---- client/internal/sender.go | 5 ++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/client/internal/nextmessage.go b/client/internal/nextmessage.go index 00c6c831..f4ec86f6 100644 --- a/client/internal/nextmessage.go +++ b/client/internal/nextmessage.go @@ -11,18 +11,24 @@ import ( // concurrency-safe interface to work with the message. type NextMessage struct { // The next message to send. - nextMessage protobufs.AgentToServer + nextMessage *protobufs.AgentToServer // Indicates that nextMessage is pending to be sent. messagePending bool // Mutex to protect the above 2 fields. messageMutex sync.Mutex } +func NewNextMessage() NextMessage { + return NextMessage{ + nextMessage: &protobufs.AgentToServer{}, + } +} + // Update applies the specified modifier function to the next message that // will be sent and marks the message as pending to be sent. func (s *NextMessage) Update(modifier func(msg *protobufs.AgentToServer)) { s.messageMutex.Lock() - modifier(&s.nextMessage) + modifier(s.nextMessage) s.messagePending = true s.messageMutex.Unlock() } @@ -35,12 +41,12 @@ func (s *NextMessage) PopPending() *protobufs.AgentToServer { if s.messagePending { // Clone the message to have a copy for sending and avoid blocking // future updates to s.NextMessage field. - msgToSend = proto.Clone(&s.nextMessage).(*protobufs.AgentToServer) + msgToSend = proto.Clone(s.nextMessage).(*protobufs.AgentToServer) s.messagePending = false // Reset fields that we do not have to send unless they change before the // next report after this one. Keep the "hash" fields. - msg := protobufs.AgentToServer{ + msg := &protobufs.AgentToServer{ InstanceUid: s.nextMessage.InstanceUid, AgentDescription: &protobufs.AgentDescription{ Hash: s.nextMessage.AgentDescription.Hash, diff --git a/client/internal/sender.go b/client/internal/sender.go index f4635424..fa928567 100644 --- a/client/internal/sender.go +++ b/client/internal/sender.go @@ -35,7 +35,10 @@ type SenderCommon struct { } func NewSenderCommon() SenderCommon { - return SenderCommon{hasPendingMessage: make(chan struct{}, 1)} + return SenderCommon{ + hasPendingMessage: make(chan struct{}, 1), + nextMessage: NewNextMessage(), + } } // ScheduleSend signals to HTTPSender that the message in NextMessage struct