github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/context.go (about)

     1  /*
     2  Copyright 2016 Stanislav Liberman
     3  Copyright 2022 Steven Stern
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9  http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package aeron
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/lirm/aeron-go/aeron/counters"
    24  	"github.com/lirm/aeron-go/aeron/idlestrategy"
    25  )
    26  
    27  // Context configuration options are located here https://github.com/real-logic/Aeron/wiki/Configuration-Options#aeron-client-options
    28  type Context struct {
    29  	aeronDir      string
    30  	mediaDriverTo time.Duration
    31  
    32  	errorHandler func(error)
    33  
    34  	newPublicationHandler     NewPublicationHandler
    35  	newSubscriptionHandler    NewSubscriptionHandler
    36  	availableImageHandler     AvailableImageHandler
    37  	unavailableImageHandler   UnavailableImageHandler
    38  	availableCounterHandler   AvailableCounterHandler
    39  	unavailableCounterHandler UnavailableCounterHandler
    40  
    41  	resourceLingerTo        time.Duration
    42  	publicationConnectionTo time.Duration
    43  	interServiceTo          time.Duration
    44  
    45  	idleStrategy idlestrategy.Idler
    46  }
    47  
    48  // NewContext creates and initializes new Context for Aeron
    49  func NewContext() *Context {
    50  	ctx := new(Context)
    51  
    52  	ctx.aeronDir = DefaultAeronDir + "/aeron-" + UserName
    53  
    54  	ctx.errorHandler = func(err error) { logger.Error(err) }
    55  
    56  	ctx.newPublicationHandler = func(string, int32, int32, int64) {}
    57  	ctx.newSubscriptionHandler = func(string, int32, int64) {}
    58  	ctx.availableImageHandler = func(Image) {}
    59  	ctx.unavailableImageHandler = func(Image) {}
    60  
    61  	ctx.mediaDriverTo = time.Second * 5
    62  	ctx.resourceLingerTo = time.Second * 3
    63  	ctx.publicationConnectionTo = time.Second * 5
    64  	ctx.interServiceTo = time.Second * 10
    65  
    66  	ctx.idleStrategy = idlestrategy.Sleeping{SleepFor: time.Millisecond * 4}
    67  
    68  	return ctx
    69  }
    70  
    71  // ErrorHandler sets the error handler callback
    72  func (ctx *Context) ErrorHandler(handler func(error)) *Context {
    73  	ctx.errorHandler = handler
    74  	return ctx
    75  }
    76  
    77  // AeronDir sets the root directory for media driver files
    78  func (ctx *Context) AeronDir(dir string) *Context {
    79  	ctx.aeronDir = dir
    80  	return ctx
    81  }
    82  
    83  // MediaDriverTimeout sets the timeout for keep alives to media driver
    84  func (ctx *Context) MediaDriverTimeout(to time.Duration) *Context {
    85  	ctx.mediaDriverTo = to
    86  	return ctx
    87  }
    88  
    89  // ResourceLingerTimeout sets the timeout for resource cleanup after they're released
    90  func (ctx *Context) ResourceLingerTimeout(to time.Duration) *Context {
    91  	ctx.resourceLingerTo = to
    92  	return ctx
    93  }
    94  
    95  // InterServiceTimeout sets the timeout for client heartbeat
    96  func (ctx *Context) InterServiceTimeout(to time.Duration) *Context {
    97  	ctx.interServiceTo = to
    98  	return ctx
    99  }
   100  
   101  func (ctx *Context) PublicationConnectionTimeout(to time.Duration) *Context {
   102  	ctx.publicationConnectionTo = to
   103  	return ctx
   104  }
   105  
   106  // newSubscriptionHandler sets an optional callback for new subscriptions
   107  func (ctx *Context) NewSubscriptionHandler(handler func(string, int32, int64)) *Context {
   108  	ctx.newSubscriptionHandler = handler
   109  	return ctx
   110  }
   111  
   112  // newPublicationHandler sets an optional callback for new publications
   113  func (ctx *Context) NewPublicationHandler(handler func(string, int32, int32, int64)) *Context {
   114  	ctx.newPublicationHandler = handler
   115  	return ctx
   116  }
   117  
   118  // AvailableImageHandler sets an optional default callback for available image notifications
   119  func (ctx *Context) AvailableImageHandler(handler func(Image)) *Context {
   120  	ctx.availableImageHandler = handler
   121  	return ctx
   122  }
   123  
   124  // UnavailableImageHandler sets an optional default callback for unavailable image notification
   125  func (ctx *Context) UnavailableImageHandler(handler func(Image)) *Context {
   126  	ctx.unavailableImageHandler = handler
   127  	return ctx
   128  }
   129  
   130  // CncFileName returns the name of the Counters file
   131  func (ctx *Context) CncFileName() string {
   132  	return ctx.aeronDir + "/" + counters.CncFile
   133  }
   134  
   135  // IdleStrategy provides an IdleStrategy for the thread responsible for communicating
   136  // with the Aeron Media Driver.
   137  func (ctx *Context) IdleStrategy(idleStrategy idlestrategy.Idler) *Context {
   138  	ctx.idleStrategy = idleStrategy
   139  	return ctx
   140  }
   141  
   142  // AvailableCounterHandler sets up a callback for when a Counter is available.  This will be added to the list before
   143  // additional handlers are added with Aeron.AddAvailableCounterHandler.
   144  func (ctx *Context) AvailableCounterHandler(handler AvailableCounterHandler) {
   145  	ctx.availableCounterHandler = handler
   146  }
   147  
   148  // GetAvailableCounterHandler gets the callback handler for when a counter is available.
   149  func (ctx *Context) GetAvailableCounterHandler() AvailableCounterHandler {
   150  	return ctx.availableCounterHandler
   151  }
   152  
   153  // UnavailableCounterHandler sets up a callback for when a Counter is unavailable.  This will be added to the list first
   154  // before additional handlers are added with Aeron.AddUnavailableCounterHandler.
   155  func (ctx *Context) UnavailableCounterHandler(handler UnavailableCounterHandler) {
   156  	ctx.unavailableCounterHandler = handler
   157  }
   158  
   159  func (ctx *Context) GetUnavailableCounterHandler() UnavailableCounterHandler {
   160  	return ctx.unavailableCounterHandler
   161  }