github.com/safing/portbase@v0.19.5/database/subscription.go (about)

     1  package database
     2  
     3  import (
     4  	"github.com/safing/portbase/database/query"
     5  	"github.com/safing/portbase/database/record"
     6  )
     7  
     8  // Subscription is a database subscription for updates.
     9  type Subscription struct {
    10  	q        *query.Query
    11  	local    bool
    12  	internal bool
    13  
    14  	Feed chan record.Record
    15  }
    16  
    17  // Cancel cancels the subscription.
    18  func (s *Subscription) Cancel() error {
    19  	c, err := getController(s.q.DatabaseName())
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	c.subscriptionLock.Lock()
    25  	defer c.subscriptionLock.Unlock()
    26  
    27  	for key, sub := range c.subscriptions {
    28  		if sub.q == s.q {
    29  			c.subscriptions = append(c.subscriptions[:key], c.subscriptions[key+1:]...)
    30  			close(s.Feed) // this close is guarded by the controllers subscriptionLock.
    31  			return nil
    32  		}
    33  	}
    34  	return nil
    35  }