aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/context.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet/service/context.go')
-rw-r--r--src/gnunet/service/context.go87
1 files changed, 0 insertions, 87 deletions
diff --git a/src/gnunet/service/context.go b/src/gnunet/service/context.go
deleted file mode 100644
index 4ae786d..0000000
--- a/src/gnunet/service/context.go
+++ /dev/null
@@ -1,87 +0,0 @@
1// This file is part of gnunet-go, a GNUnet-implementation in Golang.
2// Copyright (C) 2019, 2020 Bernd Fix >Y<
3//
4// gnunet-go is free software: you can redistribute it and/or modify it
5// under the terms of the GNU Affero General Public License as published
6// by the Free Software Foundation, either version 3 of the License,
7// or (at your option) any later version.
8//
9// gnunet-go is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Affero General Public License for more details.
13//
14// You should have received a copy of the GNU Affero General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//
17// SPDX-License-Identifier: AGPL3.0-or-later
18
19package service
20
21import (
22 "sync"
23
24 "gnunet/util"
25
26 "github.com/bfix/gospel/concurrent"
27)
28
29// SessionContext is used to set a context for each client connection handled
30// by a service; the session is handled by the 'ServeClient' method of a
31// service implementation.
32type SessionContext struct {
33 ID int // session identifier
34 wg *sync.WaitGroup // wait group for the session
35 sig *concurrent.Signaller // signaller for the session
36 pending int // number of pending go-routines
37 active bool // is the context active (un-cancelled)?
38 onCancel *sync.Mutex // only run one Cancel() at a time
39}
40
41// NewSessionContext instantiates a new session context.
42func NewSessionContext() *SessionContext {
43 return &SessionContext{
44 ID: util.NextID(),
45 wg: new(sync.WaitGroup),
46 sig: concurrent.NewSignaller(),
47 pending: 0,
48 active: true,
49 onCancel: new(sync.Mutex),
50 }
51}
52
53// Cancel all go-routines associated with this context.
54func (ctx *SessionContext) Cancel() {
55 ctx.onCancel.Lock()
56 if ctx.active {
57 // we are going out-of-business
58 ctx.active = false
59 // send signal to terminate...
60 ctx.sig.Send(true)
61 // wait for session go-routines to finish
62 ctx.wg.Wait()
63 }
64 ctx.onCancel.Unlock()
65}
66
67// Add a go-routine to the wait group.
68func (ctx *SessionContext) Add() {
69 ctx.wg.Add(1)
70 ctx.pending++
71}
72
73// Remove a go-routine from the wait group.
74func (ctx *SessionContext) Remove() {
75 ctx.wg.Done()
76 ctx.pending--
77}
78
79// Waiting returns the number of waiting go-routines.
80func (ctx *SessionContext) Waiting() int {
81 return ctx.pending
82}
83
84// Signaller returns the working instance for the context.
85func (ctx *SessionContext) Signaller() *concurrent.Signaller {
86 return ctx.sig
87}