2017-01-19 05:49:01 +01:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2020-08-14 17:38:18 +02:00
|
|
|
"bytes"
|
2017-10-08 18:57:11 +02:00
|
|
|
"context"
|
2020-08-14 17:38:18 +02:00
|
|
|
"encoding/json"
|
2017-01-19 05:49:01 +01:00
|
|
|
"fmt"
|
2020-08-14 17:38:18 +02:00
|
|
|
"math/rand"
|
2017-10-08 18:57:11 +02:00
|
|
|
"net"
|
2020-08-14 17:38:18 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2017-10-08 18:57:11 +02:00
|
|
|
"sync"
|
2017-01-19 05:49:01 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2020-08-11 17:43:01 +02:00
|
|
|
"github.com/hashicorp/terraform/states/remote"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2017-01-19 05:49:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteClient_impl(t *testing.T) {
|
|
|
|
var _ remote.Client = new(RemoteClient)
|
2017-02-15 23:20:59 +01:00
|
|
|
var _ remote.ClientLocker = new(RemoteClient)
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClient(t *testing.T) {
|
2020-08-13 16:29:43 +02:00
|
|
|
testCases := []string{
|
|
|
|
fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
|
|
|
fmt.Sprintf("tf-unit/%s/", time.Now().String()),
|
2017-03-02 07:15:08 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 16:29:43 +02:00
|
|
|
for _, path := range testCases {
|
|
|
|
t.Run(path, func(*testing.T) {
|
|
|
|
// Get the backend
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Grab the client
|
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
})
|
|
|
|
}
|
2017-01-19 05:49:01 +01:00
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2017-03-14 20:24:50 +01:00
|
|
|
// test the gzip functionality of the client
|
|
|
|
func TestRemoteClient_gzipUpgrade(t *testing.T) {
|
|
|
|
statePath := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// Get the backend
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-14 20:24:50 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": statePath,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-14 20:24:50 +01:00
|
|
|
|
|
|
|
// Grab the client
|
2018-10-01 23:35:23 +02:00
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
2017-03-14 20:24:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
|
|
|
|
// create a new backend with gzip
|
2018-03-21 02:43:02 +01:00
|
|
|
b = backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-14 20:24:50 +01:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": statePath,
|
|
|
|
"gzip": true,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-03-14 20:24:50 +01:00
|
|
|
|
|
|
|
// Grab the client
|
2018-10-01 23:35:23 +02:00
|
|
|
state, err = b.StateMgr(backend.DefaultStateName)
|
2017-03-14 20:24:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
// TestConsul_largeState tries to write a large payload using the Consul state
|
|
|
|
// manager, as there is a limit to the size of the values in the KV store it
|
|
|
|
// will need to be split up before being saved and put back together when read.
|
|
|
|
func TestConsul_largeState(t *testing.T) {
|
|
|
|
path := "tf-unit/test-large-state"
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-13 23:25:58 +01:00
|
|
|
"address": srv.HTTPAddr,
|
2017-02-07 16:05:53 +01:00
|
|
|
"path": path,
|
2020-08-14 17:38:18 +02:00
|
|
|
}))
|
2017-02-07 16:33:05 +01:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
2017-02-07 16:33:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
c := s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
c.Path = path
|
2017-04-06 20:04:50 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
// testPaths fails the test if the keys found at the prefix don't match
|
|
|
|
// what is expected
|
|
|
|
testPaths := func(t *testing.T, expected []string) {
|
|
|
|
kv := c.Client.KV()
|
|
|
|
pairs, _, err := kv.List(c.Path, nil)
|
2020-08-13 15:15:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-14 17:38:18 +02:00
|
|
|
res := make([]string, 0)
|
|
|
|
for _, p := range pairs {
|
|
|
|
res = append(res, p.Key)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res, expected) {
|
|
|
|
t.Fatalf("Wrong keys: %#v", res)
|
2020-08-13 15:15:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
testPayload := func(t *testing.T, data map[string]string, keys []string) {
|
|
|
|
payload, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = c.Put(payload)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not put payload", err)
|
|
|
|
}
|
2017-04-06 20:04:50 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
remote, err := c.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-04-06 20:04:50 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
// md5 := md5.Sum(payload)
|
|
|
|
// if !bytes.Equal(md5[:], remote.MD5) {
|
|
|
|
// t.Fatal("the md5 sums do not match")
|
|
|
|
// }
|
2017-04-06 20:04:50 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
if !bytes.Equal(payload, remote.Data) {
|
|
|
|
t.Fatal("the data do not match")
|
|
|
|
}
|
2017-04-06 20:04:50 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
testPaths(t, keys)
|
2017-04-06 20:04:50 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
// The default limit for the size of the value in Consul is 524288 bytes
|
|
|
|
testPayload(
|
|
|
|
t,
|
|
|
|
map[string]string{
|
|
|
|
"foo": strings.Repeat("a", 524288+2),
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"tf-unit/test-large-state",
|
|
|
|
"tf-unit/test-large-state/tfstate.2cb96f52c9fff8e0b56cb786ec4d2bed/0",
|
|
|
|
"tf-unit/test-large-state/tfstate.2cb96f52c9fff8e0b56cb786ec4d2bed/1",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// We try to replace the payload with a small one, the old chunks should be removed
|
|
|
|
testPayload(
|
|
|
|
t,
|
|
|
|
map[string]string{"var": "a"},
|
|
|
|
[]string{"tf-unit/test-large-state"},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test with gzip and chunks
|
|
|
|
b = backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
"gzip": true,
|
|
|
|
}))
|
2020-08-13 15:15:46 +02:00
|
|
|
|
|
|
|
s, err = b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
2020-08-14 17:38:18 +02:00
|
|
|
t.Fatal(err)
|
2020-08-13 15:15:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
c = s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
c.Path = path
|
2020-08-13 15:15:46 +02:00
|
|
|
|
2020-08-14 17:38:18 +02:00
|
|
|
// We need a long random string so it results in multiple chunks even after
|
|
|
|
// being gziped
|
|
|
|
|
|
|
|
// We use a fixed seed so the test can be reproductible
|
|
|
|
rand.Seed(1234)
|
|
|
|
RandStringRunes := func(n int) string {
|
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
testPayload(
|
|
|
|
t,
|
|
|
|
map[string]string{
|
|
|
|
"bar": RandStringRunes(5 * (524288 + 2)),
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"tf-unit/test-large-state",
|
|
|
|
"tf-unit/test-large-state/tfstate.58e8160335864b520b1cc7f2222a4019/0",
|
|
|
|
"tf-unit/test-large-state/tfstate.58e8160335864b520b1cc7f2222a4019/1",
|
|
|
|
"tf-unit/test-large-state/tfstate.58e8160335864b520b1cc7f2222a4019/2",
|
|
|
|
"tf-unit/test-large-state/tfstate.58e8160335864b520b1cc7f2222a4019/3",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deleting the state should remove all chunks
|
|
|
|
err = c.Delete()
|
2017-04-06 20:04:50 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-14 17:38:18 +02:00
|
|
|
testPaths(t, []string{})
|
|
|
|
}
|
2020-08-13 15:15:46 +02:00
|
|
|
|
2017-02-07 16:05:53 +01:00
|
|
|
func TestConsul_stateLock(t *testing.T) {
|
2020-08-13 16:29:43 +02:00
|
|
|
testCases := []string{
|
|
|
|
fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
|
|
|
fmt.Sprintf("tf-unit/%s/", time.Now().String()),
|
2020-08-13 15:15:46 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 16:29:43 +02:00
|
|
|
for _, path := range testCases {
|
|
|
|
t.Run(path, func(*testing.T) {
|
|
|
|
// create 2 instances to get 2 remote.Clients
|
|
|
|
sA, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
})).StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sB, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
})).StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestRemoteLocks(t, sA.(*remote.State).Client, sB.(*remote.State).Client)
|
|
|
|
})
|
2017-02-07 16:33:05 +01:00
|
|
|
}
|
2017-02-07 16:05:53 +01:00
|
|
|
}
|
2020-08-13 15:15:46 +02:00
|
|
|
|
2017-04-06 20:04:50 +02:00
|
|
|
func TestConsul_destroyLock(t *testing.T) {
|
2020-08-13 16:29:43 +02:00
|
|
|
testCases := []string{
|
|
|
|
fmt.Sprintf("tf-unit/%s", time.Now().String()),
|
|
|
|
fmt.Sprintf("tf-unit/%s/", time.Now().String()),
|
2017-04-06 20:04:50 +02:00
|
|
|
}
|
2020-08-13 15:15:46 +02:00
|
|
|
|
2020-09-29 10:08:32 +02:00
|
|
|
testLock := func(client *RemoteClient, lockPath string) {
|
|
|
|
// get the lock val
|
|
|
|
pair, _, err := client.Client.KV().Get(lockPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if pair != nil {
|
|
|
|
t.Fatalf("lock key not cleaned up at: %s", pair.Key)
|
|
|
|
}
|
2020-08-13 15:15:46 +02:00
|
|
|
}
|
2020-09-29 10:08:32 +02:00
|
|
|
|
2020-08-13 16:29:43 +02:00
|
|
|
for _, path := range testCases {
|
|
|
|
t.Run(path, func(*testing.T) {
|
|
|
|
// Get the backend
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Grab the client
|
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:08:32 +02:00
|
|
|
clientA := s.(*remote.State).Client.(*RemoteClient)
|
2020-08-13 16:29:43 +02:00
|
|
|
|
|
|
|
info := statemgr.NewLockInfo()
|
2020-09-29 10:08:32 +02:00
|
|
|
id, err := clientA.Lock(info)
|
2020-08-13 16:29:43 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:08:32 +02:00
|
|
|
lockPath := clientA.Path + lockSuffix
|
2020-08-13 16:29:43 +02:00
|
|
|
|
2020-09-29 10:08:32 +02:00
|
|
|
if err := clientA.Unlock(id); err != nil {
|
2020-08-13 16:29:43 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-29 10:08:32 +02:00
|
|
|
testLock(clientA, lockPath)
|
|
|
|
|
|
|
|
// The release the lock from a second client to test the
|
|
|
|
// `terraform force-unlock <lock_id>` functionnality
|
|
|
|
s, err = b.StateMgr(backend.DefaultStateName)
|
2020-08-13 16:29:43 +02:00
|
|
|
if err != nil {
|
2020-09-29 10:08:32 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientB := s.(*remote.State).Client.(*RemoteClient)
|
|
|
|
|
|
|
|
info = statemgr.NewLockInfo()
|
|
|
|
id, err = clientA.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := clientB.Unlock(id); err != nil {
|
2020-08-13 16:29:43 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-29 10:08:32 +02:00
|
|
|
|
|
|
|
testLock(clientA, lockPath)
|
|
|
|
|
|
|
|
err = clientA.Unlock(id)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("consul lock should have been lost")
|
|
|
|
}
|
|
|
|
if err.Error() != "consul lock was lost" {
|
|
|
|
t.Fatal("got wrong error", err)
|
2020-08-13 16:29:43 +02:00
|
|
|
}
|
|
|
|
})
|
2017-04-06 20:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-30 17:07:56 +02:00
|
|
|
|
|
|
|
func TestConsul_lostLock(t *testing.T) {
|
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
|
|
|
// create 2 instances to get 2 remote.Clients
|
2018-03-21 02:43:02 +01:00
|
|
|
sA, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-05-30 17:07:56 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
2018-10-01 23:35:23 +02:00
|
|
|
})).StateMgr(backend.DefaultStateName)
|
2017-05-30 17:07:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
sB, err := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-05-30 17:07:56 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path + "-not-used",
|
2018-10-01 23:35:23 +02:00
|
|
|
})).StateMgr(backend.DefaultStateName)
|
2017-05-30 17:07:56 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
info := statemgr.NewLockInfo()
|
2017-05-30 17:07:56 +02:00
|
|
|
info.Operation = "test-lost-lock"
|
|
|
|
id, err := sA.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reLocked := make(chan struct{})
|
|
|
|
testLockHook = func() {
|
|
|
|
close(reLocked)
|
2017-10-08 22:24:45 +02:00
|
|
|
testLockHook = nil
|
2017-05-30 17:07:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we use the second client to break the lock
|
|
|
|
kv := sB.(*remote.State).Client.(*RemoteClient).Client.KV()
|
|
|
|
_, err = kv.Delete(path+lockSuffix, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-reLocked
|
|
|
|
|
|
|
|
if err := sA.Unlock(id); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 18:57:11 +02:00
|
|
|
|
|
|
|
func TestConsul_lostLockConnection(t *testing.T) {
|
|
|
|
// create an "unreliable" network by closing all the consul client's
|
|
|
|
// network connections
|
|
|
|
conns := &unreliableConns{}
|
|
|
|
origDialFn := dialContext
|
|
|
|
defer func() {
|
|
|
|
dialContext = origDialFn
|
|
|
|
}()
|
|
|
|
dialContext = conns.DialContext
|
|
|
|
|
|
|
|
path := fmt.Sprintf("tf-unit/%s", time.Now().String())
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-10-08 18:57:11 +02:00
|
|
|
"address": srv.HTTPAddr,
|
|
|
|
"path": path,
|
2018-03-21 02:43:02 +01:00
|
|
|
}))
|
2017-10-08 18:57:11 +02:00
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
s, err := b.StateMgr(backend.DefaultStateName)
|
2017-10-08 18:57:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:43:01 +02:00
|
|
|
info := statemgr.NewLockInfo()
|
2017-10-08 18:57:11 +02:00
|
|
|
info.Operation = "test-lost-lock-connection"
|
|
|
|
id, err := s.Lock(info)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
// kill the connection a few times
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
dialed := conns.dialedDone()
|
|
|
|
// kill any open connections
|
|
|
|
conns.Kill()
|
|
|
|
// wait for a new connection to be dialed, and kill it again
|
|
|
|
<-dialed
|
2017-10-08 18:57:11 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
if err := s.Unlock(id); err != nil {
|
|
|
|
t.Fatal("unlock error:", err)
|
2017-10-08 18:57:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type unreliableConns struct {
|
|
|
|
sync.Mutex
|
|
|
|
conns []net.Conn
|
|
|
|
dialCallback func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *unreliableConns) DialContext(ctx context.Context, netw, addr string) (net.Conn, error) {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
dialer := &net.Dialer{}
|
|
|
|
conn, err := dialer.DialContext(ctx, netw, addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.conns = append(u.conns, conn)
|
|
|
|
|
|
|
|
if u.dialCallback != nil {
|
|
|
|
u.dialCallback()
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 01:45:10 +02:00
|
|
|
func (u *unreliableConns) dialedDone() chan struct{} {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
dialed := make(chan struct{})
|
|
|
|
u.dialCallback = func() {
|
|
|
|
defer close(dialed)
|
|
|
|
u.dialCallback = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialed
|
|
|
|
}
|
|
|
|
|
2017-10-08 18:57:11 +02:00
|
|
|
// Kill these with a deadline, just to make sure we don't end up with any EOFs
|
|
|
|
// that get ignored.
|
|
|
|
func (u *unreliableConns) Kill() {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
|
|
|
|
for _, conn := range u.conns {
|
|
|
|
conn.(*net.TCPConn).SetDeadline(time.Now())
|
|
|
|
}
|
|
|
|
u.conns = nil
|
|
|
|
}
|