summaryrefslogtreecommitdiff
path: root/lgc/upload.go
blob: e4518b8dc6232e1cf488850fcef1f3a36849bb82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * This file is part of Go Responsiveness.
 *
 * Go Responsiveness is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software Foundation,
 * either version 2 of the License, or (at your option) any later version.
 * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>.
 */

package lgc

import (
	"context"
	"crypto/tls"
	"fmt"
	"io"
	"net/http"
	"sync"
	"sync/atomic"
	"time"

	"github.com/network-quality/goresponsiveness/debug"
	"github.com/network-quality/goresponsiveness/stats"
	"github.com/network-quality/goresponsiveness/utilities"
)

// TODO: All 64-bit fields that are accessed atomically must
// appear at the top of this struct.
type LoadGeneratingConnectionUpload struct {
	uploaded           uint64
	lastIntervalEnd    int64
	URL                string
	ConnectToAddr      string
	uploadStartTime    time.Time
	lastUploaded       uint64
	client             *http.Client
	debug              debug.DebugLevel
	InsecureSkipVerify bool
	KeyLogger          io.Writer
	clientId           uint64
	status             LgcStatus
	statusLock         *sync.Mutex
	statusWaiter       *sync.Cond
}

func NewLoadGeneratingConnectionUpload(url string, keyLogger io.Writer, connectToAddr string, insecureSkipVerify bool) LoadGeneratingConnectionUpload {
	lgu := LoadGeneratingConnectionUpload{
		URL:                url,
		KeyLogger:          keyLogger,
		ConnectToAddr:      connectToAddr,
		InsecureSkipVerify: insecureSkipVerify,
		statusLock:         &sync.Mutex{},
	}
	lgu.status = LGC_STATUS_NOT_STARTED
	lgu.statusWaiter = sync.NewCond(lgu.statusLock)
	return lgu
}

func (lgu *LoadGeneratingConnectionUpload) WaitUntilStarted(ctxt context.Context) bool {
	conditional := func() bool { return lgu.status != LGC_STATUS_NOT_STARTED }
	go utilities.ContextSignaler(ctxt, 500*time.Millisecond, &conditional, lgu.statusWaiter)
	return utilities.WaitWithContext(ctxt, &conditional, lgu.statusLock, lgu.statusWaiter)
}

func (lgu *LoadGeneratingConnectionUpload) ClientId() uint64 {
	return lgu.clientId
}

func (lgu *LoadGeneratingConnectionUpload) TransferredInInterval() (uint64, time.Duration) {
	transferred := atomic.SwapUint64(&lgu.uploaded, 0)
	newIntervalEnd := (time.Now().Sub(lgu.uploadStartTime)).Nanoseconds()
	previousIntervalEnd := atomic.SwapInt64(&lgu.lastIntervalEnd, newIntervalEnd)
	intervalLength := time.Duration(newIntervalEnd - previousIntervalEnd)
	if debug.IsDebug(lgu.debug) {
		fmt.Printf("upload: Transferred: %v bytes in %v.\n", transferred, intervalLength)
	}
	return transferred, intervalLength
}

func (lgu *LoadGeneratingConnectionUpload) Client() *http.Client {
	return lgu.client
}

func (lgu *LoadGeneratingConnectionUpload) Status() LgcStatus {
	return lgu.status
}

func (lgd *LoadGeneratingConnectionUpload) Direction() LgcDirection {
	return LGC_UP
}

type syntheticCountingReader struct {
	n   *uint64
	ctx context.Context
	lgu *LoadGeneratingConnectionUpload
}

func (s *syntheticCountingReader) Read(p []byte) (n int, err error) {
	if s.ctx.Err() != nil {
		return 0, io.EOF
	}
	if *s.n == 0 {
		s.lgu.statusLock.Lock()
		s.lgu.status = LGC_STATUS_RUNNING
		s.lgu.statusWaiter.Broadcast()
		s.lgu.statusLock.Unlock()
	}
	err = nil
	n = len(p)

	atomic.AddUint64(s.n, uint64(n))
	return
}

func (lgu *LoadGeneratingConnectionUpload) doUpload(ctx context.Context) error {
	lgu.uploaded = 0
	s := &syntheticCountingReader{n: &lgu.uploaded, ctx: ctx, lgu: lgu}
	var resp *http.Response = nil
	var request *http.Request = nil
	var err error

	if request, err = http.NewRequest(
		"POST",
		lgu.URL,
		s,
	); err != nil {
		lgu.statusLock.Lock()
		lgu.status = LGC_STATUS_ERROR
		lgu.statusWaiter.Broadcast()
		lgu.statusLock.Unlock()
		return err
	}

	// Used to disable compression
	request.Header.Set("Accept-Encoding", "identity")
	request.Header.Set("User-Agent", utilities.UserAgent())

	lgu.uploadStartTime = time.Now()
	lgu.lastIntervalEnd = 0

	lgu.statusLock.Lock()
	lgu.status = LGC_STATUS_RUNNING
	lgu.statusWaiter.Broadcast()
	lgu.statusLock.Unlock()

	if resp, err = lgu.client.Do(request); err != nil {
		lgu.statusLock.Lock()
		lgu.status = LGC_STATUS_ERROR
		lgu.statusWaiter.Broadcast()
		lgu.statusLock.Unlock()
		return err
	}

	lgu.statusLock.Lock()
	lgu.status = LGC_STATUS_DONE
	lgu.statusWaiter.Broadcast()
	lgu.statusLock.Unlock()

	resp.Body.Close()
	if debug.IsDebug(lgu.debug) {
		fmt.Printf("Ending a load-generating upload.\n")
	}
	return nil
}

func (lgu *LoadGeneratingConnectionUpload) Start(
	parentCtx context.Context,
	debugLevel debug.DebugLevel,
) bool {
	lgu.uploaded = 0
	lgu.clientId = utilities.GenerateUniqueId()
	lgu.debug = debugLevel

	transport := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: lgu.InsecureSkipVerify,
		},
	}

	if !utilities.IsInterfaceNil(lgu.KeyLogger) {
		if debug.IsDebug(lgu.debug) {
			fmt.Printf(
				"Using an SSL Key Logger for this load-generating upload.\n",
			)
		}
		transport.TLSClientConfig.KeyLogWriter = lgu.KeyLogger
	}

	utilities.OverrideHostTransport(transport, lgu.ConnectToAddr)

	lgu.client = &http.Client{Transport: transport}

	if debug.IsDebug(lgu.debug) {
		fmt.Printf("Started a load-generating upload (id: %v).\n", lgu.clientId)
	}

	go lgu.doUpload(parentCtx)
	return true
}

func (lgu *LoadGeneratingConnectionUpload) Stats() *stats.TraceStats {
	// Get all your stats from the download side of the LGC.
	return nil
}