github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/internal/proxy/proxy.go (about)

     1  package proxy
     2  
     3  import (
     4  	"context"
     5  
     6  	gdb "github.com/blong14/gache/internal/db"
     7  	glog "github.com/blong14/gache/internal/logging"
     8  )
     9  
    10  type QueryProxy struct {
    11  	inbox chan *gdb.Query
    12  	pool  *WorkPool
    13  }
    14  
    15  func NewQueryProxy() (*QueryProxy, error) {
    16  	inbox := make(chan *gdb.Query)
    17  	return &QueryProxy{
    18  		inbox: inbox,
    19  		pool:  NewWorkPool(inbox),
    20  	}, nil
    21  }
    22  
    23  func (qp *QueryProxy) Send(ctx context.Context, query *gdb.Query) {
    24  	qp.pool.Send(ctx, query)
    25  }
    26  
    27  func StartProxy(ctx context.Context, qp *QueryProxy) {
    28  	glog.Track("starting query proxy")
    29  	qp.pool.Start(ctx)
    30  	for _, table := range []string{"default"} {
    31  		query, done := gdb.NewAddTableQuery(ctx, []byte(table))
    32  		qp.Send(ctx, query)
    33  		<-done
    34  	}
    35  	glog.Track("default tables added")
    36  }
    37  
    38  func StopProxy(ctx context.Context, qp *QueryProxy) {
    39  	glog.Track("stopping query proxy")
    40  	qp.pool.WaitAndStop(ctx)
    41  	close(qp.inbox)
    42  }