github.com/cmd-stream/base-go@v0.0.0-20230813145615-dd6ac24c16f5/cmd.go (about)

     1  package base
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // Seq represents a sequence number of the command.
     9  //
    10  // The sequence number is used to determine the mapping between the command and
    11  // its results.
    12  type Seq int64
    13  
    14  // Proxy is a server transport proxy, it allows commands to send results back.
    15  //
    16  // Implementation of this interface should be thread-safe.
    17  type Proxy interface {
    18  	Send(seq Seq, result Result) error
    19  	SendWithDeadline(deadline time.Time, seq Seq, result Result) error
    20  }
    21  
    22  // Cmd represents a cmd-stream command.
    23  //
    24  // All user-defined commands must implement this interface. The command
    25  // execution could be time-limited. This can be achieved by using the at
    26  // parameter, which indicates when the command was received
    27  //
    28  //		deadline := at.Add(CmdTimeout)
    29  //		ownCtx, cancel := context.WithDeadline(ctx, deadline)
    30  //		// Do the context dependent work.
    31  //		...
    32  //		err = proxy.SendWithDeadline(deadline, seq, result)
    33  //	 ...
    34  //
    35  // With Proxy you can send more than one result back, the last one should have
    36  // result.LastOne() == true.
    37  type Cmd[T any] interface {
    38  	Exec(ctx context.Context, at time.Time, seq Seq, receiver T, proxy Proxy) error
    39  }