github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/federation/federation.go (about)

     1  // Copyright (c) 2017-2022, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package federation
     6  
     7  import (
     8  	"context"
     9  	"sync"
    10  
    11  	"github.com/choria-io/go-choria/inter"
    12  	log "github.com/sirupsen/logrus"
    13  )
    14  
    15  const (
    16  	Unconnected = iota
    17  	Federation
    18  	Collective
    19  )
    20  
    21  type transformer interface {
    22  	chainable
    23  	runable
    24  }
    25  
    26  type connector interface {
    27  	chainable
    28  	runable
    29  }
    30  
    31  type FederationBroker struct {
    32  	choria inter.Framework
    33  
    34  	Name string
    35  
    36  	fedIn         connector
    37  	fedOut        connector
    38  	collectiveIn  connector
    39  	collectiveOut connector
    40  
    41  	requestT transformer
    42  	replyT   transformer
    43  
    44  	identity string
    45  	logger   *log.Entry
    46  }
    47  
    48  func NewFederationBroker(clusterName string, choria inter.Framework) (broker *FederationBroker, err error) {
    49  	broker = &FederationBroker{
    50  		Name:     clusterName,
    51  		choria:   choria,
    52  		identity: choria.Configuration().Identity,
    53  		logger:   log.WithFields(log.Fields{"cluster": clusterName, "component": "federation"}),
    54  	}
    55  
    56  	return
    57  }
    58  
    59  func (fb *FederationBroker) Start(ctx context.Context, wg *sync.WaitGroup) {
    60  	fb.logger.Infof("Starting Federation Broker %s", fb.Name)
    61  
    62  	defer wg.Done()
    63  
    64  	// requests from federation
    65  	fb.fedIn, _ = NewChoriaNatsIngest(10, Federation, 10000, fb, nil)
    66  	fb.collectiveOut, _ = NewChoriaNatsEgest(10, Collective, 10000, fb, nil)
    67  	fb.requestT, _ = NewChoriaRequestTransformer(10, 1000, fb, nil)
    68  	fb.fedIn.To(fb.requestT)
    69  	fb.requestT.To(fb.collectiveOut)
    70  
    71  	// replies from collective
    72  	fb.collectiveIn, _ = NewChoriaNatsIngest(10, Collective, 10000, fb, nil)
    73  	fb.fedOut, _ = NewChoriaNatsEgest(10, Federation, 10000, fb, nil)
    74  	fb.replyT, _ = NewChoriaReplyTransformer(10, 1000, fb, nil)
    75  	fb.collectiveIn.To(fb.replyT)
    76  	fb.replyT.To(fb.fedOut)
    77  
    78  	go fb.requestT.Run(ctx)
    79  	go fb.replyT.Run(ctx)
    80  	go fb.collectiveOut.Run(ctx)
    81  	go fb.collectiveIn.Run(ctx)
    82  	go fb.requestT.Run(ctx)
    83  	go fb.fedOut.Run(ctx)
    84  	go fb.fedIn.Run(ctx)
    85  
    86  	<-ctx.Done()
    87  
    88  	log.Warn("Choria Federation Broker shutting down")
    89  }
    90  
    91  func nameForConnectionMode(mode int) string {
    92  	switch mode {
    93  	case Collective:
    94  		return "collective"
    95  	case Federation:
    96  		return "federation"
    97  	default:
    98  		return "unconnected"
    99  	}
   100  }