Ver código fonte

remove unnecessary indirection in semaphore

tags/v2.6.0-rc1
Shivaram Lingamneni 3 anos atrás
pai
commit
5b33cd436f
7 arquivos alterados com 39 adições e 40 exclusões
  1. 6
    6
      irc/channel.go
  2. 12
    11
      irc/client.go
  3. 1
    1
      irc/semaphores.go
  4. 2
    2
      irc/server.go
  5. 3
    3
      irc/socket.go
  6. 13
    13
      irc/utils/semaphores.go
  7. 2
    4
      irc/utils/semaphores_test.go

+ 6
- 6
irc/channel.go Ver arquivo

@@ -61,15 +61,15 @@ func NewChannel(s *Server, name, casefoldedName string, registered bool) *Channe
61 61
 	config := s.Config()
62 62
 
63 63
 	channel := &Channel{
64
-		createdTime:    time.Now().UTC(), // may be overwritten by applyRegInfo
65
-		members:        make(MemberSet),
66
-		name:           name,
67
-		nameCasefolded: casefoldedName,
68
-		server:         s,
64
+		createdTime:     time.Now().UTC(), // may be overwritten by applyRegInfo
65
+		members:         make(MemberSet),
66
+		name:            name,
67
+		nameCasefolded:  casefoldedName,
68
+		server:          s,
69
+		writerSemaphore: utils.NewSemaphore(1),
69 70
 	}
70 71
 
71 72
 	channel.initializeLists()
72
-	channel.writerSemaphore.Initialize(1)
73 73
 	channel.history.Initialize(0, 0)
74 74
 
75 75
 	if !registered {

+ 12
- 11
irc/client.go Ver arquivo

@@ -356,20 +356,20 @@ func (server *Server) RunClient(conn IRCConn) {
356 356
 			Duration: config.Accounts.LoginThrottling.Duration,
357 357
 			Limit:    config.Accounts.LoginThrottling.MaxAttempts,
358 358
 		},
359
-		server:         server,
360
-		accountName:    "*",
361
-		nick:           "*", // * is used until actual nick is given
362
-		nickCasefolded: "*",
363
-		nickMaskString: "*", // * is used until actual nick is given
364
-		realIP:         realIP,
365
-		proxiedIP:      proxiedIP,
366
-		requireSASL:    requireSASL,
367
-		nextSessionID:  1,
359
+		server:          server,
360
+		accountName:     "*",
361
+		nick:            "*", // * is used until actual nick is given
362
+		nickCasefolded:  "*",
363
+		nickMaskString:  "*", // * is used until actual nick is given
364
+		realIP:          realIP,
365
+		proxiedIP:       proxiedIP,
366
+		requireSASL:     requireSASL,
367
+		nextSessionID:   1,
368
+		writerSemaphore: utils.NewSemaphore(1),
368 369
 	}
369 370
 	if requireSASL {
370 371
 		client.requireSASLMessage = banMsg
371 372
 	}
372
-	client.writerSemaphore.Initialize(1)
373 373
 	client.history.Initialize(config.History.ClientLength, time.Duration(config.History.AutoresizeWindow))
374 374
 	client.brbTimer.Initialize(client)
375 375
 	session := &Session{
@@ -445,6 +445,8 @@ func (server *Server) AddAlwaysOnClient(account ClientAccount, channelToStatus m
445 445
 		realname: realname,
446 446
 
447 447
 		nextSessionID: 1,
448
+
449
+		writerSemaphore: utils.NewSemaphore(1),
448 450
 	}
449 451
 
450 452
 	if client.checkAlwaysOnExpirationNoMutex(config, true) {
@@ -456,7 +458,6 @@ func (server *Server) AddAlwaysOnClient(account ClientAccount, channelToStatus m
456 458
 	for _, m := range uModes {
457 459
 		client.SetMode(m, true)
458 460
 	}
459
-	client.writerSemaphore.Initialize(1)
460 461
 	client.history.Initialize(0, 0)
461 462
 	client.brbTimer.Initialize(client)
462 463
 

+ 1
- 1
irc/semaphores.go Ver arquivo

@@ -37,5 +37,5 @@ func (serversem *ServerSemaphores) Initialize() {
37 37
 	if capacity > MaxServerSemaphoreCapacity {
38 38
 		capacity = MaxServerSemaphoreCapacity
39 39
 	}
40
-	serversem.ClientDestroy.Initialize(capacity)
40
+	serversem.ClientDestroy = utils.NewSemaphore(capacity)
41 41
 }

+ 2
- 2
irc/server.go Ver arquivo

@@ -612,11 +612,11 @@ func (server *Server) applyConfig(config *Config) (err error) {
612 612
 	if initial {
613 613
 		maxIPConc := int(config.Server.IPCheckScript.MaxConcurrency)
614 614
 		if maxIPConc != 0 {
615
-			server.semaphores.IPCheckScript.Initialize(maxIPConc)
615
+			server.semaphores.IPCheckScript = utils.NewSemaphore(maxIPConc)
616 616
 		}
617 617
 		maxAuthConc := int(config.Accounts.AuthScript.MaxConcurrency)
618 618
 		if maxAuthConc != 0 {
619
-			server.semaphores.AuthScript.Initialize(maxAuthConc)
619
+			server.semaphores.AuthScript = utils.NewSemaphore(maxAuthConc)
620 620
 		}
621 621
 
622 622
 		if err := overrideServicePrefixes(config.Server.OverrideServicesHostname); err != nil {

+ 3
- 3
irc/socket.go Ver arquivo

@@ -40,10 +40,10 @@ type Socket struct {
40 40
 // NewSocket returns a new Socket.
41 41
 func NewSocket(conn IRCConn, maxSendQBytes int) *Socket {
42 42
 	result := Socket{
43
-		conn:          conn,
44
-		maxSendQBytes: maxSendQBytes,
43
+		conn:            conn,
44
+		maxSendQBytes:   maxSendQBytes,
45
+		writerSemaphore: utils.NewSemaphore(1),
45 46
 	}
46
-	result.writerSemaphore.Initialize(1)
47 47
 	return &result
48 48
 }
49 49
 

+ 13
- 13
irc/utils/semaphores.go Ver arquivo

@@ -12,21 +12,21 @@ import (
12 12
 // A semaphore of capacity 1 can be used as a trylock.
13 13
 type Semaphore (chan empty)
14 14
 
15
-// Initialize initializes a semaphore to a given capacity.
16
-func (semaphore *Semaphore) Initialize(capacity int) {
17
-	*semaphore = make(chan empty, capacity)
15
+// NewSemaphore creates and initializes a semaphore to a given capacity.
16
+func NewSemaphore(capacity int) Semaphore {
17
+	return make(chan empty, capacity)
18 18
 }
19 19
 
20 20
 // Acquire acquires a semaphore, blocking if necessary.
21
-func (semaphore *Semaphore) Acquire() {
22
-	(*semaphore) <- empty{}
21
+func (semaphore Semaphore) Acquire() {
22
+	semaphore <- empty{}
23 23
 }
24 24
 
25 25
 // TryAcquire tries to acquire a semaphore, returning whether the acquire was
26 26
 // successful. It never blocks.
27
-func (semaphore *Semaphore) TryAcquire() (acquired bool) {
27
+func (semaphore Semaphore) TryAcquire() (acquired bool) {
28 28
 	select {
29
-	case (*semaphore) <- empty{}:
29
+	case semaphore <- empty{}:
30 30
 		return true
31 31
 	default:
32 32
 		return false
@@ -36,14 +36,14 @@ func (semaphore *Semaphore) TryAcquire() (acquired bool) {
36 36
 // AcquireWithTimeout tries to acquire a semaphore, blocking for a maximum
37 37
 // of approximately `d` while waiting for it. It returns whether the acquire
38 38
 // was successful.
39
-func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
39
+func (semaphore Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
40 40
 	if timeout < 0 {
41 41
 		return semaphore.TryAcquire()
42 42
 	}
43 43
 
44 44
 	timer := time.NewTimer(timeout)
45 45
 	select {
46
-	case (*semaphore) <- empty{}:
46
+	case semaphore <- empty{}:
47 47
 		acquired = true
48 48
 	case <-timer.C:
49 49
 		acquired = false
@@ -55,9 +55,9 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
55 55
 // AcquireWithContext tries to acquire a semaphore, blocking at most until
56 56
 // the context expires. It returns whether the acquire was successful.
57 57
 // Note that if the context is already expired, the acquire may succeed anyway.
58
-func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
58
+func (semaphore Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
59 59
 	select {
60
-	case (*semaphore) <- empty{}:
60
+	case semaphore <- empty{}:
61 61
 		acquired = true
62 62
 	case <-ctx.Done():
63 63
 		acquired = false
@@ -66,6 +66,6 @@ func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bo
66 66
 }
67 67
 
68 68
 // Release releases a semaphore.
69
-func (semaphore *Semaphore) Release() {
70
-	<-(*semaphore)
69
+func (semaphore Semaphore) Release() {
70
+	<-semaphore
71 71
 }

+ 2
- 4
irc/utils/semaphores_test.go Ver arquivo

@@ -10,8 +10,7 @@ import (
10 10
 
11 11
 func TestTryAcquire(t *testing.T) {
12 12
 	count := 3
13
-	var sem Semaphore
14
-	sem.Initialize(count)
13
+	sem := NewSemaphore(count)
15 14
 
16 15
 	for i := 0; i < count; i++ {
17 16
 		assertEqual(sem.TryAcquire(), true, t)
@@ -24,8 +23,7 @@ func TestTryAcquire(t *testing.T) {
24 23
 }
25 24
 
26 25
 func TestAcquireWithTimeout(t *testing.T) {
27
-	var sem Semaphore
28
-	sem.Initialize(1)
26
+	sem := NewSemaphore(1)
29 27
 
30 28
 	assertEqual(sem.TryAcquire(), true, t)
31 29
 

Carregando…
Cancelar
Salvar