github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/counter.go (about) 1 // Copyright 2022 Steven Stern 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package aeron 16 17 import ( 18 "github.com/lirm/aeron-go/aeron/atomic" 19 "github.com/lirm/aeron-go/aeron/counters" 20 ) 21 22 type Counter struct { 23 registrationId int64 24 clientConductor *ClientConductor 25 atomicCounter *counters.AtomicCounter 26 counterId int32 27 isClosed atomic.Bool 28 } 29 30 func NewCounter( 31 registrationId int64, 32 clientConductor *ClientConductor, 33 counterId int32) (*Counter, error) { 34 35 counter := new(Counter) 36 counter.registrationId = registrationId 37 counter.clientConductor = clientConductor 38 atomicCounter, err := counters.NewAtomicCounter(clientConductor.CounterReader(), counterId) 39 if err != nil { 40 return nil, err 41 } 42 counter.atomicCounter = atomicCounter 43 counter.counterId = counterId 44 45 return counter, nil 46 } 47 48 func (c *Counter) RegistrationId() int64 { 49 return c.registrationId 50 } 51 52 func (c *Counter) Id() int32 { 53 return c.counterId 54 } 55 56 func (c *Counter) Counter() *counters.AtomicCounter { 57 return c.atomicCounter 58 } 59 60 func (c *Counter) Close() error { 61 if c.isClosed.CompareAndSet(false, true) { 62 if c.clientConductor != nil { 63 return c.clientConductor.releaseCounter(*c) 64 } 65 } 66 return nil 67 } 68 69 func (c *Counter) IsClosed() bool { 70 return c.isClosed.Get() 71 }