code.vegaprotocol.io/vega@v0.79.0/wallet/service/v2/connections/policy.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package connections
    17  
    18  import "time"
    19  
    20  type connectionPolicy interface {
    21  	HasConnectionExpired(now time.Time) bool
    22  	UpdateActivityDate(time.Time)
    23  	IsLongLivingConnection() bool
    24  	SetAsForcefullyClose()
    25  	IsClosed() bool
    26  }
    27  
    28  // sessionPolicy is the policy to apply between a third-party application and a wallet.
    29  // This type of policy is used for connections that only last as long as the service
    30  // is live or until they are explicitly stopped.
    31  // In short, a session connection doesn't survive the reboot of the service.
    32  type sessionPolicy struct {
    33  	expiryDate time.Time
    34  	closed     bool
    35  }
    36  
    37  func (p *sessionPolicy) UpdateActivityDate(t time.Time) {
    38  	p.expiryDate = t.Add(1 * time.Hour)
    39  }
    40  
    41  func (p *sessionPolicy) HasConnectionExpired(t time.Time) bool {
    42  	return p.expiryDate.Before(t)
    43  }
    44  
    45  func (p *sessionPolicy) IsLongLivingConnection() bool {
    46  	return false
    47  }
    48  
    49  func (p *sessionPolicy) SetAsForcefullyClose() {
    50  	p.closed = true
    51  }
    52  
    53  func (p *sessionPolicy) IsClosed() bool {
    54  	return p.closed
    55  }
    56  
    57  type longLivingConnectionPolicy struct {
    58  	// expirationDate is an optional expiry date for this connection.
    59  	expirationDate *time.Time
    60  	closed         bool
    61  }
    62  
    63  func (p *longLivingConnectionPolicy) UpdateActivityDate(_ time.Time) {}
    64  
    65  func (p *longLivingConnectionPolicy) HasConnectionExpired(now time.Time) bool {
    66  	return p.expirationDate != nil && !p.expirationDate.After(now)
    67  }
    68  
    69  func (p *longLivingConnectionPolicy) IsLongLivingConnection() bool {
    70  	return true
    71  }
    72  
    73  func (p *longLivingConnectionPolicy) SetAsForcefullyClose() {
    74  	p.closed = true
    75  }
    76  
    77  func (p *longLivingConnectionPolicy) IsClosed() bool {
    78  	return p.closed
    79  }