github.com/msinev/pmc@v0.0.0-20220512222710-eb40bf080315/pkg/trackclient/client.go (about)

     1  package trackclient
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"time"
     7  )
     8  import "sync"
     9  import "sync/atomic"
    10  
    11  import "encoding/json"
    12  
    13  type ClientRequset struct {
    14  	RequestStart time.Time  `json:"start"`
    15  	RequestEnd   *time.Time `json:"end,omitempty"`
    16  	RequestBody  string     `json:"body"`
    17  	Complete     bool       `json:"complete"`
    18  }
    19  
    20  type Client struct {
    21  	ClientID   string `json:"id"`
    22  	Credential string `json:"key"`
    23  	//
    24  	RequestStartCount    int  `json:"started"`
    25  	RequestCompleteCount int  `json:"completed"`
    26  	RequestAbortCount    int  `json:"aborted"`
    27  	Blocked              bool `json:"blocked"`
    28  	//
    29  	ClientCreation time.Time      `json:"creationTime"`
    30  	RequestsTime   time.Duration  `json:"processingTime"`
    31  	Processing     *ClientRequset `json:"processing,omitempty"`
    32  }
    33  
    34  var clientStorage map[string]*Client
    35  var clientKey map[string][]*Client
    36  var idCount int64
    37  var clientLock sync.RWMutex
    38  
    39  func newClient() string {
    40  	meID := atomic.AddInt64(&idCount, 1)
    41  	return fmt.Sprintf("%08d", meID)
    42  }
    43  
    44  func getClientList() []byte {
    45  	clientLock.RLock()
    46  	defer clientLock.RUnlock()
    47  
    48  	v := make([]*Client, 0, len(clientStorage))
    49  	for _, value := range clientStorage {
    50  		v = append(v, value)
    51  	}
    52  
    53  	b, err := json.Marshal(v)
    54  
    55  	if err != nil {
    56  		return []byte("[]")
    57  	}
    58  
    59  	return b
    60  }
    61  
    62  func getClientState(id string) []byte {
    63  	clientLock.RLock()
    64  	defer clientLock.RUnlock()
    65  
    66  	v, ok := clientStorage[id]
    67  	if !ok {
    68  		return []byte("{}")
    69  	}
    70  
    71  	b, err := json.Marshal(v)
    72  
    73  	if err != nil {
    74  		return []byte("{}")
    75  	}
    76  
    77  	return b
    78  }
    79  
    80  func registerClient(key string, force bool) string {
    81  	clientLock.Lock()
    82  	defer clientLock.Unlock()
    83  
    84  	myID := newClient()
    85  	newClient := &Client{
    86  		ClientID:   myID,
    87  		Credential: key,
    88  		//
    89  		RequestStartCount:    0,
    90  		RequestCompleteCount: 0,
    91  		RequestAbortCount:    0,
    92  		Blocked:              false,
    93  		//
    94  		ClientCreation: time.Now(),
    95  		RequestsTime:   0 * time.Nanosecond,
    96  		Processing:     nil,
    97  	}
    98  
    99  	clientKey[key] = append(clientKey[key], newClient)
   100  	clientStorage[myID] = newClient
   101  
   102  	return myID
   103  }
   104  
   105  func registerReqest(client string, request string) bool {
   106  	clientLock.Lock()
   107  	defer clientLock.Unlock()
   108  	Client, ok := clientStorage[client]
   109  
   110  	if !ok {
   111  		return false
   112  	}
   113  
   114  	if Client.Processing != nil && Client.Processing.RequestEnd == nil {
   115  		return false
   116  	}
   117  
   118  	Client.Processing = &ClientRequset{
   119  		RequestStart: time.Now(),
   120  		RequestEnd:   nil,
   121  		RequestBody:  request,
   122  		Complete:     false,
   123  	}
   124  
   125  	return true
   126  }
   127  
   128  func completeReqest(client string, request string) bool {
   129  	clientLock.Lock()
   130  	defer clientLock.Unlock()
   131  	Client, ok := clientStorage[client]
   132  
   133  	if !ok {
   134  		return false
   135  	}
   136  
   137  	if Client.Processing == nil || Client.Processing.RequestEnd != nil || Client.Processing.RequestBody != request {
   138  		return false
   139  	}
   140  
   141  	now := time.Now()
   142  	Client.Processing.RequestEnd = &now
   143  	Client.Processing.Complete = true
   144  	return true
   145  }
   146  
   147  func abortReqest(client string) bool {
   148  	clientLock.Lock()
   149  	defer clientLock.Unlock()
   150  	Client, ok := clientStorage[client]
   151  
   152  	if !ok {
   153  		return false
   154  	}
   155  
   156  	if Client.Processing == nil || Client.Processing.RequestEnd != nil {
   157  		return false
   158  	}
   159  
   160  	now := time.Now()
   161  	Client.Processing.RequestEnd = &now
   162  	Client.Processing.Complete = false
   163  	return true
   164  }