github.com/argoproj/argo-events@v1.9.1/eventbus/stan/sensor/sensor_stan.go (about)

     1  package sensor
     2  
     3  import (
     4  	"context"
     5  	"crypto/rand"
     6  	"fmt"
     7  	"math/big"
     8  
     9  	"github.com/argoproj/argo-events/common"
    10  	eventbuscommon "github.com/argoproj/argo-events/eventbus/common"
    11  	stanbase "github.com/argoproj/argo-events/eventbus/stan/base"
    12  	"go.uber.org/zap"
    13  )
    14  
    15  type SensorSTAN struct {
    16  	*stanbase.STAN
    17  	sensorName string
    18  }
    19  
    20  func NewSensorSTAN(url, clusterID, sensorName string, auth *eventbuscommon.Auth, logger *zap.SugaredLogger) *SensorSTAN {
    21  	return &SensorSTAN{
    22  		stanbase.NewSTAN(url, clusterID, auth, logger),
    23  		sensorName,
    24  	}
    25  }
    26  
    27  func (n *SensorSTAN) Initialize() error {
    28  	return nil
    29  }
    30  
    31  func (n *SensorSTAN) Connect(ctx context.Context, triggerName string, dependencyExpression string, deps []eventbuscommon.Dependency, atLeastOnce bool) (eventbuscommon.TriggerConnection, error) {
    32  	// Generate clientID with hash code
    33  	hashKey := fmt.Sprintf("%s-%s-%s", n.sensorName, triggerName, dependencyExpression)
    34  	randomNum, _ := rand.Int(rand.Reader, big.NewInt(int64(100)))
    35  	hashVal := common.Hasher(hashKey)
    36  	clientID := fmt.Sprintf("client-%v-%v", hashVal, randomNum.Int64())
    37  
    38  	conn, err := n.MakeConnection(clientID)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return NewSTANTriggerConn(conn, n.sensorName, triggerName, dependencyExpression, deps), nil
    44  }