package host import ( "context" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostwrite" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/schedule" ) func ClaimTimerDelivery(set *ClaimProducerSet, newID func() string) schedule.DeliveryAdmission { return func(ctx context.Context, work hostwrite.Work, rt registry.Route, t registry.Timer) (context.Context, func(), error) { if set == nil || newID == nil { return nil, nil, hostwrite.ErrUnavailable } selected, err := hostbox.Selected(work) if err != nil { return nil, nil, err } if selected.Host != rt.Host || t.Host != rt.Host || selected.Captured.Incarnation != rt.ClaimIncarnation || selected.Captured.Stamp != rt.ClaimStamp || t.ClaimIncarnation != rt.ClaimIncarnation || t.ClaimStamp != rt.ClaimStamp { return nil, nil, hostbox.ErrChanged } set.mu.Lock() var chosen *ClaimProducers for _, p := range set.items { if p.target.Host == selected.Host && p.target.Incarnation == selected.Captured.Incarnation && p.target.Stamp == selected.Captured.Stamp && p.target.Session == selected.Session && p.target.Box == selected.Box { if chosen != nil { set.mu.Unlock() return nil, nil, hostbox.ErrChanged } chosen = p } } set.mu.Unlock() if chosen == nil { return nil, nil, hostwrite.ErrUnavailable } var child context.Context var finish func(error) err = work.With(ctx, hostwrite.Identity{}, func() error { var e error child, finish, e = chosen.Register(ctx, chosen.target, "timer-"+newID()) return e }) if err != nil { return nil, nil, err } return child, func() { finish(nil) }, nil } }