<commit_before>package scamp

import (
  "sync"
)

type ClientChan chan *Client

type Client struct {
  conn *Connection
  serv *Service

  requests MessageChan
  openReplies map[int]MessageChan

  closeSplitReqsAndReps chan bool

  isClosed bool
  closedMutex sync.Mutex
}

func Dial(connspec string) (client *Client, err error){
  Trace.Printf("connecting to `%s`", connspec)

  conn,err := DialConnection(connspec)
  client = NewClient(conn)
  if err != nil {
    return
  }
  client.conn = conn

  return
}

func NewClient(conn *Connection) (client *Client){
  Trace.Printf("client allocated")
  client = new(Client)

  client.conn = conn
  client.requests = make(MessageChan)
  client.openReplies = make(map[int]MessageChan)

  client.closeSplitReqsAndReps = make(chan bool)

  conn.SetClient(client)
  
  go client.splitReqsAndReps()

  return
}

func (client *Client)SetService(serv *Service) {
  client.serv = serv
}

func (client *Client)Send(msg *Message) (responseChan MessageChan, err error){ 
  // Info.Printf("sending message `%d`", msg.RequestId)
  err = client.conn.Send(msg)
  if err != nil {
    return
  }

  if msg.MessageType == MESSAGE_TYPE_REQUEST {
    Trace.Printf("sending request so waiting for reply")
    responseChan = make(MessageChan)
    client.openReplies[msg.RequestId] = responseChan
  } else {
    Trace.Printf("sending reply so done with this message")
  }

  return
}

func (client *Client)Close() {
  client.closedMutex.Lock()
  defer client.closedMutex.Unlock()
  if client.isClosed {
    Trace.Printf("client already closed. skipping shutdown.")
    return
  }

  client.conn.Close()

  client.closeSplitReqsAndReps <- true

  // // Notify wrapper service that we're dead
  if client.serv != nil {
    client.serv.RemoveClient(client)
  }

  client.isClosed = true<commit_msg>Stefan recommend a lock on sending data<commit_after>package scamp

import (
  "sync"
)

type ClientChan chan *Client

type Client struct {
  conn *Connection
  serv *Service

  requests MessageChan
  openReplies map[int]MessageChan

  closeSplitReqsAndReps chan bool

  isClosed bool
  closedM sync.Mutex

  sendM sync.Mutex
}

func Dial(connspec string) (client *Client, err error){
  Trace.Printf("connecting to `%s`", connspec)

  conn,err := DialConnection(connspec)
  client = NewClient(conn)
  if err != nil {
    return
  }
  client.conn = conn

  return
}

func NewClient(conn *Connection) (client *Client){
  Trace.Printf("client allocated")
  client = new(Client)

  client.conn = conn
  client.requests = make(MessageChan)
  client.openReplies = make(map[int]MessageChan)

  client.closeSplitReqsAndReps = make(chan bool)

  conn.SetClient(client)
  
  go client.splitReqsAndReps()

  return
}

func (client *Client)SetService(serv *Service) {
  client.serv = serv
}

func (client *Client)Send(msg *Message) (responseChan MessageChan, err error){ 
  client.sendM.Lock()
  defer client.sendM.Unlock()

  // Info.Printf("sending message `%d`", msg.RequestId)
  err = client.conn.Send(msg)
  if err != nil {
    return
  }

  if msg.MessageType == MESSAGE_TYPE_REQUEST {
    Trace.Printf("sending request so waiting for reply")
    responseChan = make(MessageChan)
    client.openReplies[msg.RequestId] = responseChan
  } else {
    Trace.Printf("sending reply so done with this message")
  }

  return
}

func (client *Client)Close() {
  client.closedM.Lock()
  defer client.closedM.Unlock()
  if client.isClosed {
    Trace.Printf("client already closed. skipping shutdown.")
    return
  }

  client.conn.Close()

  client.closeSplitReqsAndReps <- true

  // // Notify wrapper service that we're dead
  if client.serv != nil {
    client.serv.RemoveClient(client)
  }

  client.isClosed = true
{'fraction_non_alphanumeric': 0.10878323932312652, 'fraction_numerical': 0.0, 'mean_word_length': 3.7682458386683737, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6151419', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1221, 'n_words': 300}