Shutdown flushes pending message sends and disconnects the Topic. It only returns after all pending messages have been sent.
(ctx context.Context)
| 279 | // Shutdown flushes pending message sends and disconnects the Topic. |
| 280 | // It only returns after all pending messages have been sent. |
| 281 | func (t *Topic) Shutdown(ctx context.Context) (err error) { |
| 282 | ctx, span := t.tracer.Start(ctx, "Topic.Shutdown") |
| 283 | defer func() { t.tracer.End(ctx, span, err) }() |
| 284 | |
| 285 | t.mu.Lock() |
| 286 | if errors.Is(t.err, errTopicShutdown) { |
| 287 | defer t.mu.Unlock() |
| 288 | return t.err |
| 289 | } |
| 290 | t.err = errTopicShutdown |
| 291 | t.mu.Unlock() |
| 292 | c := make(chan struct{}) |
| 293 | go func() { |
| 294 | defer close(c) |
| 295 | t.batcher.Shutdown() |
| 296 | }() |
| 297 | select { |
| 298 | case <-ctx.Done(): |
| 299 | case <-c: |
| 300 | } |
| 301 | t.cancel() |
| 302 | if err := t.driver.Close(); err != nil { |
| 303 | return wrapError(t.driver, err) |
| 304 | } |
| 305 | return ctx.Err() |
| 306 | } |
| 307 | |
| 308 | // As converts i to driver-specific types. |
| 309 | // See https://gocloud.dev/concepts/as/ for background information, the "As" |