Skip to content

Commit 8adb229

Browse files
authored
Merge pull request #1307 from jehiah/ctx_1307
*: remove *Context
2 parents c3a47b9 + c19adf3 commit 8adb229

21 files changed

+379
-399
lines changed

nsqadmin/context.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

nsqadmin/http.go

Lines changed: 96 additions & 96 deletions
Large diffs are not rendered by default.

nsqadmin/notify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func basicAuthUser(req *http.Request) string {
3939
}
4040

4141
func (s *httpServer) notifyAdminAction(action, topic, channel, node string, req *http.Request) {
42-
if s.ctx.nsqadmin.getOpts().NotificationHTTPEndpoint == "" {
42+
if s.nsqadmin.getOpts().NotificationHTTPEndpoint == "" {
4343
return
4444
}
4545
via, _ := os.Hostname()
@@ -67,5 +67,5 @@ func (s *httpServer) notifyAdminAction(action, topic, channel, node string, req
6767
Via: via,
6868
}
6969
// Perform all work in a new goroutine so this never blocks
70-
go func() { s.ctx.nsqadmin.notifications <- a }()
70+
go func() { s.nsqadmin.notifications <- a }()
7171
}

nsqadmin/nsqadmin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (n *NSQAdmin) Main() error {
179179
})
180180
}
181181

182-
httpServer := NewHTTPServer(&Context{n})
182+
httpServer := NewHTTPServer(n)
183183
n.waitGroup.Wrap(func() {
184184
exitFunc(http_api.Serve(n.httpListener, http_api.CompressHandler(httpServer), "HTTP", n.logf))
185185
})

nsqd/channel.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Channel struct {
4343

4444
topicName string
4545
name string
46-
ctx *context
46+
nsqd *NSQD
4747

4848
backend BackendQueue
4949

@@ -71,7 +71,7 @@ type Channel struct {
7171
}
7272

7373
// NewChannel creates a new instance of the Channel type and returns a pointer
74-
func NewChannel(topicName string, channelName string, ctx *context,
74+
func NewChannel(topicName string, channelName string, nsqd *NSQD,
7575
deleteCallback func(*Channel)) *Channel {
7676

7777
c := &Channel{
@@ -80,16 +80,16 @@ func NewChannel(topicName string, channelName string, ctx *context,
8080
memoryMsgChan: nil,
8181
clients: make(map[int64]Consumer),
8282
deleteCallback: deleteCallback,
83-
ctx: ctx,
83+
nsqd: nsqd,
8484
}
8585
// create mem-queue only if size > 0 (do not use unbuffered chan)
86-
if ctx.nsqd.getOpts().MemQueueSize > 0 {
87-
c.memoryMsgChan = make(chan *Message, ctx.nsqd.getOpts().MemQueueSize)
86+
if nsqd.getOpts().MemQueueSize > 0 {
87+
c.memoryMsgChan = make(chan *Message, nsqd.getOpts().MemQueueSize)
8888
}
89-
if len(ctx.nsqd.getOpts().E2EProcessingLatencyPercentiles) > 0 {
89+
if len(nsqd.getOpts().E2EProcessingLatencyPercentiles) > 0 {
9090
c.e2eProcessingLatencyStream = quantile.New(
91-
ctx.nsqd.getOpts().E2EProcessingLatencyWindowTime,
92-
ctx.nsqd.getOpts().E2EProcessingLatencyPercentiles,
91+
nsqd.getOpts().E2EProcessingLatencyWindowTime,
92+
nsqd.getOpts().E2EProcessingLatencyPercentiles,
9393
)
9494
}
9595

@@ -100,30 +100,30 @@ func NewChannel(topicName string, channelName string, ctx *context,
100100
c.backend = newDummyBackendQueue()
101101
} else {
102102
dqLogf := func(level diskqueue.LogLevel, f string, args ...interface{}) {
103-
opts := ctx.nsqd.getOpts()
103+
opts := nsqd.getOpts()
104104
lg.Logf(opts.Logger, opts.LogLevel, lg.LogLevel(level), f, args...)
105105
}
106106
// backend names, for uniqueness, automatically include the topic...
107107
backendName := getBackendName(topicName, channelName)
108108
c.backend = diskqueue.New(
109109
backendName,
110-
ctx.nsqd.getOpts().DataPath,
111-
ctx.nsqd.getOpts().MaxBytesPerFile,
110+
nsqd.getOpts().DataPath,
111+
nsqd.getOpts().MaxBytesPerFile,
112112
int32(minValidMsgLength),
113-
int32(ctx.nsqd.getOpts().MaxMsgSize)+minValidMsgLength,
114-
ctx.nsqd.getOpts().SyncEvery,
115-
ctx.nsqd.getOpts().SyncTimeout,
113+
int32(nsqd.getOpts().MaxMsgSize)+minValidMsgLength,
114+
nsqd.getOpts().SyncEvery,
115+
nsqd.getOpts().SyncTimeout,
116116
dqLogf,
117117
)
118118
}
119119

120-
c.ctx.nsqd.Notify(c)
120+
c.nsqd.Notify(c)
121121

122122
return c
123123
}
124124

125125
func (c *Channel) initPQ() {
126-
pqSize := int(math.Max(1, float64(c.ctx.nsqd.getOpts().MemQueueSize)/10))
126+
pqSize := int(math.Max(1, float64(c.nsqd.getOpts().MemQueueSize)/10))
127127

128128
c.inFlightMutex.Lock()
129129
c.inFlightMessages = make(map[MessageID]*Message)
@@ -160,13 +160,13 @@ func (c *Channel) exit(deleted bool) error {
160160
}
161161

162162
if deleted {
163-
c.ctx.nsqd.logf(LOG_INFO, "CHANNEL(%s): deleting", c.name)
163+
c.nsqd.logf(LOG_INFO, "CHANNEL(%s): deleting", c.name)
164164

165165
// since we are explicitly deleting a channel (not just at system exit time)
166166
// de-register this from the lookupd
167-
c.ctx.nsqd.Notify(c)
167+
c.nsqd.Notify(c)
168168
} else {
169-
c.ctx.nsqd.logf(LOG_INFO, "CHANNEL(%s): closing", c.name)
169+
c.nsqd.logf(LOG_INFO, "CHANNEL(%s): closing", c.name)
170170
}
171171

172172
// this forceably closes client connections
@@ -212,7 +212,7 @@ finish:
212212
// it does not drain inflight/deferred because it is only called in Close()
213213
func (c *Channel) flush() error {
214214
if len(c.memoryMsgChan) > 0 || len(c.inFlightMessages) > 0 || len(c.deferredMessages) > 0 {
215-
c.ctx.nsqd.logf(LOG_INFO, "CHANNEL(%s): flushing %d memory %d in-flight %d deferred messages to backend",
215+
c.nsqd.logf(LOG_INFO, "CHANNEL(%s): flushing %d memory %d in-flight %d deferred messages to backend",
216216
c.name, len(c.memoryMsgChan), len(c.inFlightMessages), len(c.deferredMessages))
217217
}
218218

@@ -221,7 +221,7 @@ func (c *Channel) flush() error {
221221
case msg := <-c.memoryMsgChan:
222222
err := writeMessageToBackend(msg, c.backend)
223223
if err != nil {
224-
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
224+
c.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
225225
}
226226
default:
227227
goto finish
@@ -233,7 +233,7 @@ finish:
233233
for _, msg := range c.inFlightMessages {
234234
err := writeMessageToBackend(msg, c.backend)
235235
if err != nil {
236-
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
236+
c.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
237237
}
238238
}
239239
c.inFlightMutex.Unlock()
@@ -243,7 +243,7 @@ finish:
243243
msg := item.Value.(*Message)
244244
err := writeMessageToBackend(msg, c.backend)
245245
if err != nil {
246-
c.ctx.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
246+
c.nsqd.logf(LOG_ERROR, "failed to write message to backend - %s", err)
247247
}
248248
}
249249
c.deferredMutex.Unlock()
@@ -306,9 +306,9 @@ func (c *Channel) put(m *Message) error {
306306
case c.memoryMsgChan <- m:
307307
default:
308308
err := writeMessageToBackend(m, c.backend)
309-
c.ctx.nsqd.SetHealth(err)
309+
c.nsqd.SetHealth(err)
310310
if err != nil {
311-
c.ctx.nsqd.logf(LOG_ERROR, "CHANNEL(%s): failed to write message to backend - %s",
311+
c.nsqd.logf(LOG_ERROR, "CHANNEL(%s): failed to write message to backend - %s",
312312
c.name, err)
313313
return err
314314
}
@@ -331,9 +331,9 @@ func (c *Channel) TouchMessage(clientID int64, id MessageID, clientMsgTimeout ti
331331

332332
newTimeout := time.Now().Add(clientMsgTimeout)
333333
if newTimeout.Sub(msg.deliveryTS) >=
334-
c.ctx.nsqd.getOpts().MaxMsgTimeout {
334+
c.nsqd.getOpts().MaxMsgTimeout {
335335
// we would have gone over, set to the max
336-
newTimeout = msg.deliveryTS.Add(c.ctx.nsqd.getOpts().MaxMsgTimeout)
336+
newTimeout = msg.deliveryTS.Add(c.nsqd.getOpts().MaxMsgTimeout)
337337
}
338338

339339
msg.pri = newTimeout.UnixNano()
@@ -398,7 +398,7 @@ func (c *Channel) AddClient(clientID int64, client Consumer) error {
398398
return nil
399399
}
400400

401-
maxChannelConsumers := c.ctx.nsqd.getOpts().MaxChannelConsumers
401+
maxChannelConsumers := c.nsqd.getOpts().MaxChannelConsumers
402402
if maxChannelConsumers != 0 && len(c.clients) >= maxChannelConsumers {
403403
return errors.New("E_TOO_MANY_CHANNEL_CONSUMERS")
404404
}

nsqd/channel_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestChannelEmptyConsumer(t *testing.T) {
150150
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
151151
topic := nsqd.GetTopic(topicName)
152152
channel := topic.GetChannel("channel")
153-
client := newClientV2(0, conn, &context{nsqd})
153+
client := newClientV2(0, conn, nsqd)
154154
client.SetReadyCount(25)
155155
err := channel.AddClient(client.ID, client)
156156
test.Equal(t, err, nil)
@@ -189,12 +189,12 @@ func TestMaxChannelConsumers(t *testing.T) {
189189
topic := nsqd.GetTopic(topicName)
190190
channel := topic.GetChannel("channel")
191191

192-
client1 := newClientV2(1, conn, &context{nsqd})
192+
client1 := newClientV2(1, conn, nsqd)
193193
client1.SetReadyCount(25)
194194
err := channel.AddClient(client1.ID, client1)
195195
test.Equal(t, err, nil)
196196

197-
client2 := newClientV2(2, conn, &context{nsqd})
197+
client2 := newClientV2(2, conn, nsqd)
198198
client2.SetReadyCount(25)
199199
err = channel.AddClient(client2.ID, client2)
200200
test.NotEqual(t, err, nil)

nsqd/client_v2.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type clientV2 struct {
6161
metaLock sync.RWMutex
6262

6363
ID int64
64-
ctx *context
64+
nsqd *NSQD
6565
UserAgent string
6666

6767
// original connection
@@ -108,25 +108,25 @@ type clientV2 struct {
108108
AuthState *auth.State
109109
}
110110

111-
func newClientV2(id int64, conn net.Conn, ctx *context) *clientV2 {
111+
func newClientV2(id int64, conn net.Conn, nsqd *NSQD) *clientV2 {
112112
var identifier string
113113
if conn != nil {
114114
identifier, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
115115
}
116116

117117
c := &clientV2{
118-
ID: id,
119-
ctx: ctx,
118+
ID: id,
119+
nsqd: nsqd,
120120

121121
Conn: conn,
122122

123123
Reader: bufio.NewReaderSize(conn, defaultBufferSize),
124124
Writer: bufio.NewWriterSize(conn, defaultBufferSize),
125125

126126
OutputBufferSize: defaultBufferSize,
127-
OutputBufferTimeout: ctx.nsqd.getOpts().OutputBufferTimeout,
127+
OutputBufferTimeout: nsqd.getOpts().OutputBufferTimeout,
128128

129-
MsgTimeout: ctx.nsqd.getOpts().MsgTimeout,
129+
MsgTimeout: nsqd.getOpts().MsgTimeout,
130130

131131
// ReadyStateChan has a buffer of 1 to guarantee that in the event
132132
// there is a race the state update is not lost
@@ -142,7 +142,7 @@ func newClientV2(id int64, conn net.Conn, ctx *context) *clientV2 {
142142
IdentifyEventChan: make(chan identifyEvent, 1),
143143

144144
// heartbeats are client configurable but default to 30s
145-
HeartbeatInterval: ctx.nsqd.getOpts().ClientTimeout / 2,
145+
HeartbeatInterval: nsqd.getOpts().ClientTimeout / 2,
146146

147147
pubCounts: make(map[string]uint64),
148148
}
@@ -155,7 +155,7 @@ func (c *clientV2) String() string {
155155
}
156156

157157
func (c *clientV2) Identify(data identifyDataV2) error {
158-
c.ctx.nsqd.logf(LOG_INFO, "[%s] IDENTIFY: %+v", c, data)
158+
c.nsqd.logf(LOG_INFO, "[%s] IDENTIFY: %+v", c, data)
159159

160160
c.metaLock.Lock()
161161
c.ClientID = data.ClientID
@@ -317,7 +317,7 @@ func (c *clientV2) IsReadyForMessages() bool {
317317
readyCount := atomic.LoadInt64(&c.ReadyCount)
318318
inFlightCount := atomic.LoadInt64(&c.InFlightCount)
319319

320-
c.ctx.nsqd.logf(LOG_DEBUG, "[%s] state rdy: %4d inflt: %4d", c, readyCount, inFlightCount)
320+
c.nsqd.logf(LOG_DEBUG, "[%s] state rdy: %4d inflt: %4d", c, readyCount, inFlightCount)
321321

322322
if inFlightCount >= readyCount || readyCount <= 0 {
323323
return false
@@ -402,7 +402,7 @@ func (c *clientV2) SetHeartbeatInterval(desiredInterval int) error {
402402
case desiredInterval == 0:
403403
// do nothing (use default)
404404
case desiredInterval >= 1000 &&
405-
desiredInterval <= int(c.ctx.nsqd.getOpts().MaxHeartbeatInterval/time.Millisecond):
405+
desiredInterval <= int(c.nsqd.getOpts().MaxHeartbeatInterval/time.Millisecond):
406406
c.HeartbeatInterval = time.Duration(desiredInterval) * time.Millisecond
407407
default:
408408
return fmt.Errorf("heartbeat interval (%d) is invalid", desiredInterval)
@@ -421,8 +421,8 @@ func (c *clientV2) SetOutputBuffer(desiredSize int, desiredTimeout int) error {
421421
case desiredTimeout == 0:
422422
// do nothing (use default)
423423
case true &&
424-
desiredTimeout >= int(c.ctx.nsqd.getOpts().MinOutputBufferTimeout/time.Millisecond) &&
425-
desiredTimeout <= int(c.ctx.nsqd.getOpts().MaxOutputBufferTimeout/time.Millisecond):
424+
desiredTimeout >= int(c.nsqd.getOpts().MinOutputBufferTimeout/time.Millisecond) &&
425+
desiredTimeout <= int(c.nsqd.getOpts().MaxOutputBufferTimeout/time.Millisecond):
426426

427427
c.OutputBufferTimeout = time.Duration(desiredTimeout) * time.Millisecond
428428
default:
@@ -436,7 +436,7 @@ func (c *clientV2) SetOutputBuffer(desiredSize int, desiredTimeout int) error {
436436
c.OutputBufferTimeout = 0
437437
case desiredSize == 0:
438438
// do nothing (use default)
439-
case desiredSize >= 64 && desiredSize <= int(c.ctx.nsqd.getOpts().MaxOutputBufferSize):
439+
case desiredSize >= 64 && desiredSize <= int(c.nsqd.getOpts().MaxOutputBufferSize):
440440
c.OutputBufferSize = desiredSize
441441
default:
442442
return fmt.Errorf("output buffer size (%d) is invalid", desiredSize)
@@ -469,7 +469,7 @@ func (c *clientV2) SetMsgTimeout(msgTimeout int) error {
469469
case msgTimeout == 0:
470470
// do nothing (use default)
471471
case msgTimeout >= 1000 &&
472-
msgTimeout <= int(c.ctx.nsqd.getOpts().MaxMsgTimeout/time.Millisecond):
472+
msgTimeout <= int(c.nsqd.getOpts().MaxMsgTimeout/time.Millisecond):
473473
c.MsgTimeout = time.Duration(msgTimeout) * time.Millisecond
474474
default:
475475
return fmt.Errorf("msg timeout (%d) is invalid", msgTimeout)
@@ -482,7 +482,7 @@ func (c *clientV2) UpgradeTLS() error {
482482
c.writeLock.Lock()
483483
defer c.writeLock.Unlock()
484484

485-
tlsConn := tls.Server(c.Conn, c.ctx.nsqd.tlsConfig)
485+
tlsConn := tls.Server(c.Conn, c.nsqd.tlsConfig)
486486
tlsConn.SetDeadline(time.Now().Add(5 * time.Second))
487487
err := tlsConn.Handshake()
488488
if err != nil {
@@ -570,10 +570,10 @@ func (c *clientV2) QueryAuthd() error {
570570
}
571571
}
572572

573-
authState, err := auth.QueryAnyAuthd(c.ctx.nsqd.getOpts().AuthHTTPAddresses,
573+
authState, err := auth.QueryAnyAuthd(c.nsqd.getOpts().AuthHTTPAddresses,
574574
remoteIP, tlsEnabled, commonName, c.AuthSecret,
575-
c.ctx.nsqd.getOpts().HTTPClientConnectTimeout,
576-
c.ctx.nsqd.getOpts().HTTPClientRequestTimeout)
575+
c.nsqd.getOpts().HTTPClientConnectTimeout,
576+
c.nsqd.getOpts().HTTPClientRequestTimeout)
577577
if err != nil {
578578
return err
579579
}

nsqd/context.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)