Debian Codegraph @

amqptest/dial.go

raw
1 package amqptest
2
3 import (
4 "sync"
5 "time"
6
7 "github.com/NeowayLabs/wabbit"
8 "github.com/NeowayLabs/wabbit/amqptest/server"
9 "github.com/NeowayLabs/wabbit/utils"
10 "github.com/pborman/uuid"
11 )
12
13 const (
14 // 1 second
15 defaultReconnectDelay = 1
16 )
17
18 // Conn is the fake AMQP connection
19 type Conn struct {
20 amqpuri string
21 isConnected bool
22 ConnID string
23 errSpread *utils.ErrBroadcast
24 errChan chan wabbit.Error
25 defErrDone chan bool
26 mu *sync.Mutex
27 hasAutoRedial bool
28 amqpServer *server.AMQPServer
29
30 dialFn func() error
31 }
32
33 // Dial mock the connection dialing to rabbitmq and
34 // returns the established connection or error if something goes wrong
35 func Dial(amqpuri string) (*Conn, error) {
36 conn := &Conn{
37 amqpuri: amqpuri,
38 errSpread: utils.NewErrBroadcast(),
39 errChan: make(chan wabbit.Error),
40 defErrDone: make(chan bool),
41 mu: &sync.Mutex{},
42 }
43
44 conn.errSpread.Add(conn.errChan)
45
46 conn.dialFn = func() error {
47 var err error
48 conn.ConnID = uuid.New()
49 conn.amqpServer, err = server.Connect(amqpuri, conn.ConnID, conn.errSpread)
50
51 if err != nil {
52 return err
53 }
54
55 // concurrent access with Close method
56 conn.mu.Lock()
57 conn.isConnected = true
58 conn.mu.Unlock()
59
60 // by default, we discard any errors
61 // send something to defErrDone to destroy
62 // this goroutine and start consume the errors
63 go func() {
64 for {
65 select {
66 case <-conn.errChan:
67 case <-conn.defErrDone:
68 conn.mu.Lock()
69 if conn.hasAutoRedial {
70 conn.mu.Unlock()
71 return
72 }
73 conn.mu.Unlock()
74 // Drain the errChan channel before
75 // the exit.
76 for {
77 if _, ok := <-conn.errChan; !ok {
78 return
79 }
80 }
81 }
82 }
83 }()
84
85 return nil
86 }
87
88 err := conn.dialFn()
89
90 if err != nil {
91 return nil, err
92 }
93
94 return conn, nil
95 }
96
97 // NotifyClose publishs notifications about server or client errors in the given channel
98 func (conn *Conn) NotifyClose(c chan wabbit.Error) chan wabbit.Error {
99 conn.errSpread.Add(c)
100 return c
101 }
102
103 // AutoRedial mock the reconnection faking a delay of 1 second
104 func (conn *Conn) AutoRedial(outChan chan wabbit.Error, done chan bool) {
105 if !conn.hasAutoRedial {
106 conn.mu.Lock()
107 conn.hasAutoRedial = true
108 conn.mu.Unlock()
109 conn.defErrDone <- true
110 }
111
112 go func() {
113 var err wabbit.Error
114 var attempts uint
115
116 select {
117 case amqpErr := <-conn.errChan:
118 err = amqpErr
119
120 if amqpErr == nil {
121 // Gracefull connection close
122 return
123 }
124 lattempts:
125 // send the error to client
126 outChan <- err
127
128 if attempts > 60 {
129 attempts = 0
130 }
131
132 // Wait n Seconds where n == attempts...
133 time.Sleep(time.Duration(attempts) * time.Second)
134
135 connErr := conn.dialFn()
136
137 if connErr != nil {
138 attempts++
139 goto lattempts
140 }
141
142 // enabled AutoRedial on the new connection
143 conn.AutoRedial(outChan, done)
144 done <- true
145 return
146 }
147 }()
148 }
149
150 // Close the fake connection
151 func (conn *Conn) Close() error {
152 conn.mu.Lock()
153 defer conn.mu.Unlock()
154
155 if conn.isConnected {
156 // Disconnect from the server.
157 if err := server.Close(conn.amqpuri, conn.ConnID); err != nil {
158 return err
159 }
160 conn.isConnected = false
161 conn.amqpServer = nil
162 }
163
164 // enables AutoRedial to gracefully shutdown
165 // This isn't wabbit stuff. It's the rabbitmq/amqp way of notify the shutdown
166 if conn.hasAutoRedial {
167 conn.errSpread.Write(nil)
168 } else {
169 conn.errSpread.Delete(conn.errChan)
170 close(conn.errChan)
171 conn.defErrDone <- true
172 }
173
174 return nil
175 }
176
177 // Channel creates a new fake channel
178 func (conn *Conn) Channel() (wabbit.Channel, error) {
179 return conn.amqpServer.CreateChannel(conn.ConnID, conn)
180 }