<commit_before>package lock

import (
	"fmt"
	"net/url"
	"time"

	"github.com/franela/goreq"
)

// LockTTL defines max time to live of a lock.
var LockTTL = time.Duration(20 * time.Second)

func init() {
	goreq.SetConnectTimeout(time.Second * 10)
}

// ConsulLock provides lock functionalities based on consul sessions.It implements
// The Lock interface.
type ConsulLock struct {
	consulAddr    string
	lockKeyPrefix string
	lockSession   string
}

// NewConsulLock creates a consul lock instance
func NewConsulLock() *ConsulLock {
	return &ConsulLock{
		consulAddr:    "http://localhost:8500",
		lockKeyPrefix: "platform/lock",
	}
}

// createSession creates a consul session
func (l *ConsulLock) createSession(ttl int) (string, error) {
	var ttlStr string
	if ttl > 0 {
		ttlStr = fmt.Sprintf("%ds", ttl)
	}
	item := url.Values{}
	item.Set("TTL", ttlStr)
	item.Set("Behaviour", "delete")
	resp, err := goreq.Request{
		Method:      "PUT",
		Uri:         l.consulAddr + "/v1/session/create",
		QueryString: item,
	}.Do()
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		b, _ := resp.Body.ToString()
		return "", fmt.Errorf(b)
	}

	var result map[string]string
	if err = resp.Body.FromJsonTo(&result); err != nil {
		return "", err
	}

	return result["ID"], nil
}

func (l *ConsulLock) acquire(key string) error {
	resp, err := goreq.Request{
		Method: "PUT",
		Uri:    l.consulAddr + "/v1/kv/" + fmt.Sprintf("%s?acquire=%s", l.lockKeyPrefix, l.lockSession),
	}.Do()
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		b, _ := resp.Body.ToString()
		return fmt.Errorf(b)
	}

	return nil
}

// Acquire acquires a lock on a key. A time-to-live time is set<commit_msg>detected acquired lock and return clear error message<commit_after>package lock

import (
	"fmt"
	"net/url"
	"time"

	"github.com/franela/goreq"
)

// LockTTL defines max time to live of a lock.
var LockTTL = time.Duration(20 * time.Second)

func init() {
	goreq.SetConnectTimeout(time.Second * 10)
}

// ConsulLock provides lock functionalities based on consul sessions.It implements
// The Lock interface.
type ConsulLock struct {
	consulAddr    string
	lockKeyPrefix string
	lockSession   string
}

// NewConsulLock creates a consul lock instance
func NewConsulLock() *ConsulLock {
	return &ConsulLock{
		consulAddr:    "http://localhost:8500",
		lockKeyPrefix: "platform/lock",
	}
}

// createSession creates a consul session
func (l *ConsulLock) createSession(ttl int) (string, error) {
	var ttlStr string
	if ttl > 0 {
		ttlStr = fmt.Sprintf("%ds", ttl)
	}
	item := url.Values{}
	item.Set("TTL", ttlStr)
	item.Set("Behaviour", "delete")
	resp, err := goreq.Request{
		Method:      "PUT",
		Uri:         l.consulAddr + "/v1/session/create",
		QueryString: item,
	}.Do()
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		b, _ := resp.Body.ToString()
		return "", fmt.Errorf(b)
	}

	var result map[string]string
	if err = resp.Body.FromJsonTo(&result); err != nil {
		return "", err
	}

	return result["ID"], nil
}

func (l *ConsulLock) acquire(key string) error {
	resp, err := goreq.Request{
		Method: "PUT",
		Uri:    l.consulAddr + "/v1/kv/" + fmt.Sprintf("%s?acquire=%s", l.lockKeyPrefix, l.lockSession),
	}.Do()
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		b, _ := resp.Body.ToString()
		return fmt.Errorf(b)
	}

	status, _ := resp.Body.ToString()
	if status == "false" {
		return fmt.Errorf("lock already acquired by another process")
	}

	return nil
}

// Acquire acquires a lock on a key. A time-to-live time is set
{'fraction_non_alphanumeric': 0.14313029226987162, 'fraction_numerical': 0.00928708003277793, 'mean_word_length': 3.5209876543209875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1325145', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1324, 'n_words': 360}