GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Setup Failed
Push — master ( e8c327...d95ec3 )
by Amir
17:24
created

main.serve   D

Complexity

Conditions 12

Size

Total Lines 58
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 42
dl 0
loc 58
rs 4.8
c 0
b 0
f 0
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like main.serve often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package main
2
3
import (
4
	"flag"
5
	cache "github.com/arazmj/gerdu/cache"
6
	"github.com/arazmj/gerdu/grpcserver"
7
	"github.com/arazmj/gerdu/httpserver"
8
	"github.com/arazmj/gerdu/lfucache"
9
	"github.com/arazmj/gerdu/lrucache"
10
	"github.com/arazmj/gerdu/memcached"
11
	"github.com/arazmj/gerdu/raftproxy"
12
	"github.com/arazmj/gerdu/redis"
13
	"github.com/arazmj/gerdu/weakcache"
14
	"github.com/inhies/go-bytesize"
15
	log "github.com/sirupsen/logrus"
16
	"os"
17
	"os/signal"
18
	"strconv"
19
	"strings"
20
	"sync"
21
)
22
23
var gerdu raftproxy.RaftCache
24
var wg = sync.WaitGroup{}
25
26
var (
27
	loglevel = flag.String("log", "info",
28
		"log level can be any of values of 'panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace'")
29
	capacityStr = flag.String("capacity", "64MB",
30
		"The size of cache, once cache reached this capacity old values will evicted.\n"+
31
			"Specify a numerical value followed by one of the following units (not case sensitive)"+
32
			"\nK or KB: Kilobytes"+
33
			"\nM or MB: Megabytes"+
34
			"\nG or GB: Gigabytes"+
35
			"\nT or TB: Terabytes")
36
	httpPort  = flag.Int("httpport", 8080, "http server port number")
37
	grpcPort  = flag.Int("grpcport", 8081, "grpc server port number")
38
	mcdPort   = flag.Int("mcdport", 11211, "memcached server port number")
39
	redisPort = flag.Int("redisport", 6379, "redis server port number")
40
	kind      = flag.String("type", "lru", "type of cache, lru or lfu, weak")
41
	protocols = flag.String("protocols", "",
42
		"protocol 'grpc', 'redis' or 'mcd' (memcached), multiple comma-separated values, http is not optional")
43
	tlsKey   = flag.String("key", "", "SSL certificate private key")
44
	tlsCert  = flag.String("cert", "", "SSL certificate public key")
45
	host     = flag.String("host", "127.0.0.1", "The host that server listens")
46
	raftAddr = flag.String("raft", "127.0.0.1:12000", "Set Raft bind address")
47
	joinAddr = flag.String("join", "", "Set join address, if any")
48
	nodeID   = flag.String("id", "master", "Node ID")
49
	storage  = flag.String("storage", "", "Path to store log files and snapshot, will store in memory if not set")
50
51
	secure = len(*tlsCert) > 0 && len(*tlsKey) > 0
52
)
53
54
func main() {
55
	flag.Parse()
56
	setLogLevel()
57
	setCache()
58
	serve()
59
}
60
61
func serve() {
62
	*protocols = strings.ToLower(*protocols)
63
64
	wg.Add(1)
65
	go func() {
66
		defer wg.Done()
67
		httpHost := *host + ":" + strconv.Itoa(*httpPort)
68
		if secure {
69
			httpserver.HTTPServeTLS(httpHost, *tlsCert, *tlsKey, gerdu)
70
		} else {
71
			httpserver.HTTPServe(httpHost, gerdu)
72
		}
73
	}()
74
75
	if strings.Contains(*protocols, "grpc") {
76
		wg.Add(1)
77
		go func() {
78
			defer wg.Done()
79
			grpcHost := *host + ":" + strconv.Itoa(*grpcPort)
80
			if secure {
81
				grpcserver.GrpcServeTLS(grpcHost, *tlsCert, *tlsKey, gerdu)
82
			} else {
83
				grpcserver.GrpcServe(grpcHost, gerdu)
84
			}
85
		}()
86
	}
87
	if strings.Contains(*protocols, "mcd") {
88
		wg.Add(1)
89
		go func() {
90
			defer wg.Done()
91
			mcdHost := *host + ":" + strconv.Itoa(*mcdPort)
92
			if secure {
93
				log.Fatalln("Memcached protocol does not support TLS")
94
				os.Exit(1)
95
			}
96
			memcached.Serve(mcdHost, gerdu)
97
		}()
98
	}
99
100
	if strings.Contains(*protocols, "redis") {
101
		wg.Add(1)
102
		go func() {
103
			defer wg.Done()
104
			redisHost := *host + ":" + strconv.Itoa(*redisPort)
105
			if secure {
106
				redis.ServeTLS(redisHost, *tlsCert, *tlsKey, gerdu)
107
			} else {
108
				redis.Serve(redisHost, gerdu)
109
			}
110
		}()
111
	}
112
113
	wg.Wait()
114
115
	terminate := make(chan os.Signal, 1)
116
	signal.Notify(terminate, os.Interrupt)
117
	<-terminate
118
	log.Println("Gerdu exiting")
119
}
120
121
func setCache() {
122
	capacity, err := bytesize.Parse(*capacityStr)
123
	if err != nil {
124
		log.Fatal("Invalid value for capacity", err.Error())
125
	}
126
127
	var c cache.UnImplementedCache
128
	if strings.ToLower(*kind) == "lru" {
129
		c = lrucache.NewCache(capacity)
130
	} else if strings.ToLower(*kind) == "lfu" {
131
		c = lfucache.NewCache(capacity)
132
	} else if strings.ToLower(*kind) == "weak" {
133
		c = weakcache.NewWeakCache()
134
	} else {
135
		log.Fatalf("Invalid value for type")
136
		os.Exit(1)
137
	}
138
	gerdu = raftproxy.NewRaftProxy(c, *raftAddr, *joinAddr, *nodeID)
139
	err = gerdu.OpenRaft(*storage)
140
	if err != nil {
141
		log.Fatalf("Cannot open raft peer connection: %s", err)
142
	}
143
144
}
145
146
func setLogLevel() {
147
	switch *loglevel {
148
	case "panic":
149
		log.SetLevel(log.PanicLevel)
150
	case "fatal":
151
		log.SetLevel(log.FatalLevel)
152
	case "error":
153
		log.SetLevel(log.ErrorLevel)
154
	case "warn":
155
		log.SetLevel(log.WarnLevel)
156
	case "info":
157
		log.SetLevel(log.InfoLevel)
158
	case "debug":
159
		log.SetLevel(log.DebugLevel)
160
	case "trace":
161
		log.SetReportCaller(true)
162
		log.SetLevel(log.DebugLevel)
163
	default:
164
		log.Fatalf("Invalid log level value %s\n", *loglevel)
165
		os.Exit(1)
166
	}
167
}
168