MCPcopy Index your code
hub / github.com/coder/coder / writeOne

Method writeOne

enterprise/tailnet/pgcoord.go:418–479  ·  view source on GitHub ↗
(tun tunnel)

Source from the content-addressed store, hash-verified

416}
417
418func (t *tunneler) writeOne(tun tunnel) error {
419 var err error
420 switch {
421 case tun.dst == uuid.Nil:
422 var deleted []database.DeleteAllTailnetTunnelsRow
423 deleted, err = t.store.DeleteAllTailnetTunnels(t.ctx, database.DeleteAllTailnetTunnelsParams{
424 SrcID: tun.src,
425 CoordinatorID: t.coordinatorID,
426 })
427 t.logger.Debug(t.ctx, "deleted all tunnels",
428 slog.F("src_id", tun.src),
429 slog.Error(err),
430 )
431 if err == nil {
432 for _, row := range deleted {
433 publishTunnelUpdate(t.ctx, t.pubsub, t.logger, row.SrcID, row.DstID)
434 }
435 }
436 case tun.active:
437 _, err = t.store.UpsertTailnetTunnel(t.ctx, database.UpsertTailnetTunnelParams{
438 CoordinatorID: t.coordinatorID,
439 SrcID: tun.src,
440 DstID: tun.dst,
441 })
442 t.logger.Debug(t.ctx, "upserted tunnel",
443 slog.F("src_id", tun.src),
444 slog.F("dst_id", tun.dst),
445 slog.Error(err),
446 )
447 if err == nil {
448 publishTunnelUpdate(t.ctx, t.pubsub, t.logger, tun.src, tun.dst)
449 }
450 case !tun.active:
451 _, err = t.store.DeleteTailnetTunnel(t.ctx, database.DeleteTailnetTunnelParams{
452 CoordinatorID: t.coordinatorID,
453 SrcID: tun.src,
454 DstID: tun.dst,
455 })
456 t.logger.Debug(t.ctx, "deleted tunnel",
457 slog.F("src_id", tun.src),
458 slog.F("dst_id", tun.dst),
459 slog.Error(err),
460 )
461 // writeOne should be idempotent
462 if xerrors.Is(err, sql.ErrNoRows) {
463 return nil // No row deleted, skip publish.
464 }
465 if err == nil {
466 publishTunnelUpdate(t.ctx, t.pubsub, t.logger, tun.src, tun.dst)
467 }
468 default:
469 panic("unreachable")
470 }
471 if err != nil && !database.IsQueryCanceledError(err) {
472 t.logger.Error(t.ctx, "write tunnel to database",
473 slog.F("src_id", tun.src),
474 slog.F("dst_id", tun.dst),
475 slog.F("active", tun.active),

Callers 1

workerMethod · 0.95

Calls 7

IsQueryCanceledErrorFunction · 0.92
publishTunnelUpdateFunction · 0.85
UpsertTailnetTunnelMethod · 0.65
DeleteTailnetTunnelMethod · 0.65
ErrorMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected