package buscompose import ( "fmt" "strings" "time" "git.novox.be/novox/mesh-sdk/go/seatdef" ) // The work queue's settings where a seat's accepted kinds name none: a week of asks, a delivery given up after five // handings, a minute before an unacknowledged one is handed again. const ( SeatQueueDefaultMaxAge = 7 * 24 * time.Hour SeatQueueDefaultAckWait = time.Minute SeatQueueDefaultDelivers = 5 ) // SeatQueueStreamName is the stream holding a seat's inbound work, named after the seat, never its holder. func SeatQueueStreamName(seat string) string { return "SEAT_" + seatUpper(seat) } // SeatQueueWorkerName is the one worker a seat's holders share on its work queue. func SeatQueueWorkerName(seat string) string { return "SEAT_" + seatUpper(seat) + "_worker" } func seatUpper(seat string) string { return strings.ToUpper(strings.NewReplacer("-", "_", ".", "_").Replace(seat)) } // SeatQueue derives a seat's work-queue stream and its one shared worker from its accepted kinds' work-queue settings // (design 47 H2 ยง777; H-A-350 DMC-1, H-A-362), the one derivation every part takes: max-age, max-msgs and // duplicates-window bound the stream; ack-wait, max-deliveries, max-ack-pending and in-progress the worker. A setting // named by two accepted kinds with two values is refused: one stream and one worker carry one value. A seat that // accepts nothing has no queue (ok false). func SeatQueue(seat string, accepts []seatdef.Message) (stream Stream, worker Consumer, ok bool, err error) { if len(accepts) == 0 { return Stream{}, Consumer{}, false, nil } durations := map[string]string{} counts := map[string]int{} for _, a := range accepts { if ps := a.QueueProblems("accepts " + a.Name); len(ps) > 0 { return Stream{}, Consumer{}, false, fmt.Errorf("the seat %s's work queue: %w", seat, ps) } for name, v := range map[string]string{"max-age": a.MaxAge, "duplicates-window": a.DuplicatesWindow, "ack-wait": a.AckWait, "in-progress": a.InProgress} { if v == "" { continue } if was, seen := durations[name]; seen && was != v { return Stream{}, Consumer{}, false, fmt.Errorf("the seat %s's work queue: %s is %s and %s in two accepted kinds", seat, name, was, v) } durations[name] = v } for name, v := range map[string]int{"max-msgs": a.MaxMsgs, "max-ack-pending": a.MaxAckPending, "max-deliveries": a.MaxDeliveries} { if v == 0 { continue } if was, seen := counts[name]; seen && was != v { return Stream{}, Consumer{}, false, fmt.Errorf("the seat %s's work queue: %s is %d and %d in two accepted kinds", seat, name, was, v) } counts[name] = v } } seconds := func(name string, def time.Duration) int { if v, set := durations[name]; set { d, _ := time.ParseDuration(v) // QueueProblems read it return int(d / time.Second) } return int(def / time.Second) } delivers := SeatQueueDefaultDelivers if v, set := counts["max-deliveries"]; set { delivers = v } subject := "mesh.seat." + seat + ".accept.>" stream = Stream{ Name: SeatQueueStreamName(seat), Subjects: []string{subject}, Retention: RetentionWorkQueue, MaxAge: seconds("max-age", SeatQueueDefaultMaxAge), MaxMsgs: counts["max-msgs"], DuplicatesSeconds: seconds("duplicates-window", 0), Why: fmt.Sprintf("work submitted to the %s seat; one holder consumes it, and it queues while nobody does", seat), } worker = Consumer{ Name: SeatQueueWorkerName(seat), Stream: stream.Name, Filters: []string{subject}, AckWaitSeconds: seconds("ack-wait", SeatQueueDefaultAckWait), MaxDeliver: delivers, MaxAckPending: counts["max-ack-pending"], InProgressSeconds: seconds("in-progress", 0), Why: fmt.Sprintf("every holder of %s pulls its asks from this one worker and acknowledges after the work is "+ "done, so a crash mid-work redelivers rather than loses", seat), } return stream, worker, true, nil }