| 301 | } |
| 302 | |
| 303 | func (store *Storage) EnableUpdatePipe(mode updatepipe.BackendMode) error { |
| 304 | if store.updPipe != nil { |
| 305 | return nil |
| 306 | } |
| 307 | |
| 308 | switch store.driver { |
| 309 | case "sqlite3", "sqlite": |
| 310 | dbId := sha1.Sum([]byte(strings.Join(store.dsn, " "))) |
| 311 | sockPath := filepath.Join( |
| 312 | config.RuntimeDirectory, |
| 313 | fmt.Sprintf("sql-%s.sock", hex.EncodeToString(dbId[:]))) |
| 314 | store.log.DebugMsg("using unix socket for external updates", "path", sockPath) |
| 315 | store.updPipe = &updatepipe.UnixSockPipe{ |
| 316 | SockPath: sockPath, |
| 317 | Log: store.log.Sublogger("updpipe"), |
| 318 | } |
| 319 | case "postgres": |
| 320 | store.log.DebugMsg("using PostgreSQL broker for external updates") |
| 321 | ps, err := pubsub.NewPQ(strings.Join(store.dsn, " ")) |
| 322 | if err != nil { |
| 323 | return fmt.Errorf("enable_update_pipe: %w", err) |
| 324 | } |
| 325 | ps.Log = store.log.Sublogger("updpipe/pubsub") |
| 326 | pipe := &updatepipe.PubSubPipe{ |
| 327 | PubSub: ps, |
| 328 | Log: store.log.Sublogger("updpipe"), |
| 329 | } |
| 330 | store.Back.UpdateManager().ExternalUnsubscribe = pipe.Unsubscribe |
| 331 | store.Back.UpdateManager().ExternalSubscribe = pipe.Subscribe |
| 332 | store.updPipe = pipe |
| 333 | default: |
| 334 | return errors.New("imapsql: driver does not have an update pipe implementation") |
| 335 | } |
| 336 | |
| 337 | inbound := make(chan mess.Update, 32) |
| 338 | outbound := make(chan mess.Update, 10) |
| 339 | store.outboundUpds = outbound |
| 340 | |
| 341 | if mode == updatepipe.ModeReplicate { |
| 342 | if err := store.updPipe.Listen(inbound); err != nil { |
| 343 | store.updPipe = nil |
| 344 | return err |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if err := store.updPipe.InitPush(); err != nil { |
| 349 | store.updPipe = nil |
| 350 | return err |
| 351 | } |
| 352 | |
| 353 | store.Back.UpdateManager().SetExternalSink(outbound) |
| 354 | |
| 355 | store.updPushStop = make(chan struct{}, 1) |
| 356 | go func() { |
| 357 | defer func() { |
| 358 | // Ensure we sent all outbound updates. |
| 359 | for upd := range outbound { |
| 360 | if err := store.updPipe.Push(upd); err != nil { |