Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jetStreamContext.PublishMsg memory consumption grow over time if nats server fail or restart #1593

Open
TomasVojacek opened this issue Mar 28, 2024 · 1 comment
Labels
defect Suspected defect such as a bug or regression

Comments

@TomasVojacek
Copy link

Observed behavior

Nats client costume all available memory >4GB RAM is nats server fail.

Code simply publish messages into JetStream
Input is 30messages/sec approx 100-2000bytes long

func connect(){
        natsConnection, err := nats.Connect("127.0.0.1", opts, nats.MaxReconnects(-1),nats.RetryOnFailedConnect(true))
	if err != nil {
		log.Err(err).Str("uri", config.NatsUri).Msg("failed to start connector")
		panic()
	}
 		jetStreamContext, err = jetstream.New(natsConnection)
		if err != nil {
			panic()
		}

}
func publishChan(){
 for{ 
 msg:=<-inputChan
  tryPublishMsg(msg)
 }
}
func  tryPublishMsg(msg *nats.Msg) {
        for {
	ctx, cancel := context.WithTimeout(context.Background(), publisher.publishTimeout)
	defer cancel()
	pubAck, err := jetStreamContext.PublishMsg(ctx, msg, jetstream.WithExpectStream("MYDATA"))
	if err == nil {
		if pubAck.Duplicate {
			prometheus.AppStats.Publisher.StreamDuplicates.Inc()
		}
		return
	}else{
	   	switch {
	case errors.Is(err, nats.ErrConnectionReconnecting):
		fallthrough
	case errors.Is(err, nats.ErrConnectionDraining):
		fallthrough
	case errors.Is(err, context.DeadlineExceeded):
		fallthrough
	case errors.Is(err, nats.ErrReconnectBufExceeded):
		fallthrough
	case errors.Is(err, nats.ErrConnectionClosed):
                log.Info().Msg("retrying")
		continue
	default:
	        log.Info().Err(err).Msg("unrecoverable error giving up")
		return
	  }
        }
}

Expected behavior

Memory consumption of client will be limited.
Normall memory footprint of whole application is 5-30MB.

Server and client version

nats.go 1.33.1
tested varios, versions nats server
nats server 2.10.9,2.10.11,2.10.12,2.10.4,2.11.1-preview1

Host environment

Developer machine
macOS 14.4.0
application running natively
nats running inside of docker container with mounted volume

Steps to reproduce

start nats server inside docker,
start consumer application
start application with publish client.
Wait several hours,
Nats server become overloaded (bug or setup problem) JetStream get corrupted or unresponsive.
nats-1 | [1] 2024/03/21 12:48:03.248044 [WRN] Internal subscription on "$JS.API.CONSUMER.INFO.MYDATA.FORWARDER" took too long: 5.000799336s
nats-1 | [1] 2024/03/21 12:48:03.248082 [WRN] Internal subscription on "$JS.API.CONSUMER.INFO.MYDATA.FORWARDER" took too long: 5.310615294s
nats-1 | [1] 2024/03/21 12:48:03.845698 [WRN] 172.18.0.4:46162 - cid:17 - Readloop processing time: 14.906449381s

Nats client start to cache internally messages that it fail to deliver to server.
profile002

@TomasVojacek TomasVojacek added the defect Suspected defect such as a bug or regression label Mar 28, 2024
@piotrpio
Copy link
Collaborator

piotrpio commented Apr 2, 2024

Hello @TomasVojacek , thanks for reporting the issue, I'll be looking at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Suspected defect such as a bug or regression
Projects
None yet
Development

No branches or pull requests

2 participants