tlog.app/go/tlog@v0.23.1/agent/query.go (about)

     1  package agent
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  )
     7  
     8  type (
     9  	sub struct {
    10  		io.Writer
    11  
    12  		id int64
    13  	}
    14  )
    15  
    16  func (a *Agent) Query(ctx context.Context, w io.Writer, ts int64, q string) error {
    17  	return nil
    18  }
    19  
    20  func (a *Agent) Subscribe(ctx context.Context, w io.Writer, q string) (int64, error) {
    21  	defer a.mu.Unlock()
    22  	a.mu.Lock()
    23  
    24  	a.subid++
    25  	id := a.subid
    26  
    27  	sub := sub{
    28  		Writer: w,
    29  		id:     id,
    30  	}
    31  
    32  	a.subs = append(a.subs, sub)
    33  
    34  	return id, nil
    35  }
    36  
    37  func (a *Agent) Unsubscribe(ctx context.Context, id int64) error {
    38  	defer a.mu.Unlock()
    39  	a.mu.Lock()
    40  
    41  	i := 0
    42  
    43  	for i < len(a.subs) && a.subs[i].id < id {
    44  		i++
    45  	}
    46  
    47  	if i == len(a.subs) || a.subs[i].id != id {
    48  		return ErrUnknownSubscription
    49  	}
    50  
    51  	copy(a.subs[i:], a.subs[i+1:])
    52  
    53  	a.subs = a.subs[:len(a.subs)-1]
    54  
    55  	return nil
    56  }