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

memory leak in azure eventhub endpoint #667

Open
eduardotang opened this issue Dec 5, 2022 · 1 comment
Open

memory leak in azure eventhub endpoint #667

eduardotang opened this issue Dec 5, 2022 · 1 comment

Comments

@eduardotang
Copy link

connection should be reused instead of creating a new connection in every send
causing

  • usage of memory from a few mb to gb for the same workload
  • eventually hitting the connection limit of eventhub even for a small workload

i have verifed with below changes

type EvenHubConn struct {
	mu   sync.Mutex
	ep   Endpoint
	conn *eventhub.Hub
}

func (conn *EvenHubConn) close() {
	if conn.conn != nil {
		conn.conn.Close(context.Background())
		conn.conn = nil
	}
}

// Send sends a message
func (conn *EvenHubConn) Send(msg string) error {
	conn.mu.Lock()
	defer conn.mu.Unlock()

	if conn.conn == nil {
		hub, err := eventhub.NewHubFromConnectionString(conn.ep.EventHub.ConnectionString)

		if err != nil {
			return err
		}
		conn.conn = hub
	}
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()

	// parse json again to get out info for our kafka key
	key := gjson.Get(msg, "key")
	id := gjson.Get(msg, "id")
	keyValue := fmt.Sprintf("%s-%s", key.String(), id.String())

	evtHubMsg := eventhub.NewEventFromString(msg)
	evtHubMsg.PartitionKey = &keyValue
	err := conn.conn.Send(ctx, evtHubMsg)
	if err != nil {
		conn.close()
		return err
	}

	return nil
}
@iwpnd
Copy link
Contributor

iwpnd commented Dec 29, 2022

Hey @lokisisland, do you think you can take a look at this? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants