github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/persistence/protocb/provider.go (about)

     1  package protocb
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"github.com/asynkron/protoactor-go/actor"
     8  	"github.com/asynkron/protoactor-go/persistence"
     9  	"github.com/couchbase/gocb"
    10  )
    11  
    12  type Provider struct {
    13  	async            bool
    14  	bucket           *gocb.Bucket
    15  	bucketName       string
    16  	snapshotInterval int
    17  	writer           *actor.PID
    18  }
    19  
    20  func (provider *Provider) GetState() persistence.ProviderState {
    21  	return &cbState{
    22  		Provider: provider,
    23  	}
    24  }
    25  
    26  func New(actorSystem *actor.ActorSystem, bucketName string, baseU string, options ...CouchbaseOption) *Provider {
    27  	c, err := gocb.Connect(baseU)
    28  	if err != nil {
    29  		log.Fatalf("Error connecting:  %v", err)
    30  	}
    31  	bucket, err := c.OpenBucketWithMt(bucketName, "")
    32  	if err != nil {
    33  		log.Fatalf("Error getting bucket:  %v", err)
    34  	}
    35  	bucket.SetTranscoder(transcoder{})
    36  
    37  	config := &couchbaseConfig{}
    38  	for _, option := range options {
    39  		option(config)
    40  	}
    41  
    42  	provider := &Provider{
    43  		snapshotInterval: config.snapshotInterval,
    44  		async:            config.async,
    45  		bucket:           bucket,
    46  		bucketName:       bucketName,
    47  	}
    48  
    49  	if config.async {
    50  		pid := actorSystem.Root.Spawn(actor.PropsFromFunc(newWriter(time.Second / 10000)))
    51  		provider.writer = pid
    52  	}
    53  
    54  	return provider
    55  }