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

Incorrect implement #5

Open
small-pineapple opened this issue Jan 14, 2021 · 2 comments
Open

Incorrect implement #5

small-pineapple opened this issue Jan 14, 2021 · 2 comments

Comments

@small-pineapple
Copy link

This is a race condition, only valid if Channel variable is volatile(Java keyword) or atomic

channel.Channel = ch

@jmvermeulen
Copy link

I don't get the issue, can you recommend a better approach?

@small-pineapple
Copy link
Author

small-pineapple commented May 10, 2021

type Wrap struct {
	connection           atomic.Value
}

func (this *Wrap) Connect(addr string) error {
	conn, err := amqp.Dial(addr)
	if err != nil {
		return err
	}

	this.connection.Store(conn)

	go func() {
		<-ch.NotifyClose(make(chan *amqp.Error))
                if this.Connect(addr) != nil {
                     time.AfterFunc(time.Second*3, func() {
                         if this.Connect(addr) != nil {
                             time.AfterFunc(time.Second*10, func() {
                                 if this.Connect(addr) != nil {
                                     // report a error?
                                 }
                             })
                         }
                     })
                 }
	}()

	return nil
}

connection will be concurrently access by different goroutine, that need protect by synchronzation.
Also you can do endless reconect if you like, however that is meaningless. A several times connect to amqp failure basically means the machine or network gets serious problem.
Notice that the amqp's model of connection and channel is not suitable for golang, and the driver do nothing on this also. It is hardly to say there is a general wrap way

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