github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/cluster/client_session.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  // http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  
    13  package cluster
    14  
    15  import (
    16  	"github.com/lirm/aeron-go/aeron"
    17  	"github.com/lirm/aeron-go/aeron/atomic"
    18  	"github.com/lirm/aeron-go/aeron/logbuffer/term"
    19  )
    20  
    21  const (
    22  	ClientSessionMockedOffer = 1
    23  )
    24  
    25  type ClientSession interface {
    26  	Id() int64
    27  	ResponseStreamId() int32
    28  	ResponseChannel() string
    29  	EncodedPrincipal() []byte
    30  	Close()
    31  	// TODO: the other close methods are not part of interface.
    32  	// I don't understand the closing bool implementation and why it is needed
    33  	// Leaving out for now unless it is really important
    34  	// IsClosing() bool
    35  	Offer(*atomic.Buffer, int32, int32, term.ReservedValueSupplier) int64
    36  	// TryClaim(...)
    37  }
    38  
    39  type containerClientSession struct {
    40  	id               int64
    41  	responseStreamId int32
    42  	responseChannel  string
    43  	encodedPrincipal []byte
    44  	agent            *ClusteredServiceAgent
    45  	response         *aeron.Publication
    46  }
    47  
    48  func newContainerClientSession(
    49  	id int64,
    50  	responseStreamId int32,
    51  	responseChannel string,
    52  	encodedPrincipal []byte,
    53  	agent *ClusteredServiceAgent,
    54  ) (*containerClientSession, error) {
    55  	pub, err := agent.aeronClient.AddPublication(responseChannel, responseStreamId)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return &containerClientSession{
    60  		id:               id,
    61  		responseStreamId: responseStreamId,
    62  		responseChannel:  responseChannel,
    63  		encodedPrincipal: encodedPrincipal,
    64  		agent:            agent,
    65  		response:         pub,
    66  	}, nil
    67  }
    68  
    69  func (s *containerClientSession) Id() int64 {
    70  	return s.id
    71  }
    72  
    73  func (s *containerClientSession) ResponseStreamId() int32 {
    74  	return s.responseStreamId
    75  }
    76  
    77  func (s *containerClientSession) ResponseChannel() string {
    78  	return s.responseChannel
    79  }
    80  
    81  func (s *containerClientSession) EncodedPrincipal() []byte {
    82  	return s.encodedPrincipal
    83  }
    84  
    85  func (s *containerClientSession) Close() {
    86  	if _, ok := s.agent.getClientSession(s.id); ok {
    87  		s.agent.closeClientSession(s.id)
    88  	}
    89  }
    90  
    91  func (s *containerClientSession) Offer(
    92  	buffer *atomic.Buffer,
    93  	offset int32,
    94  	length int32,
    95  	reservedValueSupplier term.ReservedValueSupplier,
    96  ) int64 {
    97  	return s.agent.offerToSession(
    98  		s.id,
    99  		s.response,
   100  		buffer,
   101  		offset,
   102  		length,
   103  		reservedValueSupplier,
   104  	)
   105  }