浏览代码

Remove juju/errors

tags/1.0^2
9seconds 6 年前
父节点
当前提交
2918ed11e5

+ 4
- 0
antireplay/cache.go 查看文件

@@ -1,6 +1,9 @@
1 1
 package antireplay
2 2
 
3 3
 import (
4
+	"errors"
5
+	"fmt"
6
+
4 7
 	"github.com/allegro/bigcache"
5 8
 
6 9
 	"github.com/9seconds/mtg/config"
@@ -25,6 +28,7 @@ func Init() error {
25 28
 		HardMaxCacheSize: config.C.AntiReplay.MaxSize,
26 29
 	})
27 30
 	cache = c
31
+	err = fmt.Errorf("qqq: %w", errors.New("tt"))
28 32
 
29 33
 	return err
30 34
 }

+ 4
- 4
cli/proxy.go 查看文件

@@ -41,7 +41,7 @@ func Proxy() error {
41 41
 	defer logger.Sync() // nolint: errcheck
42 42
 
43 43
 	if err := config.InitPublicAddress(ctx); err != nil {
44
-		Fatal(err.Error())
44
+		Fatal(err)
45 45
 	}
46 46
 	zap.S().Debugw("Configuration", "config", config.C)
47 47
 
@@ -62,14 +62,14 @@ func Proxy() error {
62 62
 	PrintJSONStdout(config.GetURLs())
63 63
 
64 64
 	if err := antireplay.Init(); err != nil {
65
-		Fatal(err.Error())
65
+		Fatal(err)
66 66
 	}
67 67
 	if err := stats.Init(ctx); err != nil {
68
-		Fatal(err.Error())
68
+		Fatal(err)
69 69
 	}
70 70
 	proxyListener, err := net.Listen("tcp", config.C.ListenAddr.String())
71 71
 	if err != nil {
72
-		Fatal(err.Error())
72
+		Fatal(err)
73 73
 	}
74 74
 	go func() {
75 75
 		<-ctx.Done()

+ 5
- 2
cli/utils.go 查看文件

@@ -7,8 +7,11 @@ import (
7 7
 	"os"
8 8
 )
9 9
 
10
-func Fatal(args ...interface{}) {
11
-	PrintStderr(args...)
10
+func Fatal(arg interface{}) {
11
+	if value, ok := arg.(error); ok {
12
+		arg = fmt.Errorf("fatal error: %+v", value)
13
+	}
14
+	PrintStderr(arg)
12 15
 	os.Exit(1)
13 16
 }
14 17
 

+ 4
- 3
config/config.go 查看文件

@@ -4,11 +4,12 @@ import (
4 4
 	"bytes"
5 5
 	"context"
6 6
 	"encoding/json"
7
+	"errors"
8
+	"fmt"
7 9
 	"net"
8 10
 	"strconv"
9 11
 	"time"
10 12
 
11
-	"github.com/juju/errors"
12 13
 	"go.uber.org/zap"
13 14
 	statsd "gopkg.in/alexcesaro/statsd.v2"
14 15
 )
@@ -187,7 +188,7 @@ func Init(options ...Opt) error { // nolint: gocyclo
187 188
 			case "influxdb":
188 189
 				C.StatsdStats.TagsFormat = statsd.InfluxDB
189 190
 			default:
190
-				return errors.Errorf("Incorrect statsd tag %s", value)
191
+				return fmt.Errorf("Incorrect statsd tag %s", value)
191 192
 			}
192 193
 		case OptionTypeStatsdTags:
193 194
 			C.StatsdStats.Tags = opt.Value.(map[string]string)
@@ -206,7 +207,7 @@ func Init(options ...Opt) error { // nolint: gocyclo
206 207
 		case OptionTypeAdtag:
207 208
 			C.AdTag = opt.Value.([]byte)
208 209
 		default:
209
-			return errors.Errorf("Unknown tag %v", opt.Option)
210
+			return fmt.Errorf("Unknown tag %v", opt.Option)
210 211
 		}
211 212
 	}
212 213
 

+ 7
- 8
config/global_ips.go 查看文件

@@ -2,14 +2,13 @@ package config
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"fmt"
5 6
 	"io"
6 7
 	"io/ioutil"
7 8
 	"net"
8 9
 	"net/http"
9 10
 	"strings"
10 11
 	"time"
11
-
12
-	"github.com/juju/errors"
13 12
 )
14 13
 
15 14
 const (
@@ -20,7 +19,7 @@ const (
20 19
 func getGlobalIPv4(ctx context.Context) (net.IP, error) {
21 20
 	ip, err := fetchIP(ctx, "tcp4")
22 21
 	if err != nil || ip.To4() == nil {
23
-		return nil, errors.Annotate(err, "Cannot find public ipv4 address")
22
+		return nil, fmt.Errorf("cannot find public ipv4 address: %w", err)
24 23
 	}
25 24
 	return ip, nil
26 25
 }
@@ -28,7 +27,7 @@ func getGlobalIPv4(ctx context.Context) (net.IP, error) {
28 27
 func getGlobalIPv6(ctx context.Context) (net.IP, error) {
29 28
 	ip, err := fetchIP(ctx, "tcp6")
30 29
 	if err != nil || ip.To4() != nil {
31
-		return nil, errors.Annotate(err, "Cannot find public ipv6 address")
30
+		return nil, fmt.Errorf("cannot find public ipv6 address: %w", err)
32 31
 	}
33 32
 	return ip, nil
34 33
 }
@@ -47,7 +46,7 @@ func fetchIP(ctx context.Context, network string) (net.IP, error) {
47 46
 
48 47
 	req, err := http.NewRequest("GET", ifconfigAddress, nil)
49 48
 	if err != nil {
50
-		return nil, errors.Annotate(err, "Cannot create a request")
49
+		return nil, fmt.Errorf("cannot create a request: %w", err)
51 50
 	}
52 51
 
53 52
 	resp, err := client.Do(req.WithContext(ctx))
@@ -55,19 +54,19 @@ func fetchIP(ctx context.Context, network string) (net.IP, error) {
55 54
 		if resp != nil {
56 55
 			io.Copy(ioutil.Discard, resp.Body) // nolint: errcheck
57 56
 		}
58
-		return nil, errors.Annotate(err, "Cannot perform a request")
57
+		return nil, fmt.Errorf("cannot perform a request: %w", err)
59 58
 	}
60 59
 	defer resp.Body.Close() // nolint: errcheck
61 60
 
62 61
 	respDataBytes, err := ioutil.ReadAll(resp.Body)
63 62
 	if err != nil {
64
-		return nil, errors.Annotate(err, "Cannot read response body")
63
+		return nil, fmt.Errorf("cannot read response body: %w", err)
65 64
 	}
66 65
 	respData := strings.TrimSpace(string(respDataBytes))
67 66
 
68 67
 	ip := net.ParseIP(respData)
69 68
 	if ip == nil {
70
-		return nil, errors.Errorf("ifconfig.co returns incorrect IP %s", respData)
69
+		return nil, fmt.Errorf("ifconfig.co returns incorrect IP %s", respData)
71 70
 	}
72 71
 
73 72
 	return ip, nil

+ 3
- 9
go.mod 查看文件

@@ -9,19 +9,12 @@ require (
9 9
 	github.com/allegro/bigcache v1.2.1
10 10
 	github.com/beevik/ntp v0.2.0
11 11
 	github.com/cespare/xxhash v1.1.0
12
-	github.com/dustin/go-humanize v1.0.0
13
-	github.com/gofrs/uuid v3.2.0+incompatible
14
-	github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d
15
-	github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
16
-	github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2 // indirect
17 12
 	github.com/kr/pretty v0.1.0 // indirect
18
-	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
19
-	github.com/modern-go/reflect2 v1.0.1 // indirect
20 13
 	github.com/pkg/errors v0.8.1 // indirect
21 14
 	github.com/prometheus/client_golang v1.1.0
22 15
 	github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
23 16
 	github.com/spaolacci/murmur3 v1.1.0 // indirect
24
-	github.com/stretchr/testify v1.4.0
17
+	github.com/stretchr/testify v1.4.0 // indirect
25 18
 	go.uber.org/atomic v1.4.0 // indirect
26 19
 	go.uber.org/multierr v1.1.0 // indirect
27 20
 	go.uber.org/zap v1.10.0
@@ -30,5 +23,6 @@ require (
30 23
 	gopkg.in/alecthomas/kingpin.v2 v2.2.6
31 24
 	gopkg.in/alexcesaro/statsd.v2 v2.0.0
32 25
 	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
33
-	gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
34 26
 )
27
+
28
+go 1.13

+ 0
- 12
go.sum 查看文件

@@ -24,14 +24,10 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
24 24
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
25 25
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
26 26
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
27
-github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
28
-github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
29 27
 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
30 28
 github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
31 29
 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
32 30
 github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
33
-github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
34
-github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
35 31
 github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
36 32
 github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
37 33
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
@@ -44,12 +40,6 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
44 40
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
45 41
 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
46 42
 github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
47
-github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d h1:hJXjZMxj0SWlMoQkzeZDLi2cmeiWKa7y1B8Rg+qaoEc=
48
-github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
49
-github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 h1:UUHMLvzt/31azWTN/ifGWef4WUqvXk0iRqdhdy/2uzI=
50
-github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
51
-github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2 h1:Pp8RxiF4rSoXP9SED26WCfNB28/dwTDpPXS8XMJR8rc=
52
-github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
53 43
 github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
54 44
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
55 45
 github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
@@ -132,8 +122,6 @@ gopkg.in/alexcesaro/statsd.v2 v2.0.0/go.mod h1:i0ubccKGzBVNBpdGV5MocxyA/XlLUJzA7
132 122
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
133 123
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
134 124
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
135
-gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
136
-gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
137 125
 gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
138 126
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
139 127
 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

+ 3
- 3
main.go 查看文件

@@ -143,7 +143,7 @@ func main() {
143 143
 	app.HelpFlag.Short('h')
144 144
 
145 145
 	if err := utils.SetLimits(); err != nil {
146
-		cli.Fatal(err.Error())
146
+		cli.Fatal(err)
147 147
 	}
148 148
 
149 149
 	switch kingpin.MustParse(app.Parse(os.Args[1:])) {
@@ -177,11 +177,11 @@ func main() {
177 177
 			config.Opt{Option: config.OptionTypeAdtag, Value: *proxyAdtag},
178 178
 		)
179 179
 		if err != nil {
180
-			cli.Fatal(err.Error())
180
+			cli.Fatal(err)
181 181
 		}
182 182
 
183 183
 		if err := cli.Proxy(); err != nil {
184
-			cli.Fatal(err.Error())
184
+			cli.Fatal(err)
185 185
 		}
186 186
 	}
187 187
 }

+ 2
- 2
ntp/ntp.go 查看文件

@@ -1,11 +1,11 @@
1 1
 package ntp
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"math/rand"
5 6
 	"time"
6 7
 
7 8
 	"github.com/beevik/ntp"
8
-	"github.com/juju/errors"
9 9
 	"go.uber.org/zap"
10 10
 )
11 11
 
@@ -23,7 +23,7 @@ func Fetch() (time.Duration, error) {
23 23
 	url := ntpEndpoints[rand.Intn(len(ntpEndpoints))]
24 24
 	resp, err := ntp.Query(url)
25 25
 	if err != nil {
26
-		return 0, errors.Annotatef(err, "Cannot fetch NTP server %s", url)
26
+		return 0, fmt.Errorf("Cannot fetch NTP server %s: %w", url, err)
27 27
 	}
28 28
 
29 29
 	offsetInt := int64(resp.ClockOffset)

+ 4
- 4
obfuscated2/client_protocol.go 查看文件

@@ -4,11 +4,11 @@ import (
4 4
 	"bytes"
5 5
 	"crypto/sha256"
6 6
 	"encoding/binary"
7
+	"errors"
8
+	"fmt"
7 9
 	"io"
8 10
 	"time"
9 11
 
10
-	"github.com/juju/errors"
11
-
12 12
 	"github.com/9seconds/mtg/antireplay"
13 13
 	"github.com/9seconds/mtg/config"
14 14
 	"github.com/9seconds/mtg/conntypes"
@@ -26,7 +26,7 @@ type ClientProtocol struct {
26 26
 func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrappers.StreamReadWriteCloser, error) {
27 27
 	fm, err := c.ReadFrame(socket)
28 28
 	if err != nil {
29
-		return nil, errors.Annotate(err, "Cannot make client handshake")
29
+		return nil, fmt.Errorf("cannot make a client handshake: %w", err)
30 30
 	}
31 31
 
32 32
 	decHasher := sha256.New()
@@ -76,7 +76,7 @@ func (c *ClientProtocol) Handshake(socket wrappers.StreamReadWriteCloser) (wrapp
76 76
 
77 77
 func (c *ClientProtocol) ReadFrame(socket wrappers.StreamReader) (fm Frame, err error) {
78 78
 	if _, err = io.ReadFull(handshakeReader{socket}, fm.Bytes()); err != nil {
79
-		err = errors.Annotate(err, "Cannot extract obfuscated2 frame")
79
+		err = fmt.Errorf("cannot extract obfuscated2 frame: %w", err)
80 80
 	}
81 81
 	return
82 82
 }

+ 3
- 4
obfuscated2/telegram_protocol.go 查看文件

@@ -2,8 +2,7 @@ package obfuscated2
2 2
 
3 3
 import (
4 4
 	"crypto/rand"
5
-
6
-	"github.com/juju/errors"
5
+	"fmt"
7 6
 
8 7
 	"github.com/9seconds/mtg/protocol"
9 8
 	"github.com/9seconds/mtg/telegram"
@@ -23,7 +22,7 @@ func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wr
23 22
 		req.ClientProtocol.GetDC(),
24 23
 		req.ClientProtocol.GetConnectionProtocol())
25 24
 	if err != nil {
26
-		return nil, errors.Annotate(err, "Cannot dial to Telegram")
25
+		return nil, fmt.Errorf("cannot dial to telegram: %w", err)
27 26
 	}
28 27
 	fm := generateFrame(req.ClientProtocol)
29 28
 	data := fm.Bytes()
@@ -38,7 +37,7 @@ func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wr
38 37
 	copy(data[:frameOffsetIV], copyFrame[:frameOffsetIV])
39 38
 
40 39
 	if _, err := socket.Write(data); err != nil {
41
-		return nil, errors.Annotate(err, "Cannot write handshate frame to Telegram")
40
+		return nil, fmt.Errorf("cannot write handshake frame to telegram: %w", err)
42 41
 	}
43 42
 
44 43
 	return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil

+ 4
- 5
stats/stats.go 查看文件

@@ -2,11 +2,10 @@ package stats
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"fmt"
5 6
 	"net"
6 7
 	"net/http"
7 8
 
8
-	"github.com/juju/errors"
9
-
10 9
 	"github.com/9seconds/mtg/config"
11 10
 	"github.com/9seconds/mtg/conntypes"
12 11
 )
@@ -66,21 +65,21 @@ func Init(ctx context.Context) error {
66 65
 	instanceJSON := newStatsJSON(mux)
67 66
 	instancePrometheus, err := newStatsPrometheus(mux)
68 67
 	if err != nil {
69
-		return errors.Annotate(err, "Cannot initialize Prometheus")
68
+		return fmt.Errorf("cannot initialize prometheus: %w", err)
70 69
 	}
71 70
 
72 71
 	stats := []Stats{instanceJSON, instancePrometheus}
73 72
 	if config.C.StatsdStats.Addr.IP != nil {
74 73
 		instanceStatsd, err := newStatsStatsd()
75 74
 		if err != nil {
76
-			return errors.Annotate(err, "Cannot initialize StatsD")
75
+			return fmt.Errorf("cannot inialize statsd: %w", err)
77 76
 		}
78 77
 		stats = append(stats, instanceStatsd)
79 78
 	}
80 79
 
81 80
 	listener, err := net.Listen("tcp", config.C.StatsAddr.String())
82 81
 	if err != nil {
83
-		return errors.Annotate(err, "Cannot initialize stats server")
82
+		return fmt.Errorf("cannot initialize stats server: %w", err)
84 83
 	}
85 84
 
86 85
 	srv := http.Server{

+ 5
- 5
stats/stats_prometheus.go 查看文件

@@ -1,10 +1,10 @@
1 1
 package stats
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"net"
5 6
 	"net/http"
6 7
 
7
-	"github.com/juju/errors"
8 8
 	"github.com/prometheus/client_golang/prometheus"
9 9
 	"github.com/prometheus/client_golang/prometheus/promhttp"
10 10
 
@@ -91,16 +91,16 @@ func newStatsPrometheus(mux *http.ServeMux) (Stats, error) {
91 91
 	}
92 92
 
93 93
 	if err := registry.Register(instance.connections); err != nil {
94
-		return nil, errors.Annotate(err, "Cannot register metrics for connections")
94
+		return nil, fmt.Errorf("cannot register metrics for connections: %w", err)
95 95
 	}
96 96
 	if err := registry.Register(instance.traffic); err != nil {
97
-		return nil, errors.Annotate(err, "Cannot register metrics for traffic")
97
+		return nil, fmt.Errorf("cannot register metrics for traffic: %w", err)
98 98
 	}
99 99
 	if err := registry.Register(instance.crashes); err != nil {
100
-		return nil, errors.Annotate(err, "Cannot register metrics for crashes")
100
+		return nil, fmt.Errorf("cannot register metrics for crashes: %w", err)
101 101
 	}
102 102
 	if err := registry.Register(instance.antiReplays); err != nil {
103
-		return nil, errors.Annotate(err, "Cannot register metrics for anti replays")
103
+		return nil, fmt.Errorf("cannot register metrics for anti replays: %w", err)
104 104
 	}
105 105
 
106 106
 	handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})

+ 2
- 2
stats/stats_statsd.go 查看文件

@@ -1,10 +1,10 @@
1 1
 package stats
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"net"
5 6
 	"strings"
6 7
 
7
-	"github.com/juju/errors"
8 8
 	"gopkg.in/alexcesaro/statsd.v2"
9 9
 
10 10
 	"github.com/9seconds/mtg/config"
@@ -78,7 +78,7 @@ func newStatsStatsd() (Stats, error) {
78 78
 
79 79
 	client, err := statsd.New(options...)
80 80
 	if err != nil {
81
-		return nil, errors.Annotate(err, "Cannot initialize a client")
81
+		return nil, fmt.Errorf("cannot initialize a client: %w", err)
82 82
 	}
83 83
 
84 84
 	return &statsStatsd{

+ 3
- 4
telegram/base.go 查看文件

@@ -2,12 +2,11 @@ package telegram
2 2
 
3 3
 import (
4 4
 	"context"
5
+	"fmt"
5 6
 	"math/rand"
6 7
 	"net"
7 8
 	"time"
8 9
 
9
-	"github.com/juju/errors"
10
-
11 10
 	"github.com/9seconds/mtg/conntypes"
12 11
 	"github.com/9seconds/mtg/utils"
13 12
 	"github.com/9seconds/mtg/wrappers"
@@ -29,11 +28,11 @@ func (b *baseTelegram) dialToAddress(ctx context.Context,
29 28
 	addr string) (wrappers.StreamReadWriteCloser, error) {
30 29
 	conn, err := b.dialer.Dial("tcp", addr)
31 30
 	if err != nil {
32
-		return nil, errors.Annotate(err, "Dial has failed")
31
+		return nil, fmt.Errorf("dial has failed: %w", err)
33 32
 	}
34 33
 
35 34
 	if err := utils.InitTCP(conn); err != nil {
36
-		return nil, errors.Annotate(err, "Cannot initialize TCP socket")
35
+		return nil, fmt.Errorf("cannot initialize tcp socket: %w", err)
37 36
 	}
38 37
 
39 38
 	return wrappers.NewTelegramConn(ctx, cancel, conn), nil

+ 4
- 5
utils/init_tcp.go 查看文件

@@ -1,10 +1,9 @@
1 1
 package utils
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"net"
5 6
 
6
-	"github.com/juju/errors"
7
-
8 7
 	"github.com/9seconds/mtg/config"
9 8
 )
10 9
 
@@ -12,13 +11,13 @@ func InitTCP(conn net.Conn) error {
12 11
 	tcpConn := conn.(*net.TCPConn)
13 12
 
14 13
 	if err := tcpConn.SetNoDelay(true); err != nil {
15
-		return errors.Annotate(err, "Cannot set NO_DELAY")
14
+		return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
16 15
 	}
17 16
 	if err := tcpConn.SetReadBuffer(config.C.BufferSize.Read); err != nil {
18
-		return errors.Annotate(err, "Cannot set read buffer size")
17
+		return fmt.Errorf("cannot set read buffer size: %w", err)
19 18
 	}
20 19
 	if err := tcpConn.SetWriteBuffer(config.C.BufferSize.Write); err != nil {
21
-		return errors.Annotate(err, "Cannot set write buffer size")
20
+		return fmt.Errorf("cannot set write buffer size: %w", err)
22 21
 	}
23 22
 
24 23
 	return nil

+ 8
- 11
utils/rlimit.go 查看文件

@@ -3,24 +3,21 @@
3 3
 package utils
4 4
 
5 5
 import (
6
-	"golang.org/x/sys/unix"
6
+	"fmt"
7 7
 
8
-	"github.com/juju/errors"
8
+	"golang.org/x/sys/unix"
9 9
 )
10 10
 
11
-func SetLimits() (err error) {
11
+func SetLimits() error {
12 12
 	rLimit := unix.Rlimit{}
13
-	err = unix.Getrlimit(unix.RLIMIT_NOFILE, &rLimit)
14
-	if err != nil {
15
-		err = errors.Annotate(err, "Cannot get rlimit")
16
-		return
13
+	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rLimit); err != nil {
14
+		return fmt.Errorf("cannot get rlimit: %w", err)
17 15
 	}
18 16
 	rLimit.Cur = rLimit.Max
19 17
 
20
-	err = unix.Setrlimit(unix.RLIMIT_NOFILE, &rLimit)
21
-	if err != nil {
22
-		err = errors.Annotate(err, "Cannot set rlimit")
18
+	if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &rLimit); err != nil {
19
+		return fmt.Errorf("cannot set rlimit: %w", err)
23 20
 	}
24 21
 
25
-	return
22
+	return nil
26 23
 }

+ 5
- 5
wrappers/wrapper_conn.go 查看文件

@@ -4,10 +4,10 @@ import (
4 4
 	"context"
5 5
 	"crypto/rand"
6 6
 	"encoding/hex"
7
+	"fmt"
7 8
 	"net"
8 9
 	"time"
9 10
 
10
-	"github.com/juju/errors"
11 11
 	"go.uber.org/zap"
12 12
 
13 13
 	"github.com/9seconds/mtg/config"
@@ -47,12 +47,12 @@ func (w *wrapperConn) WriteTimeout(p []byte, timeout time.Duration) (int, error)
47 47
 	select {
48 48
 	case <-w.ctx.Done():
49 49
 		w.Close()
50
-		return 0, errors.Annotate(w.ctx.Err(), "Cannot write because context was closed")
50
+		return 0, fmt.Errorf("cannot write because context was closed: %w", w.ctx.Err())
51 51
 
52 52
 	default:
53 53
 		if err := w.parent.SetWriteDeadline(time.Now().Add(timeout)); err != nil {
54 54
 			w.Close() // nolint: gosec
55
-			return 0, errors.Annotate(err, "Cannot set write deadline to the socket")
55
+			return 0, fmt.Errorf("cannot set write deadline to the socket: %w", err)
56 56
 		}
57 57
 
58 58
 		n, err := w.parent.Write(p)
@@ -73,12 +73,12 @@ func (w *wrapperConn) ReadTimeout(p []byte, timeout time.Duration) (int, error)
73 73
 	select {
74 74
 	case <-w.ctx.Done():
75 75
 		w.Close()
76
-		return 0, errors.Annotate(w.ctx.Err(), "Cannot read because context was closed")
76
+		return 0, fmt.Errorf("cannot read because context was closed: %w", w.ctx.Err())
77 77
 
78 78
 	default:
79 79
 		if err := w.parent.SetReadDeadline(time.Now().Add(timeout)); err != nil {
80 80
 			w.Close()
81
-			return 0, errors.Annotate(err, "Cannot set read deadline to the socket")
81
+			return 0, fmt.Errorf("cannot set read deadline to the socket: %w", err)
82 82
 		}
83 83
 
84 84
 		n, err := w.parent.Read(p)

+ 3
- 3
wrappers/wrapper_obfuscated2.go 查看文件

@@ -2,10 +2,10 @@ package wrappers
2 2
 
3 3
 import (
4 4
 	"crypto/cipher"
5
+	"fmt"
5 6
 	"net"
6 7
 	"time"
7 8
 
8
-	"github.com/juju/errors"
9 9
 	"go.uber.org/zap"
10 10
 )
11 11
 
@@ -18,7 +18,7 @@ type wrapperObfuscated2 struct {
18 18
 func (w *wrapperObfuscated2) ReadTimeout(p []byte, timeout time.Duration) (int, error) {
19 19
 	n, err := w.parent.ReadTimeout(p, timeout)
20 20
 	if err != nil {
21
-		return 0, errors.Annotate(err, "Cannot read stream ciphered data")
21
+		return 0, fmt.Errorf("cannot read stream ciphered data: %w", err)
22 22
 	}
23 23
 	w.decryptor.XORKeyStream(p, p[:n])
24 24
 
@@ -28,7 +28,7 @@ func (w *wrapperObfuscated2) ReadTimeout(p []byte, timeout time.Duration) (int,
28 28
 func (w *wrapperObfuscated2) Read(p []byte) (int, error) {
29 29
 	n, err := w.parent.Read(p)
30 30
 	if err != nil {
31
-		return 0, errors.Annotate(err, "Cannot read stream ciphered data")
31
+		return n, err
32 32
 	}
33 33
 	w.decryptor.XORKeyStream(p, p[:n])
34 34
 

正在加载...
取消
保存