github.com/ava-labs/avalanchego@v1.11.11/vms/avm/network/atomic.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package network
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"github.com/ava-labs/avalanchego/ids"
    11  	"github.com/ava-labs/avalanchego/snow/engine/common"
    12  	"github.com/ava-labs/avalanchego/utils"
    13  )
    14  
    15  var _ Atomic = (*atomic)(nil)
    16  
    17  type Atomic interface {
    18  	common.AppHandler
    19  
    20  	Set(common.AppHandler)
    21  }
    22  
    23  type atomic struct {
    24  	handler utils.Atomic[common.AppHandler]
    25  }
    26  
    27  func NewAtomic(h common.AppHandler) Atomic {
    28  	a := &atomic{}
    29  	a.handler.Set(h)
    30  	return a
    31  }
    32  
    33  func (a *atomic) AppRequest(
    34  	ctx context.Context,
    35  	nodeID ids.NodeID,
    36  	requestID uint32,
    37  	deadline time.Time,
    38  	msg []byte,
    39  ) error {
    40  	h := a.handler.Get()
    41  	return h.AppRequest(
    42  		ctx,
    43  		nodeID,
    44  		requestID,
    45  		deadline,
    46  		msg,
    47  	)
    48  }
    49  
    50  func (a *atomic) AppRequestFailed(
    51  	ctx context.Context,
    52  	nodeID ids.NodeID,
    53  	requestID uint32,
    54  	appErr *common.AppError,
    55  ) error {
    56  	h := a.handler.Get()
    57  	return h.AppRequestFailed(
    58  		ctx,
    59  		nodeID,
    60  		requestID,
    61  		appErr,
    62  	)
    63  }
    64  
    65  func (a *atomic) AppResponse(
    66  	ctx context.Context,
    67  	nodeID ids.NodeID,
    68  	requestID uint32,
    69  	msg []byte,
    70  ) error {
    71  	h := a.handler.Get()
    72  	return h.AppResponse(
    73  		ctx,
    74  		nodeID,
    75  		requestID,
    76  		msg,
    77  	)
    78  }
    79  
    80  func (a *atomic) AppGossip(
    81  	ctx context.Context,
    82  	nodeID ids.NodeID,
    83  	msg []byte,
    84  ) error {
    85  	h := a.handler.Get()
    86  	return h.AppGossip(
    87  		ctx,
    88  		nodeID,
    89  		msg,
    90  	)
    91  }
    92  
    93  func (a *atomic) Set(h common.AppHandler) {
    94  	a.handler.Set(h)
    95  }