@@ -59,7 +59,8 @@ type IggyTcpClient struct {
5959 clientAddress string
6060 currentServerAddress string
6161 connectedAt time.Time
62- state iggcon.State
62+ transportState iggcon.TransportState
63+ sessionState iggcon.SessionState
6364 // respHeader is the reused response-status read buffer; guarded by c.mtx.
6465 respHeader [ResponseInitialBytesLength ]byte
6566}
@@ -216,7 +217,8 @@ func NewIggyTcpClient(logger *slog.Logger, options ...Option) *IggyTcpClient {
216217 logger : logger ,
217218 clientAddress : "" ,
218219 conn : nil ,
219- state : iggcon .StateDisconnected ,
220+ transportState : iggcon .TransportStateDisconnected ,
221+ sessionState : iggcon .SessionStateUnauthenticated ,
220222 connectedAt : time.Time {},
221223 leaderRedirectionState : iggcon.LeaderRedirectionState {},
222224 currentServerAddress : opts .config .serverAddress ,
@@ -341,14 +343,14 @@ func (c *IggyTcpClient) sendWireAndFetchResponse(ctx context.Context, wirePayloa
341343 c .mtx .Lock ()
342344 defer c .mtx .Unlock ()
343345
344- switch c .state {
345- case iggcon .StateShutdown :
346+ switch c .transportState {
347+ case iggcon .TransportStateShutdown :
346348 c .logger .Debug ("Cannot send data. Client is shutdown." )
347349 return nil , ierror .ErrClientShutdown
348- case iggcon .StateDisconnected :
350+ case iggcon .TransportStateDisconnected :
349351 c .logger .Debug ("Cannot send data. Client is not connected." )
350352 return nil , ierror .ErrNotConnected
351- case iggcon .StateConnecting :
353+ case iggcon .TransportStateConnecting :
352354 c .logger .Debug ("Cannot send data. Client is still connecting." )
353355 return nil , ierror .ErrNotConnected
354356 }
@@ -431,12 +433,19 @@ func (c *IggyTcpClient) sendLocked(wirePayload []byte) ([]byte, error) {
431433 return buffer , nil
432434}
433435
436+ func (c * IggyTcpClient ) setSessionState (state iggcon.SessionState ) {
437+ c .mtx .Lock ()
438+ c .sessionState = state
439+ c .mtx .Unlock ()
440+ }
441+
434442// invalidateConnLocked closes the connection and marks it as disconnected
435443func (c * IggyTcpClient ) invalidateConnLocked () {
436444 if c .conn != nil {
437445 _ = c .conn .Close ()
438446 }
439- c .state = iggcon .StateDisconnected
447+ c .transportState = iggcon .TransportStateDisconnected
448+ c .sessionState = iggcon .SessionStateUnauthenticated
440449}
441450
442451func (c * IggyTcpClient ) GetConnectionInfo () * iggcon.ConnectionInfo {
@@ -451,24 +460,22 @@ func (c *IggyTcpClient) GetConnectionInfo() *iggcon.ConnectionInfo {
451460// Connect establishes the TCP connection to the server.
452461func (c * IggyTcpClient ) Connect (ctx context.Context ) error {
453462 c .mtx .Lock ()
454- switch c .state {
455- case iggcon .StateShutdown :
463+ switch c .transportState {
464+ case iggcon .TransportStateShutdown :
456465 c .mtx .Unlock ()
457466 c .logger .Debug ("Cannot connect. Client is shutdown." )
458467 return ierror .ErrClientShutdown
459- case iggcon .StateConnected ,
460- iggcon .StateAuthenticating ,
461- iggcon .StateAuthenticated :
468+ case iggcon .TransportStateConnected :
462469 clientAddress := c .clientAddress
463470 c .mtx .Unlock ()
464471 c .logger .Debug ("Client is already connected." , slog .String ("client_address" , clientAddress ))
465472 return nil
466- case iggcon .StateConnecting :
473+ case iggcon .TransportStateConnecting :
467474 c .mtx .Unlock ()
468475 c .logger .Debug ("Client is already connecting." )
469476 return nil
470477 default :
471- c .state = iggcon .StateConnecting
478+ c .transportState = iggcon .TransportStateConnecting
472479 }
473480 connectedAt := c .connectedAt
474481 c .mtx .Unlock ()
@@ -543,7 +550,7 @@ func (c *IggyTcpClient) Connect(ctx context.Context) error {
543550 return nil
544551 }); err != nil {
545552 c .mtx .Lock ()
546- c .state = iggcon .StateDisconnected
553+ c .transportState = iggcon .TransportStateDisconnected
547554 c .mtx .Unlock ()
548555 if ! c .config .reconnection .enabled {
549556 c .logger .Warn ("Automatic reconnection is disabled." )
@@ -554,7 +561,7 @@ func (c *IggyTcpClient) Connect(ctx context.Context) error {
554561
555562 c .mtx .Lock ()
556563 c .conn = conn
557- c .state = iggcon .StateConnected
564+ c .transportState = iggcon .TransportStateConnected
558565 c .connectedAt = time .Now ()
559566 c .logger .Info ("Iggy client has connected to the Iggy server" , slog .String ("client_address" , c .clientAddress ), slog .String ("server_address" , c .currentServerAddress ))
560567 c .mtx .Unlock ()
@@ -610,12 +617,13 @@ func (c *IggyTcpClient) disconnect() error {
610617 c .mtx .Lock ()
611618 defer c .mtx .Unlock ()
612619
613- if c .state == iggcon .StateDisconnected {
620+ if c .transportState == iggcon .TransportStateDisconnected {
614621 return nil
615622 }
616623
617624 c .logger .Info ("Iggy client is disconnecting from server..." , slog .String ("client_address" , c .clientAddress ))
618- c .state = iggcon .StateDisconnected
625+ c .transportState = iggcon .TransportStateDisconnected
626+ c .sessionState = iggcon .SessionStateUnauthenticated
619627
620628 if c .conn != nil {
621629 if err := c .conn .Close (); err != nil {
@@ -632,7 +640,7 @@ func (c *IggyTcpClient) shutdown() error {
632640 c .mtx .Lock ()
633641 defer c .mtx .Unlock ()
634642
635- if c .state == iggcon .StateShutdown {
643+ if c .transportState == iggcon .TransportStateShutdown {
636644 return nil
637645 }
638646
@@ -644,7 +652,8 @@ func (c *IggyTcpClient) shutdown() error {
644652 }
645653 }
646654
647- c .state = iggcon .StateShutdown
655+ c .transportState = iggcon .TransportStateShutdown
656+ c .sessionState = iggcon .SessionStateUnauthenticated
648657 c .logger .Info ("Iggy TCP client has been shutdown." , slog .String ("client_address" , c .clientAddress ))
649658 // TODO push shutdown event
650659 return nil
0 commit comments