Skip to content

Commit ef28ccc

Browse files
Merge pull request #42 from matrix-org/linting
Introduce stricter lints
2 parents cff61fd + f4ec8c6 commit ef28ccc

File tree

6 files changed

+71
-40
lines changed

6 files changed

+71
-40
lines changed

.golangci.yaml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
linters-settings:
2-
lll:
3-
line-length: 120
2+
nlreturn:
3+
block-size: 2
44
wsl:
5-
allow-cuddle-declarations: true
5+
enforce-err-cuddling: true
6+
funlen:
7+
lines: 100
8+
statements: 50
9+
gomoddirectives:
10+
replace-allow-list:
11+
- maunium.net/go/mautrix # currently needed for the PoC to work (custom mautrix)
612

713
linters:
814
enable-all: true
915
disable:
10-
- nlreturn
11-
- exhaustivestruct
12-
- exhaustruct
13-
- cyclop
14-
- gochecknoglobals
15-
- gomoddirectives
16-
- exhaustive
17-
- funlen
18-
- gofumpt
19-
- interfacer
20-
- godox # We ought to enable this at some point
16+
- ifshort # deprecated
17+
- nosnakecase # deprecated
18+
- interfacer # deprecated
19+
- deadcode # deprecated
20+
- exhaustivestruct # deprecated
21+
- varcheck # deprecated
22+
- structcheck # deprecated
23+
- maligned # deprecated
24+
- scopelint # deprecated
25+
- golint # deprecated
26+
- rowserrcheck # https://github.com/golangci/golangci-lint/issues/2649
27+
- sqlclosecheck # https://github.com/golangci/golangci-lint/issues/2649
28+
- wastedassign # https://github.com/golangci/golangci-lint/issues/2649
29+
- gomnd # we use status code numbers and for our use case it's not practical
30+
- godox # we have TODOs at this stage of the project, enable in future
31+
- forbidigo # we use things like fmt.Printf for debugging, enable in future
32+
fast: true

src/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (c *Call) SendDataChannelMessage(msg event.SFUMessage) {
513513
}
514514

515515
func (c *Call) CheckKeepAliveTimestamp() {
516-
timeout := time.Second * time.Duration(config.KeepAliveTimeout)
516+
timeout := time.Second * time.Duration(c.conf.Config.KeepAliveTimeout)
517517
for range time.Tick(timeout) {
518518
if c.lastKeepAliveTimestamp.Add(timeout).Before(time.Now()) {
519519
if c.PeerConnection.ConnectionState() != webrtc.PeerConnectionStateClosed {

src/conference.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,32 @@ import (
2525
"maunium.net/go/mautrix/id"
2626
)
2727

28-
var ErrNoSuchCall = errors.New("no such call")
29-
var ErrFoundExistingCallWithSameSessionAndDeviceID = errors.New("found existing call with equal DeviceID and SessionID")
28+
var (
29+
ErrNoSuchCall = errors.New("no such call")
30+
ErrFoundExistingCallWithSameSessionAndDeviceID = errors.New("found existing call with equal DeviceID and SessionID")
31+
)
32+
33+
// Configuration for the group conferences (calls).
34+
type ConferenceConfig struct {
35+
// Keep-alive timeout for WebRTC connections. If no keep-alive has been received
36+
// from the client for this duration, the connection is considered dead.
37+
KeepAliveTimeout int
38+
}
3039

3140
type Conference struct {
3241
ConfID string
33-
Calls map[string]*Call // By callID
42+
Calls map[string]*Call // By callID
43+
Config *ConferenceConfig // TODO: this must be protected by a mutex actually
3444

3545
mutex sync.RWMutex
3646
logger *logrus.Entry
3747
Metadata *Metadata
3848
}
3949

40-
func NewConference(confID string) *Conference {
50+
func NewConference(confID string, config *ConferenceConfig) *Conference {
4151
conference := new(Conference)
4252

53+
conference.Config = config
4354
conference.ConfID = confID
4455
conference.Calls = make(map[string]*Call)
4556
conference.Metadata = NewMetadata(conference)

src/focus.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ type Focus struct {
3838
client *mautrix.Client
3939
confs Confs
4040
logger *logrus.Entry
41+
config *ConferenceConfig
4142
}
4243

43-
func NewFocus(name string, client *mautrix.Client) *Focus {
44+
func NewFocus(name string, client *mautrix.Client, config *ConferenceConfig) *Focus {
4445
focus := new(Focus)
4546

4647
focus.name = name
4748
focus.client = client
4849
focus.confs.confs = make(map[string]*Conference)
4950
focus.logger = logrus.WithFields(logrus.Fields{})
51+
focus.config = config
5052

5153
return focus
5254
}
@@ -58,7 +60,7 @@ func (f *Focus) GetConf(confID string, create bool) (*Conference, error) {
5860

5961
if conference == nil {
6062
if create {
61-
conference = NewConference(confID)
63+
conference = NewConference(confID, f.config)
6264
f.confs.confs[confID] = conference
6365
} else {
6466
return nil, ErrNoSuchConference
@@ -69,9 +71,11 @@ func (f *Focus) GetConf(confID string, create bool) (*Conference, error) {
6971
}
7072

7173
func (f *Focus) getExistingCall(confID string, callID string) (*Call, error) {
72-
var conf *Conference
73-
var call *Call
74-
var err error
74+
var (
75+
conf *Conference
76+
call *Call
77+
err error
78+
)
7579

7680
if conf, err = f.GetConf(confID, false); err != nil || conf == nil {
7781
f.logger.Printf("failed to get conf %s: %s", confID, err)
@@ -110,9 +114,11 @@ func (f *Focus) onEvent(_ mautrix.EventSource, evt *event.Event) {
110114
return
111115
}
112116

113-
var conf *Conference
114-
var call *Call
115-
var err error
117+
var (
118+
conf *Conference
119+
call *Call
120+
err error
121+
)
116122

117123
switch evt.Type.Type {
118124
case event.ToDeviceCallInvite.Type:

src/main.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ import (
2727
"github.com/sirupsen/logrus"
2828
)
2929

30-
var config *Config
31-
32-
var logTime = flag.Bool("logTime", false, "whether or not to print time and date in logs")
33-
var configFilePath = flag.String("config", "config.yaml", "configuration file path")
34-
var cpuProfile = flag.String("cpuProfile", "", "write CPU profile to `file`")
35-
var memProfile = flag.String("memProfile", "", "write memory profile to `file`")
36-
3730
func initCPUProfiling(cpuProfile *string) func() {
3831
logrus.Info("initializing CPU profiling")
3932

@@ -95,6 +88,13 @@ func killListener(c chan os.Signal, beforeExit []func()) {
9588
}
9689

9790
func main() {
91+
var (
92+
logTime = flag.Bool("logTime", false, "whether or not to print time and date in logs")
93+
configFilePath = flag.String("config", "config.yaml", "configuration file path")
94+
cpuProfile = flag.String("cpuProfile", "", "write CPU profile to `file`")
95+
memProfile = flag.String("memProfile", "", "write memory profile to `file`")
96+
)
97+
9898
flag.Parse()
9999

100100
initLogging(logTime)
@@ -115,12 +115,10 @@ func main() {
115115

116116
go killListener(c, beforeExit)
117117

118-
var err error
119-
config, err = loadConfig(*configFilePath)
120-
118+
config, err := loadConfig(*configFilePath)
121119
if err != nil {
122120
logrus.WithError(err).Fatal("could not load config")
123121
}
124122

125-
InitMatrix()
123+
InitMatrix(config)
126124
}

src/matrix.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
const localSessionID = "sfu"
2727

28-
func InitMatrix() {
28+
func InitMatrix(config *Config) {
2929
client, err := mautrix.NewClient(config.HomeserverURL, config.UserID, config.AccessToken)
3030
if err != nil {
3131
logrus.WithError(err).Fatal("Failed to create client")
@@ -43,7 +43,11 @@ func InitMatrix() {
4343
logrus.WithField("device_id", whoami.DeviceID).Info("Identified SFU as DeviceID")
4444
client.DeviceID = whoami.DeviceID
4545

46-
focus := NewFocus(fmt.Sprintf("%s (%s)", config.UserID, client.DeviceID), client)
46+
focus := NewFocus(
47+
fmt.Sprintf("%s (%s)", config.UserID, client.DeviceID),
48+
client,
49+
&ConferenceConfig{KeepAliveTimeout: config.KeepAliveTimeout},
50+
)
4751

4852
syncer, ok := client.Syncer.(*mautrix.DefaultSyncer)
4953
if !ok {

0 commit comments

Comments
 (0)